Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

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

CMS success of failure message


Go to End


1212 Views

Avatar
mi32dogs

Community Member, 75 Posts

19 November 2016 at 3:58pm

Ok, can’t find it anywhere so let see if somebody knows

In the administration I have some custom actions, for example I have a gridfield button that deletes all records.

How do I display a success of failure message on the screen to the admin user.

I use this code

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('deleteWithConfirm gridfield-better-buttons-delete');
		return array(
			$this->targetFragment => '<p class="grid-del-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->hide = 1;
			$item->AddDate = date('Y-m-d H:i:s');
			//$item->write();
			Debug::show($item->ID);
		}
		Controller::curr()->redirectBack();
	}
}