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?