Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Customising the CMS /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Firing a FormAction within CMSFields


Go to End


3 Posts   891 Views

Avatar
shanka

Community Member, 2 Posts

7 May 2015 at 7:59am

Hi,

Is it possible to fire a FormAction button from within CMSFields? I'm currently using the following code but the button click doesn't want to fire the form action. Does this need to be called through a custom entwine function or is there something already built in that I'm missing?

class NotesPage extends Page {
	function getCMSFields() {
		$fields = parent::getCMSFields();

		// Page Sections
		$fields->addFieldsToTab('Root.Notes', array(
			// Send for approval
			LiteralField::create('', '<h3>Notify Admin Of Content To Approve</h3>'),
			FormAction::create('NotifyAdmin', 'Notify Admin'),
		));

		return $fields;
	}
}

class NotesPageExtension extends LeftAndMainExtension {
    private static $allowed_actions = array(
		'NotifyAdmin'
    );

	// Email the admin about content to moderate
	public function NotifyAdmin($data, $form){
		$className = $this->owner->stat('tree_class');
        $SQL_id = Convert::raw2sql($data['ID']);

        $record = DataObject::get_by_id($className, $SQL_id);

        if(!$record || !$record->ID){
            throw new SS_HTTPResponse_Exception(
                "Bad record ID #" . (int)$data['ID'], 404);
        }

        // at this point you have a $record,
        // which is your page you can work with!

        // this generates a message that will show up in the CMS
        $this->owner->response->addHeader(
            'X-Status',
            rawurlencode('Notifying admin')
        );

        return $this->owner->getResponseNegotiator()->respond($this->owner->request);
	}
}

Cheers

Avatar
Pyromanik

Community Member, 419 Posts

11 May 2015 at 11:00pm

You might like to look into the workflow module.
It manages what it looks like you're trying to achieve.

Avatar
shanka

Community Member, 2 Posts

14 May 2015 at 2:28pm

It look's like there is actually an inlineformaction field available http://api.silverstripe.org/3.1/class-InlineFormAction.html

Thanks for your help.