phpbackendsilverstripesilverstripe-4data-objects

Silverstripe 4.0.4 - getCMSActions not working as expected on dataobject


I am trying to add a copy dataobject button next to the save and delete button on a dataobject but "getCMSActions" does not seem to work.

I have followed the tutorials on the following pages:

https://docs.silverstripe.org/en/4/developer_guides/customising_the_admin_interface/how_tos/extend_cms_interface/#extending-the-cms-actions

https://docs.silverstripe.org/en/4/developer_guides/customising_the_admin_interface/how_tos/cms_alternating_button/

But both did not solve my problem my code currently looks like this.

public function getCMSActions() {
    $actions = parent::getCMSActions();

    if ($this->ID) {
        $actions->push(FormAction::create('copy', _t('SiteBlockAdmin.Copy', 'Copy'))
            ->setUseButtonTag(true)
            ->setAttribute('data-icon', 'arrow-circle-double'));
        $actions->push(DropdownField::create('BegrotingsPageCopyToID', '', BegrotingsPage::get()->map())
            ->setEmptyString('Selecteer pagina voor kopie'));
    }       

    return $actions;
}

What I want to achieve is to make the copy button and dropdownfield show up next to the save and delete button with the getCMSActions field.


Solution

  • The problem is that GridFieldDetailForm_ItemRequest::getFormActions() doesn't call $this->record->getCMSActions(), instead it defines its initial list of actions as $actions = new FieldList();.

    I assume you're managing your DataObject via a ModelAdmin.

    You can add an extension to this class and add the fields that way (but it's sub-optimal):

    # File: app/_config/extensions.yml
    SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest:
      extensions:
        MyExtension: MyExtension
    

    And your extension could look like this:

    <?php
    
    use SilverStripe\Forms\DropdownField;
    use SilverStripe\Forms\FieldList;
    use SilverStripe\Forms\FormAction;
    use SilverStripe\ORM\DataExtension;
    
    class MyExtension extends DataExtension
    {
        public function updateFormActions(FieldList $actions)
        {
            $record = $this->owner->getRecord();
            // This extension would run on every GridFieldDetailForm, so ensure you ignore contexts where
            // you are managing a DataObject you don't care about
            if (!$record instanceof YourDataObject || !$record->exists()) {
                return;
            }
    
            $actions->push(FormAction::create('copy', _t('SiteBlockAdmin.Copy', 'Copy'))
                ->setUseButtonTag(true)
                ->setAttribute('data-icon', 'arrow-circle-double'));
            $actions->push(DropdownField::create('BegrotingsPageCopyToID', '', BegrotingsPage::get()->map())
                ->setEmptyString('Selecteer pagina voor kopie'));
        }
    }
    

    I've also raised an issue to follow up on the misleading documentation: https://github.com/silverstripe/silverstripe-framework/issues/8773