Jump to:

23550 Posts in 19336 Topics by 2890 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: 248 Views
  • lozhowlett
    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.


    Attached Files
  • Phat
    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(
             _t('TableListField.DELETEALL', 'Delete All'),
          $button->setAttribute('data-icon', 'cross-circle');
          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 !
          foreach ($items as $item) {

    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'));

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.