Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Custom button and its functionality in Admin Silverstripe?

How to add Custom button and its functionality in Admin Silverstripe?

Please tell me solution.

Custom Button add only in one menu.

like image 236
user3111011 Avatar asked Aug 21 '15 12:08

user3111011


1 Answers

Like @wmk mentioned in the comments, you can just take the framework code for GridFieldPrintButton as a base and go from there. SilverStripe also have a basic tutorial for creating a custom ActionProvider.

Rather than rehash the tutorial here, I will provide you a very basic custom action provider that you can copy and extend to do what you need. While you don't note the exact result you are wanting from the button, I will provide just a very generic class.

This code is a stripped down version of the GridFieldPrintButton that @wmk mentioned. It supports both the button itself invoking the custom code as well as the URL.

I've noted in the code a reference that I have kept to "grid-print-button", this is to make your button sit nicely next to the print rather than likely sitting on another line (as it did in my testing on an older 3.1 site I built).

class GridFieldCustomButton implements GridField_HTMLProvider, GridField_ActionProvider, GridField_URLHandler {

    protected $targetFragment;
    protected $someCustomConstructData;

    //TargetFragment is just for positioning control of the HTML fragment
    //SomeCustomConstructData is just an example of providing some default options into your butotn
    public function __construct($targetFragment = "after", $someCustomConstructData = null) {
        $this->targetFragment = $targetFragment;
        $this->someCustomConstructData = $someCustomConstructData;
    }

    //Generate the HTML fragment for the GridField
    public function getHTMLFragments($gridField) {
        $button = new GridField_FormAction(
            $gridField,
            'custom',
            'Custom Action',
            'custom',
            null
        );
        return array(
            //Note: "grid-print-button" is used here to match the styling of the buttons in ModelAdmin
            $this->targetFragment => '<p class="grid-print-button">' . $button->Field() . '</p>',
        );
    }

    public function getActions($gridField) {
        return array('myCustomAction');
    }

    public function handleAction(GridField $gridField, $actionName, $arguments, $data) {
        if($actionName == 'myCustomAction') {
            return $this->handleMyCustomAction();
        }
    }

    //For accessing the custom action from the URL
    public function getURLHandlers($gridField) {
        return array(
            'myCustomAction' => 'handleMyCustomAction',
        );
    }

    //Handle the custom action, for both the action button and the URL
    public function handleMyCustomAction($gridField, $request = null) {
        //Do your stuff here!
    }
}

Continuing on from the discussion in the comments, you will need to modify your custom ModelAdmin to add new components to its GridField.

class MyCustomAdmin extends ModelAdmin
{
    private static $managed_models = array(
        'MyCustomObject' 
    );

    private static $url_segment = 'custom-admin';
    private static $menu_title = 'All Custom Objects';

    public function getEditForm($ID = null, $Fields = null)
    {
        $form = parent::getEditForm($ID, $Fields);
        $fields = $form->Fields();

        $gridField = $fields->fieldByName('MyCustomObject');
        $gridFieldConfig = $gridField->getConfig();
        $gridFieldConfig->addComponent(new GridFieldCustomButton());

        return $form;
    }
}

Specifically, the line $gridFieldConfig->addComponent(new GridFieldCustomButton()) does the work, taking your custom button as I have shown above and added it to the ModelAdmin. You can also specify where it should go in the GridField too by providing "buttons-before-left" as the first argument in the GridFieldCustomButton constructor.

eg. $gridFieldConfig->addComponent(new GridFieldCustomButton("buttons-before-left"))

More information regarding GridField fragments can be found in the SilverStripe developer documentation.

like image 102
Turnerj Avatar answered Nov 07 '22 03:11

Turnerj