A personal repository of random information in compensation for a fatigued biological computer
Breaded IT » Web Development » SilverStripe CMS pre V3.0 » Silverstripe 2.2.x Notes » Page.php
<?php
/**
* Defines the ArticlePage page type
*/
class xxxP extends Page {
static $db = array(
);
static $has_one = array(
);
}
class xxx_Controller extends Page_Controller {
}
?>
Control behaviour: They say:
static fields we can define to change the properties of a page type. The $allowed_children field is an array of page types that are allowed to be children of the page in the site tree.
Add additional fields to the static $db thingy. Note that names must be unique: The names chosen for the fields you add must not already be used across the entire cms!
Add fields to xxx.php to getinto the db (need to do the rebuild thingy)
static $db = array(
'Date' => 'Date',
'Author' => 'Text'
);
To be able to update these fields in the CMS editor we need to override getCMSFields() with our own in xxx.php:
function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Content.Main', new CalendarDateField('Date'), 'Content');
$fields->addFieldToTab('Root.Content.Main', new TextField('Author'), 'Content');
return $fields;
They say:
$fields = parent::getCMSFields();
Firstly, we get the fields from the parent class; we want to add fields, not replace them. The $fields variable returned is a FieldSet object.
$fields->addFieldToTab('Root.Content.Main', new CalendarDateField('Date'), 'Content');
$fields->addFieldToTab('Root.Content.Main', new TextField('Author'), 'Content');
We can then add our new fields with addFieldToTab. The first argument is the tab on which we want to add the field to: “Root.Content.Main” is the tab which the content editor is on. The second argument is the field to add - this is not a database field, but a FormField. We must create new form fields, passing them the name of the database field we wish to edit. See the FormField documentation for all the field types available. The last argument says that we wish to put our fields above the “Content” field, see the FieldSet documentation for more details.
return $fields;
Finally, we return the fields to the CMS. If we flush the cache (?flush=1), we will be able to edit the fields in the CMS.