4

I'm trying to remove the Export to CSV button in the top of a GridField in ModelAdmin.

I can't seem to find the class that creates the button (GridFieldExportButton right?). I'm guessing there is a function that populates the GridField with buttons / "actions" which I'm not familiar with.

Semicolon
  • 1,904
  • 14
  • 26

1 Answers1

8

To remove the scaffolded GridField for relationships...

class MyDataObject extends DataObject {

    ...

    private static $has_many= array(
        'OtherDataObjects' => 'OtherDataObject'
    );

    ...

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

        if($grid = $fields->dataFieldByName('OtherDataObjects'))
            $grid->getConfig()
                ->removeComponentsByType('SilverStripe\Forms\GridField\GridFieldExportButton');

        return $fields;
    }

    ...

}

If you are making the GridField then just add this when you create the field...

$gridField->getConfig()->removeComponentsByType('SilverStripe\Forms\GridField\GridFieldExportButton'); 

If you are looking for a gridfield that isn't within a data object edit form and is actually...

class MyAdmin extends ModelAdmin {

    ...

    function getEditForm($id = null, $fields = null) {
        $form = parent::getEditForm($id, $fields);

        if($this->modelClass == 'MyDataObjectName') {
            $form->Fields()
                ->fieldByName($this->sanitiseClassName($this->modelClass))
                ->getConfig()
                ->removeComponentsByType('SilverStripe\Forms\GridField\GridFieldExportButton');
        }
        return $form;
    }

    ...
}

Setting model_importers to empty will do the reverse and remove the import ...

class MyAdmin extends ModelAdmin {

    ...

    static $model_importers = array();

    ...
}
Barry
  • 3,303
  • 7
  • 23
  • 42
  • That seems to get rid of the import function, not the export button. Also valuable by the way, since I want to hide that from user as well. – Semicolon Jul 14 '16 at 12:46
  • Indeed! Updated to include the removal of the button you actually asked about! – Barry Jul 14 '16 at 12:57
  • 'MyFieldName' should be the DataObject class name? – Semicolon Jul 14 '16 at 13:06
  • Sorry, I don't understand why any $db field should be addressed to remove the export button. The DataObject has several $db fields, which one should I put in there? – Semicolon Jul 14 '16 at 13:20
  • ah, I'm going to get there on this one.. the issue is that you have a gird field named after a relationship (like a has many or many many) it is that that should be there... this is the name of the "grid field" if you have just created your own gridfield then just use $grid->getConfig() ->removeComponentsByType('GridFieldExportButton'); on that field to remove it. – Barry Jul 14 '16 at 13:47
  • The thing is, this dataobject has no relation set (ie has_many). It's pulled into a ModelAdmin extension (custom cms section) using $managed_models=array('DataObjectClassName'); – Semicolon Jul 14 '16 at 14:09
  • Ok... added a case for that too... I _must_ have covered every angle now lol :) – Barry Jul 14 '16 at 14:18
  • Haha you sticked to it to the end. The last addition worked succesfully. Cheers. – Semicolon Jul 14 '16 at 14:20
  • 1
    Not sure if this solution was meant for ss3, but in order for this to work in ss4, you need to write the full path to the component, `removeComponentsByType('SilverStripe\Forms\GridField\GridFieldExportButton')` – otherwise the component can't be found. – WoodrowShigeru Jan 17 '19 at 10:02
  • @WoodrowShigeru absolutely, this is for SS3, SS4 stable release was end of 2017, over a year after this post – Barry Jan 17 '19 at 11:22
  • Ah, comparing post dates with release dates. That's a helpful trick. – WoodrowShigeru Jan 17 '19 at 12:05
  • Also I wrote the post and I never moved over to SS3 :) – Barry Jan 17 '19 at 17:01
  • @WoodrowShigeru you should add yours as an answer as I've confirmed that works it SS4 – Rudiger Apr 12 '20 at 06:14
  • @Rudiger as I've just seen this I've updated, happy this post still helps some 4 years later :) – Barry Apr 12 '20 at 08:55