Jump to:

23370 Posts in 18160 Topics by 2864 members

General Questions

SilverStripe Forums » General Questions » ModelAdmin - Adding new actions

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

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

Page: 1
Go to End
Author Topic: 3070 Views
  • Hamish
    Avatar
    Community Member
    712 Posts

    ModelAdmin - Adding new actions Link to this post

    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?

  • BLU42 Media
    Avatar
    Community Member
    71 Posts

    Re: ModelAdmin - Adding new actions Link to this post

    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

  • joelg
    Avatar
    Community Member
    129 Posts

    Re: ModelAdmin - Adding new actions Link to this post

    Hi Hamish

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

  • Matze0681
    Avatar
    Community Member
    25 Posts

    Re: ModelAdmin - Adding new actions Link to this post

    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...

  • nitromike
    Avatar
    Community Member
    3 Posts

    Re: ModelAdmin - Adding new actions Link to this post

    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();
    }

    }

    3070 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.