Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

has_one inline editing / unlink action problem


Go to End


1426 Views

Avatar
Exposure

Community Member, 1 Post

12 February 2017 at 4:57pm

Hi, I'm a Silverstripe beginner and ran into a few issues. I want to be able to edit a has_one relation DataObject while editing the 'parent' (in my case an event). Since SS doesn't automatically provide a GridField or similar editing capabilities for this relation (like for many_many) I wanted to do this:

1) Create a new tab named 'Organiser' under Events that shows a GridField with all available organisers if NO link has been set to an organiser, yet. I installed the GridfieldrelationHandler extension that allows me to set the link within the GridField.

2) If a link to an organiser is already set, I want to show the GridField with a slightly config. Display only one entry with the linked record and replace the DELETE action in the GridField with an UNLINK action, because it's too easy to delete a record accidentally.

So far I ran into 2 issues:
A) The replaced DELETE action, now an UNLINK action, still deletes records from the Organisers table - even though it looks like the unlink button! Does that not work on has_one relations?

B) When I link or unlink (at the moment still delete) an organiser from an Event, the GridField does not get reloaded, hence my if/else condition doesn't work for different GridField configs in this situation. It works if I do a page reload but I suspect I need to add some kind of reload/refresh action after Silverstripe added or removed a link?

class Events extends DataObject {   
    private static $db = array (
    	'Name' => 'Varchar(100)',
    	'Date' => 'Date',
    	'ShortDesc' => 'Varchar(255)'
    );

    public static $has_one = array(
        'Organiser'    => 'Organisers'
    )

    public function getCMSfields() {
        $fields = parent::getCMSFields();

        // If an organiser is already linked to the event, use Gridfield config that only shows 1 entry with the linked organiser; add remove link action
    	if($this->Organiser()->exists()) {
			$organiserGridFieldConfig=GridFieldConfig_RecordEditor::create(1);
			$organiserGridFieldConfig->addComponent(new GridFieldHasOneRelationHandler($this, 'Organiser'));	// Using Arillo/GridfieldrelationHandler extension
			$organiserGridFieldConfig->removeComponentsByType('GridFieldDeleteAction');   						// remove default DELETE record action
			$organiserGridFieldConfig->addComponent(new GridFieldDeleteAction(true));     						// add remove link action

			$fields->addFieldToTab('Root.Organiser', $organiserGridField = new GridField('Organiser', 'Organiser', Organisers::get()->filter(array('ID'=> $this->OrganiserID)), $organiserGridFieldConfig));
        }
        // If no Organiser is linked to the Event, show all available organisers in GridField and have a different GridField config
        else {
            $organiserGridFieldConfig = GridFieldConfig_RecordEditor::create(10);
            $organiserGridFieldConfig->addComponent(new GridFieldHasOneRelationHandler($this, 'Organiser'));
            
            $fields->addFieldToTab('Root.Organiser', $organiserGridField = new GridField('Organiser', 'Organiser', Organisers::get(), $organiserGridFieldConfig));
        }

        return $fields;
    }
}


class Organisers extends DataObject {
    private static $db = array (
        'Name'          =>  'Varchar(30)',
        'City'          =>  'Varchar(30)',
        'Address'       =>  'Varchar(40)',
        'Postcode'      =>  'Varchar(10)',
        'Phone'         =>  'Varchar(20)'
    );

    public static $has_many = array(
        'Events' => 'Event'
    );
}