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

Adding a button to the backend CMS in ModelAdmin -- SS 3.1


Go to End


2 Posts   5634 Views

Avatar
MJA

Community Member, 21 Posts

12 December 2013 at 11:03pm

Edited: 12/12/2013 11:20pm

Hi guys,

I'm trying to add a button in the CMS edit/amend page in ModelAdmin.

I have worked out how to add the button (code below)

This places a new button at the bottom of the CMS page beside the standard CMS Add/Delete buttons -- all good so far

The problem is that on clicking any of the CMS buttons on this page including the standard buttons SS flashes up a Forbidden message and does nothing else

Clearly I am missing something here, but I cannot for the life of me work out out what

Any help you can offer would be most welcome

Thanks in advance

Mike Armstrong
(Silvertoad Ltd)

the code so far

Quote.php

	class Quote extends DataObject {
	
		public function getCMSActions() {
			$actions = parent::getCMSActions();
			
			$bookAction = new FormAction ('doBookVehicle', 'Book Vehicle');
			$bookAction->addExtraClass('ss-ui-action-constructive');
			$actions->push($bookAction);
			
			return $actions;
		}
	
	}	// end class QuoteValidator

QuoteAdmin.php

	class QuoteAdmin extends ModelAdmin {
	
		public function getEditForm($id = null, $fields = null) {
			$form = parent::getEditForm($id, $fields);
			
			$listField = $form->Fields()->fieldByName($this->modelClass);
			if ($gridField = $listField->getConfig()->getComponentByType('GridFieldDetailForm'))
				$gridField->setItemRequestClass('QuoteFieldDetailForm_ItemRequest');
			
			return $form;
		}
	
	}	// end class QuoteAdmin

	class QuoteFieldDetailForm_ItemRequest extends GridFieldDetailForm_ItemRequest {
	
		public function ItemEditForm() {
			$form = parent::ItemEditForm();
			$formActions = $form->Actions();
			
			if ($actions = $this->record->getCMSActions())
				foreach ($actions as $action)
					$formActions->push($action);
			
			return $form;
		}
		
		public function doBookVehicle($data, $form) {
			// do stuff here
		}
	
	}	// end class QuoteFieldDetailForm_ItemRequest

Avatar
MJA

Community Member, 21 Posts

17 December 2013 at 10:58pm

Solved

Just in case anyone else is having trouble with this, I have now found the answer
And it is (of course) very simple indeed

Simply needed to add an $allowed_actions array to the controller class (i.e. to QuoteFieldDetailForm_ItemRequest)
Code below:-

	class QuoteFieldDetailForm_ItemRequest extends GridFieldDetailForm_ItemRequest {
	
		private static $allowed_actions = array (
			'edit',
			'view',
			'ItemEditForm'
		);
		
		public function ItemEditForm() {
			$form = parent::ItemEditForm();
			$formActions = $form->Actions();
			
			if ($actions = $this->record->getCMSActions())
				foreach ($actions as $action)
					$formActions->push($action);
			
			return $form;
		}
		
		public function doBookVehicle($data, $form) {
		}
	
	}	// end class QuoteFieldDetailForm_ItemRequest