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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

ModelAdmin - Adding new actions


Go to End


5 Posts   4974 Views

Avatar
Hamish

Community Member, 712 Posts

17 July 2009 at 10:04am

Edited: 17/07/2009 10:07am

Howdy all,

Quick question about adding form actions to Model Admin. I am using a decorator to add new form actions to object that is managed by a model admin subclass. I can add a new form action (i.e., next to save, delete) with the updateCMSActions hook, and can use the same call to sneak in the required javascript, however there does not appear to be a way to add a new method to the model admin (or, more specifically, the appropriate ModelAdmin_RecordController wrapper).

My code so far:

class MyCustomDecorator extends DataObjectDecorator {

	public function updateCMSActions(FieldSet &$fields) {
		Requirements::javascript('my_new_module/javascript/FormActions.js');
		if($this->owner->canEdit(Member::currentUser())){
			$fields->push(new FormAction('doSomeAction', 'My New Form Action'));
		}
	}

}

// javascript
// (replicated behavour from ModelAdmin.js)
(function($) {
$(document).ready(function() {
	/**
	 * Decommission button 
	 */
	$('#right #form_actions_right input[name=action_doSomeAction]').livequery('click', function(){
		var form = $('#right form');
		var formAction = form.attr('action') + '?' + $(this).fieldSerialize();
		
		// Post the data to save
		$.post(formAction, form.formToArray(), function(result){
			$('#right #ModelAdminPanel').html(result);
			
			statusMessage(ss.i18n._t('ModelAdmin.SAVED'));

			Behaviour.apply(); // refreshes ComplexTableField
			if(window.onresize) window.onresize();
		}, 'html');

		return false;
	});
})})(jQuery);

All groovy so far. The button appears within the ModelAdmin and clicking it will send a request to the following url:

http://mycoolsite/admin/mymanagedobject/theobject/1/EditForm?action_doSomeAction=SomeAction

But this returns a 500 error:

[User Error] Uncaught Exception: Object->__call(): the method 'dosomeaction' does not exist on 'Form'

This was not entirely unexpected, since I couldn't see any way to add the appropriate method to the record controller. Ideally, method 'doSomeAction' would live in the decorator, or the record controller would check for the method within the object (and, and applied decorators).

Without this capability, it would seem that the updateCMSActions method breaks modeladmin.

Any ideas?

Avatar
BLU42 Media

Community Member, 71 Posts

30 April 2010 at 5:47am

Hi Hamish-

I know this is an old post I'm responding to... wondering if you've had any luck in adding actions to Model Admin? I'm in the same position with a project.

Thanks for your time!
John

Avatar
joelg

Community Member, 134 Posts

12 May 2010 at 7:04am

Hi Hamish

I would very much like to know too if you made this work?

Avatar
Matze0681

Community Member, 25 Posts

11 August 2010 at 4:18pm

heres a solution if somebody still needs it.. know its an old post

just extend ModelAdmin_RecordController with your own RecordController class and overwrite the static $record_controller_class with your class like:

public static $record_controller_class = "XYZ_RecordController";

please ignore if obsolete...

Avatar
nitromike

Community Member, 3 Posts

21 March 2012 at 4:26am

Edited: 21/03/2012 4:27am

I found a solution for this topic, I know it's been a while, but I'd like to compare if anyone else has a solution to determine the best and most understandable way to accomplish adding actions to model admin.

Task, add Process action, when pressed it simply checks the Processed field
In DataObject:
function getCMSActions()
{
$actions = parent::getCMSActions();
$actions->push(new FormAction('doProcess','Process') );
return $actions;
}
function doProcess()
{
/* more complicated tasks can be performed here */
$this->Processed = 1;
return $this->write();
}

In ModelAdmin.js
/**
* RHS panel Process button
*/
$('#right input[name=action_doProcess]').live('click', function(){
var form = $('#right form');
var formAction = form.attr('action') + '?' + $(this).fieldSerialize();

// @todo TinyMCE coupling
if(typeof tinyMCE != 'undefined') tinyMCE.triggerSave();

// Post the data to save
$.post(formAction, form.formToArray(), function(result){
// @todo TinyMCE coupling
tinymce_removeAll();

$('#right #ModelAdminPanel').html(result);

if($('#right #ModelAdminPanel form').hasClass('validationerror')) {
statusMessage(ss.i18n._t('ModelAdmin.VALIDATIONERROR', 'Validation Error'), 'bad');
} else {
statusMessage(ss.i18n._t('ModelAdmin.PROCESSED', 'Processed'), 'good');
}

// Is jQuery.live a solution?
Behaviour.apply(); // refreshes ComplexTableField
if(window.onresize) window.onresize();
}, 'html');

return false;
});

In CustomModelAdmin_RecordController:
function doProcess($data,$form,$request)
{
$this->currentRecord->doProcess();
$request['Processed'] = 1;
if(Director::is_ajax()) {
return $this->edit($request);
} else {
Director::redirectBack();
}

}