How to add a new button to CMS

Silverstripe 4.5

Forgive me for flooding the forum I’m new to SIlverstripe. I’m again stuck with adding a new button to the admin CMS. I’ve tried various approaches and can’t believe why this is so complicated.

I tried this with no luck.

I simply want to add another button here to say “save and finalise” or “save and exit”.
But I was unable to event the get that button appeared on the UI…!

This is where I want it to appear: SilverStripe - Inspection Reports

Does anyone know of a good tutorial/ example that I can follow? The above example is missing a lot of information and I can’t figure out why it is not working.

This is one approach I’ve tried



    public function getCMSActions() {
        $actions = parent::getCMSActions(); 
        $saveAction = new FormAction ('saveExit', 'Save and exit');
        $saveAction->addExtraClass('ss-ui-action-constructive');
        $actions->push($saveAction); 
        return $actions;
    }

    public function getCMSFields()

    {
        $fields = parent::getCMSFields();
        $fields->addFieldToTab('Root.Main', 
            FormAction::create('saveExit', 'Save exit')
        );
     //other fields
    return $fileds
}

Thanks,
Isuru

This issue indicates that the documentation is wrong :slightly_frowning_face: but unfortunately doesn’t say the right way to do it.

1 Like

I’ve managed to do this by extending the GridFieldDetailForm_ItemRequest because that one is responsible for rendering the edit form in the grin (surprise!)

SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest:
  extensions:
    - App\Extensions\TargetClassActions
<?php
namespace App\Extensions;

use SilverStripe\Control\Controller;
use SilverStripe\Core\Extension;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\GridField\GridFieldDetailForm_ItemRequest;
use SilverStripe\Forms\LiteralField;

/**
 * @property-read GridFieldDetailForm_ItemRequest $owner
 */
class TargetClassActions extends Extension {


    public function updateFormActions(FieldList $actions)
    {
        $targetClass = TargetClass::class;
        $record = $this->owner->getRecord();
        if($record && $record->exists() && $record->ClassName == $targetClass) {
            $action = 'newaction';
            $classDesc = str_replace('\\','-',$targetClass);
            $link = '/'.Controller::curr()->Link("$classDesc/$action/{$record->ID}");

            $actions->push(
                LiteralField::create('Literal_'.$action,'<a target="_blank" href="'.$link.'" class="btn btn-outline-secondary">Download Unclaimed tokens</a>')
            );
        }
    }
}
<?php
namespace App\Admin;

use SilverStripe\Admin\ModelAdmin;
use SilverStripe\Control\HTTPRequest;

class TargetClassAdmin extends ModelAdmin
{

    private static $allowed_actions = [
        'newaction',
    ];
    private static $url_handlers = [
        'newaction/$ID' => 'newaction',
    ];

    private static $managed_models = [
        TargetClass::class,
    ];

    public function newaction(HTTPRequest $request)
    {
        $params = $request->match('$ID');

        //do the stuff
    }

}