Jump to:

23374 Posts in 18175 Topics by 2866 members

General Questions

SilverStripe Forums » General Questions » Adding custom action in view page of modeladmin

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 205 Views
  • lozhowlett
    Avatar
    Community Member
    146 Posts

    Adding custom action in view page of modeladmin Link to this post

    I need to extend the grid field list page with a custom button.

    Please see attached image, I want to add a "Delete All" records button in my ModelAdmin extension of products, that I can control by VendorID (A product has a vendor). So that when vendors are logged in they can select delete all and upload a new set of products.

    I cant use replace data on the import CSV, becuase it will replace all vendors. So i have disabled that and instead want to create a custom button that will only delete their products.

    Thanks!

    Attached Files
  • Phat
    Avatar
    Community Member
    8 Posts

    Re: Adding custom action in view page of modeladmin Link to this post

    First, you need to create a custom GridFieldComponent for 'Delete All' button - something like below

    class GridFieldDeleteAllButton implements GridField_HTMLProvider, GridField_ActionProvider {
       protected $targetFragment;

       public function __construct($targetFragment = "before") {
          $this->targetFragment = $targetFragment;
       }

       public function getHTMLFragments($gridField) {
          $button = new GridField_FormAction(
             $gridField,
             'deleteall',
             _t('TableListField.DELETEALL', 'Delete All'),
             'deleteall',
             null
          );
          $button->setAttribute('data-icon', 'cross-circle');
          $button->addExtraClass('no-ajax');
          return array(
             $this->targetFragment => '<p class="grid-csv-button">' . $button->Field() . '</p>',
          );
       }

       public function getActions($gridField) {
          return array('deleteall');
       }

       public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
          if($actionName == 'deleteall') {
             return $this->handleDeleteAll($gridField);
          }
       }

       public function handleDeleteAll($gridField, $request = null) {
          //Add your own DELETE logic here !
          $items=$gridField->getList();
          foreach ($items as $item) {
             $item->delete();
          }
          Controller::curr()->redirectBack();
       }
    }

    You then can add this new component to your gridfield under your ModelAdmin extension

    public function updateEditForm(&$form) {
    $gridField = $form->Fields()->fieldByName($this->sanitiseClassName($this->owner->modelClass));
    $gridField->getConfig()->addComponent(new GridFieldDeleteAllButton('before'));
    }

    205 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.