01
Sep
8
Adding a CMS action the (slightly) hacky way
What it Does
Adds a button to the bottom of the page which runs a custom function.
Requirements
SilverStripe v2.3.2 +
The bit you probably know
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 although certainly not the 'correct' way to do it, still does not require any editing of the core files and is not particularly messy.
Note. If anyone from SilverStripe is reading this (or anyone else who knows how these functions work), please oh please write some 'proper' documentation for us. I/we will be very grateful :)
So back to my way of doing this, first thing we need to do is add the form action to our page using the getCMSActions() function which many of you will have already had (probably frustrating) experience of. So inside your page model we add this:
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, for some reason this will give you an error, 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.
So create a new file inside mysite/code called CMSActionDecorator.php and put this code in it:
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:
Config settings
Finally we need to tell SilverStripe that we have extended CMSMain by adding this line to our mysite/_config.php:
And there you have it, the hacky way to add form actions to your CMS pages. Wasn't all that bad was is? :)
About the Author
Name: Aram Balakjian
Website: http://www.aabweb.co.uk
Aram is a web designer/developer running London based agency aab web. He has a strong passion for developing attractive, usable sites around the SilverStripe CMS.
Comments (8)
-
[url=http://www.mytampabayscience.com/profile/buycheapNexium]xxx[/url]
Posted by Tom Tester, 22/07/2010 12:39pm (7 days ago)
-
<p class="credit">© copyright <a href="home" title="go to SSbits home">SSbits</a> 2009 - 2010 | Powered by <a href="http://www.silverstripe.org">SilverStripe CMS</a> | Site by <a href="http://www.aabweb.co.uk">aab web</a></p>
Posted by Tom Tester, 22/07/2010 12:38pm (7 days ago)
-
Thanks for updating us with the nice information.
Posted by cms website development, 19/05/2010 2:39am (2 months ago)
-
If you are using ModelAdmin then this is a very useful thread on SS Forums for this...
http://www.silverstripe.org/customising-the-cms/show/277197#post277197Posted by Barry, 28/01/2010 4:31am (6 months ago)
-
Good work!!
Posted by Ardjan, 07/09/2009 4:16am (11 months ago)
-
Hi Sanchez
Unfortunately I don't have much (read any) experience with model admin. If there is anyone out there who could contribute some model admins snippets or tutorials, feel free to use the big button at the top of each page to submit your post ;)
Posted by aram, 03/09/2009 5:49pm (11 months ago)
-
How would you do this with ModelAdmin?
Posted by Sanchez, 02/09/2009 8:36pm (11 months ago)
-
THANK YOU!!!!!!!!!!
Posted by Sanchez, 02/09/2009 7:53pm (11 months ago)
RSS feed for comments on this page RSS feed for all comments