Silverstripe - 在CMSFields中触发FormAction

时间:2015-05-06 23:48:12

标签: content-management-system silverstripe

是否可以从CMSFields中触发FormAction按钮?我目前正在使用以下代码,但按钮点击并不想触发表单操作。这是否需要通过自定义缠绕函数调用,还是已经内置了我遗失的内容?



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);
	}
}
&#13;
&#13;
&#13;

0 个答案:

没有答案