Rudiger
Rudiger

Reputation: 6769

Adding a form action to a config page

I've been trying to make an extension to add some function to the CMS. As it's a setting for the CMS I've added it to the settings tab. While I can take values and save them I needed an action on the page to synchronise a system and I can't get my action to be called, here is my code.

private static $db = array(
    'Path' => 'Varchar(50)',
);

private static $allowed_actions = array (
    'update',
);

public function updateCMSFields(FieldList $fields)
{
    $fields->addFieldsToTab('Root.Importer', array(
        ImporterPathField::create('Path', 'Path')->setDescription('Path to area'),
        FormAction::create('update', 'Synchronise')
    ));
}

public function update() {
    SS_Log::add_writer(new SS_LogEmailWriter('[email protected]'), SS_Log::ERR);

}

It doesn't get called. If I need to add the function to the left nav rather than part of the settings I'm ok with that too but I also tried that with even less success. Is it possible to get the action called on button press?

Upvotes: 2

Views: 100

Answers (1)

Tom Densham
Tom Densham

Reputation: 56

You need to place the $allowed_actions and the update method in an extension for CMSSettingsController. Also you should probably put the FormAction into the CMSActions list.

Here's how I would do this:

SiteConfigExtension.php

class SiteConfigExtension extends DataExtension
{
    private static $db = array(
        'Path' => 'Varchar(50)',
    );

    public function updateCMSFields(FieldList $fields)
    {
        $fields->addFieldsToTab('Root.Importer', array(
            ImporterPathField::create('Path', 'Path')->setDescription('Path to area')
        ));
    }

    public function updateCMSActions(FieldList $actions)
    {
        $actions->push(
            FormAction::create('update', 'Synchronise')
        );
    }
}

CMSSettingsControllerExtension.php

class CMSSettingsControllerExtension extends DataExtension
{
    private static $allowed_actions = array (
        'update',
    );

    public function update() {
        SS_Log::add_writer(new SS_LogEmailWriter('[email protected]'), SS_Log::ERR);
    }
}

Upvotes: 3

Related Questions