Steve
Steve

Reputation: 1211

Add a new button in Model Admin in SilverStripe 3.1

I have created a new model admin using SilverStripe 3.1.

I wish to add a button next to the 'Add' button on top of the grid view. Is there any way I can do this and create an action for this?

Here is what I found so far.

public function getEditForm($id = null, $fields = null)
{
    /**
     * @var $EditForm CMSForm
     */
    $EditForm = parent::getEditForm($id, $fields);

    $EditForm->Fields()->add(LiteralField::create('Sync', '<button><a href="www.google.com"> Sync </a></button>'));

    return $EditForm;
}

Also I want to have a handler for this.

Upvotes: 2

Views: 633

Answers (1)

Steve
Steve

Reputation: 1211

So after some time I have figured out what I need to do. There are threesteps to this.

Step one: Create a new class Name it what ever and make it extend GridField_HTMLProvider

class GridFieldSyncButton implements GridField_HTMLProvider
{

    /**
     * @var string
     */
    protected $targetFragment;

    /**
     * @param string $targetFragment
     */
    public function __construct($targetFragment = 'before')
    {
        $this->targetFragment = $targetFragment;
    }

    /**
     * @param $gridField
     * @return array
     */
    public function getHTMLFragments($gridField)
    {
        //-- The link to where the button links
        $data = new ArrayData(array('Sync' => Controller::join_links('link')));

        //--
        return array
        (
            $this->targetFragment => $data->renderWith('GridFieldSyncButton')
        );
    }
}

here what you did is created a new grid field component which you will be able to attach

Step two: Create the template

<a href="$Sync" class="ss-ui-button ui-button ui-widget ui-state-default ui-corner-all" data-icon="add">
    Sync Calculators
</a>

<br/><br/>

Step Three : Add it to your grid

$GridField = $EditForm->Fields()->items[0];
$GridField->getConfig()->addComponent(new GridFieldSyncButton());

Upvotes: 1

Related Questions