API Enable Title to be CMS driven

FIX issue with field scaffolding hiding the "Enabled" checkbox
FIX Safe escaping of CMSTitle / Description fields
This commit is contained in:
Damian Mooyman 2015-03-06 13:33:26 +13:00
parent 871ba84d97
commit b23606b498
4 changed files with 56 additions and 38 deletions

View File

@ -85,9 +85,11 @@ blank). Each widget should be in its own folder like widgets_widgetName/
After installing or creating a new widget, **make sure to run db/build?flush=1** at the end of the URL, *before* After installing or creating a new widget, **make sure to run db/build?flush=1** at the end of the URL, *before*
attempting to use it. attempting to use it.
The class should extend the Widget class, and must specify three static variables - $title, the title that will appear The class should extend the Widget class, and must specify three config variables:
in the rendered widget (eg Photos), $cmsTitle, a more descriptive title that will appear in the cms editor (eg Flickr
Photos), and $description, a short description that will appear in the cms editor (eg This widget shows photos from * `title`: The title that will appear in the rendered widget (eg Photos). This can be customised by the CMS admin
* `cmsTitle`: a more descriptive title that will appear in the cms editor (eg Flickr Photos)
* `description`: a short description that will appear in the cms editor (eg This widget shows photos from
Flickr). The class may also specify functions to be used in the template like a page type can. Flickr). The class may also specify functions to be used in the template like a page type can.
If a Widget has configurable options, then it can specify a number of database fields to store these options in via the If a Widget has configurable options, then it can specify a number of database fields to store these options in via the

View File

@ -16,15 +16,24 @@ class Widget extends DataObject {
* @var array * @var array
*/ */
private static $db = array( private static $db = array(
"Title" => "Varchar(255)",
"Sort" => "Int", "Sort" => "Int",
"Enabled" => "Boolean" "Enabled" => "Boolean",
); );
/** /**
* @var array * @var array
*/ */
private static $defaults = array( private static $defaults = array(
'Enabled' => true 'Enabled' => true,
);
/**
* @var array
*/
private static $casting = array(
'CMSTitle' => 'Text',
'Description' => 'Text',
); );
private static $only_available_in = array(); private static $only_available_in = array();
@ -36,21 +45,6 @@ class Widget extends DataObject {
"Parent" => "WidgetArea", "Parent" => "WidgetArea",
); );
/**
* @var array
*/
private static $has_many = array();
/**
* @var array
*/
private static $many_many = array();
/**
* @var array
*/
private static $belongs_many_many = array();
/** /**
* @var string * @var string
*/ */
@ -82,6 +76,11 @@ class Widget extends DataObject {
* @var WidgetController * @var WidgetController
*/ */
protected $controller; protected $controller;
public function populateDefaults() {
parent::populateDefaults();
$this->setField('Title', $this->getTitle());
}
/** /**
* Note: Overloaded in {@link WidgetController}. * Note: Overloaded in {@link WidgetController}.
@ -120,22 +119,49 @@ class Widget extends DataObject {
/** /**
* @return string * @return string
* @deprecated
*/ */
public function Title() { public function Title() {
return _t($this->class.'.TITLE', $this->config()->title); return $this->getTitle();
}
/**
* Get the frontend title for this widget
*
* @return string
*/
public function getTitle() {
return $this->getField('Title')
?: _t($this->class.'.TITLE', $this->config()->title);
}
/**
* @return string
* @deprecated
*/
public function CMSTitle() {
return $this->getCMSTitle();
} }
/** /**
* @return string * @return string
*/ */
public function CMSTitle() { public function getCMSTitle() {
return _t($this->class.'.CMSTITLE', $this->config()->cmsTitle); return _t($this->class.'.CMSTITLE', $this->config()->cmsTitle);
} }
/** /**
* @return string * @return string
* @deprecated
*/ */
public function Description() { public function Description() {
return $this->getDescription();
}
/**
* @return string
*/
public function getDescription() {
return _t($this->class.'.DESCRIPTION', $this->config()->description); return _t($this->class.'.DESCRIPTION', $this->config()->description);
} }
@ -159,10 +185,11 @@ class Widget extends DataObject {
* @return FieldList * @return FieldList
*/ */
public function getCMSFields() { public function getCMSFields() {
$fields = parent::getCMSFields(); $fields = new FieldList(
$fields->removeByName('ParentID'); new TextField('Title', $this->fieldLabel('Title'), null, 255),
$fields->removeByName('Sort'); new CheckboxField('Enabled', $this->fieldLabel('Enabled'))
);
$this->extend('updateCMSFields', $fields);
return $fields; return $fields;
} }

View File

@ -18,7 +18,7 @@
}, },
"extra": { "extra": {
"branch-alias": { "branch-alias": {
"dev-master": "1.0.x-dev" "dev-master": "1.1.x-dev"
} }
} }
} }

View File

@ -439,15 +439,4 @@ class WidgetAreaEditorTest_TestWidget extends Widget implements TestOnly {
private static $cmsTitle = "Test widget"; private static $cmsTitle = "Test widget";
private static $title = "Test widget"; private static $title = "Test widget";
private static $description = "Test widget"; private static $description = "Test widget";
private static $db = array(
'Title' => 'Varchar'
);
public function getCMSFields() {
$fields = new FieldList();
$fields->push(new TextField('Title'));
return $fields;
}
function Title() {
return $this->Title ? $this->Title : self::$title;
}
} }