I want to add a "Delete All" action button to the Userforms Submissions Gridfield. I have created the code for the button in another file.
On the page that extends UserDefinedForm I am trying to get the existing inherited Submissions gridfield and add another component (the button) to it:
class CompetitionFormPage extends UserDefinedForm {
static $db = array(
'Details' => 'HTMLText'
);
public function getCMSFields() {
$fields = parent::getCMSFields();
$DetailsField = new HtmlEditorField('Details', 'Details');
$fields->addFieldsToTab('Root.Main', array(
new HtmlEditorField('Details', 'Details')
));
//get exisitng Submissions gridfield
$gridField = $fields->fieldByName('Submissions');
$gridField->getConfig()->addComponent(new GridFieldDeleteAllButton());
return $fields;
}
}
class CompetitionFormPage_Controller extends UserDefinedForm_Controller {
}
This results in a Fatal error: Call to a member function getConfig() on a non-object
For reference here is the GridFieldDeleteAllButton
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();
}
}
Any help, much appreciated.