Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add buttons for custom action to ModelAdmin

When managing Dataobjects with ModelAdmin in Silverstripe 3, i was wondering how you can add buttons for custom action to the list view as well as to the edit-page.

I've managed to add a button for a action to the edit-page by setting the ItemRequest on the ModelAdmins GridField class with the code below:

class MyModelAdmin extends ModelAdmin
{
    //...

    public function getEditForm($id = null, $fields = null)
    {
        $form = parent::getEditForm($id, $fields);
        $form
            ->Fields()
            ->fieldByName($this->sanitiseClassName($this->modelClass))
            ->getConfig()
            ->getComponentByType('GridFieldDetailForm')
            ->setItemRequestClass('MyModelGridFieldDetailForm_ItemRequest');

        return $form;
    }

}

MyModelGridFieldDetailForm_ItemRequest.php

class MyModelGridFieldDetailForm_ItemRequest extends GridFieldDetailForm_ItemRequest
{
    function ItemEditForm()
    {
        $form = parent::ItemEditForm();
        $formActions = $form->Actions();

        $button = FormAction::create('myAction');
        $button->setTitle('My Custom Action');
        $button->addExtraClass('ss-ui-action-constructive');
        $formActions->push($button);

        $form->setActions($formActions);
        return $form;
    }


    function myAction($data, $form)
    {

        //do things

        $form->sessionMessage('My Action has been successful', 'good');

        if ($this->gridField->getList()->byId($this->record->ID)) {
            return $this->edit(Controller::curr()->getRequest());
        } else {
            $noActionURL = Controller::curr()->removeAction($data['url']);
            Controller::curr()->getRequest()->addHeader('X-Pjax', 'Content');
            return Controller::curr()->redirect($noActionURL, 302);
        }
    }

}

So the question is:

Can i have a button for the same action in the ModelAdmin GridField Listview? So that it appears next to the edit and delete button:

ModelAdmin GridField actions

like image 662
mgherkins Avatar asked Aug 21 '13 05:08

mgherkins


People also ask

What are the custom admin actions?

Our custom actions are the nice looking deposit and withdraw buttons next to each account. Why Not Use the Existing Admin Actions? The built-in admin actions operate on a queryset. They are hidden in a dropbox menu in the top toolbar, and they are mostly useful for executing bulk operations. A good example is the default delete action.

How to add custom actions for change forms in Django admin?

15 Django Admin does not provide a way to add custom actions for change forms. However, you can get what you want with a few hacking. First you will have to override the submit row. your_app/templates/admin/submit_line.html

How to add bulk actions in articleadmin?

The django docs explain how to add bulk actions, by adding to the actions on a model, like so: class ArticleAdmin(admin.ModelAdmin): actions = ['make_published'] def make_published(self, request, queryset): queryset.update(status='p') make_published.short_description = "Mark selected stories as published"

How do I add a batch action to an existing action?

The first step is to create a custom AdminController . Then, create a new method to handle the batch action. The method name must follow the pattern action_name + BatchAction () and they receive an array argument with the IDs of the entities the action should be applied to.


1 Answers

You can do this by creating your own GridFieldComponent implementing GridField_ColumnProvider and GridField_ActionProvider.

In your case you can pretty much copy the GridFieldDeleteAction class and edit getColumnContent() with your own GridField_FormAction and edit handleAction() to do your magic.

like image 142
colymba Avatar answered Oct 03 '22 06:10

colymba