Tutorials - Big bits of code to help you do more
Adding a CMS action
Tweet1 September 2009 | | | Supports v2.4, v2.3
If like me you cannot figure out how on earth to use the updateCMSActions() function in a decorator and you also can't get the getCMSActions() function to find your custom method inside the controller then here is a way to make it work, which will allow you to create custom actions and buttons to trigger those actions within the CMS.
So let's jump strait in, first thing we need to do is add the form action to our page using the getCMSActions() function. So inside your page model we add this:
function getCMSActions(){
$actions = parent::getCMSActions();
$Action = new FormAction(
"doAction",
"Do something different"
);
$actions->push($Action);
return $actions;
}
Decorating CMSMain
Now we have our button you might have though adding the doAction() function would be a simple case of sticking it into our controller. Unfortunately not, this will give you an error because when viewing a page in the CMS the edit form does not have access to the functions in our controller. So instead we are going to add our function to the same place as the other CMS actions: inside CMSMain. However to avoid having to edit the core files we will use the LeftAndMainDecorator to do this. This works in exactly the same way as the DataObjectDecorator but, you guessed it, decorates LeftAndMain, which is the code class that controlls much of the CMS interface.
So create a new file inside mysite/code called CMSActionDecorator.php and put this code in it:
<?php
class CMSActionDecorator extends LeftAndMainDecorator {
function doAction(){
FormResponse::status_message(sprintf('All good!'),'good');
return FormResponse::respond();
}
}
So all our doAction() function is going to do when run is show a green 'All good!' message at the bottom of the page. You can however put anything you like in here.
Something a little more useful
At this point you may be wondering how much use this is without having the currently selected pages ID. Well by adding $id = (int)$_REQUEST['ID']; to the top of this function we can then use DataObject::get_by_id() or similar to get the current page and work on it. For example say I wanted to create a button that deleted all the DataObjects attached to the current page I could change my doAction() function to something like this:
function doAction(){
$id = (int)$_REQUEST['ID'];
$DataObjects = DataObject::get('SomeObject', 'PageID=' . $id);
foreach($DataObjects as $DataObject){
$DataObject->Delete();
}
FormResponse::status_message(sprintf('Deleted all objects' ),'good');
return FormResponse::respond();
}
Config settings
Finally we need to tell SilverStripe that we have extended CMSMain by adding this line to our mysite/_config.php:
Object::add_extension('CMSMain', 'CMSActionDecorator');
And there you have it, the slightly hacky but still usable way to add form actions to your CMS pages.
5 Comments
RSS feed for comments on this page RSS feed for all comments
FullWebService
22/01/2011 12:58pm (1 year ago)
Two questions:
Is it possible to add any Javascript to these actions?
Is it possible to add buttons to the left of the default actions? A prepend instead of push sort of thing.
FullWebService
23/01/2011 11:48am (1 year ago)
I found the prepend thing: insertFirst()
VRoxane
26/05/2011 5:42pm (12 months ago)
Hi Aram !
Great job as usual :) I was wondering : "how to add a new Action to a ModelAdmin Managed model ?"
I did this :
Object::add_extension('ModelAdmin', 'MAActionDecorator');
Created the MAActionDecorator.php and pnd put the code you gave in my DataObject. The button shows but nothing happens when I click it. No green 'All good!' message shows up :(
Any idea ?
Aram Balakjian
26/05/2011 5:51pm (12 months ago)
Hi Roxanne,
It's slightly different for ModelAdmin, luckily I have written a tutorial for that one too :)
http://www.ssbits.com/tutorials/2011/add-a-duplicate-button-to-model-admin/
Let me know how you get on.
Aram
VRoxane
27/05/2011 7:05pm (12 months ago)
Of course you DID write another wonderful tutorial !
And after little struggle with my Dowhat Iwant function.... I did it !
Thank you Aram :) !!!
Post a comment ...
You cannot post comments until you have logged in. Login Here.