Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Adding a button to the backend CMS in ModelAdmin -- SS 3.1

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

Page: 1
Go to End
Author Topic: 262 Views
  • MJA
    Avatar
    Community Member
    14 Posts

    Adding a button to the backend CMS in ModelAdmin -- SS 3.1 Link to this post

    Hi guys,

    I'm trying to add a button in the CMS edit/amend page in ModelAdmin.

    I have worked out how to add the button (code below)

    This places a new button at the bottom of the CMS page beside the standard CMS Add/Delete buttons -- all good so far

    The problem is that on clicking any of the CMS buttons on this page including the standard buttons SS flashes up a Forbidden message and does nothing else

    Clearly I am missing something here, but I cannot for the life of me work out out what

    Any help you can offer would be most welcome

    Thanks in advance

    Mike Armstrong
    (Silvertoad Ltd)

    the code so far

    Quote.php

       class Quote extends DataObject {
       
          public function getCMSActions() {
             $actions = parent::getCMSActions();
             
             $bookAction = new FormAction ('doBookVehicle', 'Book Vehicle');
             $bookAction->addExtraClass('ss-ui-action-constructive');
             $actions->push($bookAction);
             
             return $actions;
          }
       
       }   // end class QuoteValidator

    QuoteAdmin.php

       class QuoteAdmin extends ModelAdmin {
       
          public function getEditForm($id = null, $fields = null) {
             $form = parent::getEditForm($id, $fields);
             
             $listField = $form->Fields()->fieldByName($this->modelClass);
             if ($gridField = $listField->getConfig()->getComponentByType('GridFieldDetailForm'))
                $gridField->setItemRequestClass('QuoteFieldDetailForm_ItemRequest');
             
             return $form;
          }
       
       }   // end class QuoteAdmin

       class QuoteFieldDetailForm_ItemRequest extends GridFieldDetailForm_ItemRequest {
       
          public function ItemEditForm() {
             $form = parent::ItemEditForm();
             $formActions = $form->Actions();
             
             if ($actions = $this->record->getCMSActions())
                foreach ($actions as $action)
                   $formActions->push($action);
             
             return $form;
          }
          
          public function doBookVehicle($data, $form) {
             // do stuff here
          }
       
       }   // end class QuoteFieldDetailForm_ItemRequest

  • MJA
    Avatar
    Community Member
    14 Posts

    Re: Adding a button to the backend CMS in ModelAdmin -- SS 3.1 Link to this post

    Solved

    Just in case anyone else is having trouble with this, I have now found the answer
    And it is (of course) very simple indeed

    Simply needed to add an $allowed_actions array to the controller class (i.e. to QuoteFieldDetailForm_ItemRequest)
    Code below:-

       class QuoteFieldDetailForm_ItemRequest extends GridFieldDetailForm_ItemRequest {
       
          private static $allowed_actions = array (
             'edit',
             'view',
             'ItemEditForm'
          );
          
          public function ItemEditForm() {
             $form = parent::ItemEditForm();
             $formActions = $form->Actions();
             
             if ($actions = $this->record->getCMSActions())
                foreach ($actions as $action)
                   $formActions->push($action);
             
             return $form;
          }
          
          public function doBookVehicle($data, $form) {
          }
       
       }   // end class QuoteFieldDetailForm_ItemRequest

    262 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.