SilverStripe
A Wikibookian believes this page should be split into smaller pages with a narrower subtopic. You can help by splitting this big page into smaller ones. Please make sure to follow the naming policy. Dividing books into smaller sections can provide more focus and allow each one to do one thing well, which benefits everyone. |
A reader requests expansion of this book to include more material. You can help by adding new material (learn how) or ask for assistance in the reading room. |
The intent of this book is to be a set of solutions for common problems in the SilverStripe CMS ('content management system').
About SilverStripe
editSilverStripe is an MVC (model/view/controller) Framework and Content Management System based on PHP5 and MySQL5.
Building a module
editLoading page specific JavaScript and CSS
edit// controller class Movies_Controller extends Page_Controller { // init function init() { Requirements::javascript('mysite/javascript/movies.js'); parent::init(); } }
CRUD (Create, Read, Update and Delete) in the CMS
editLet's say that we need to allow the Administrator to edit the list of coders in the Developers section.
First we need to create the Developers DataObject.
class Developers_DataObject extends DataObject { // fields static $db = array( 'Name' =>'Varchar(150)', 'Lastname' =>'Varchar(150)',); // edit form in the CMS function getCMSFields_forPopup() { $fields = new FieldSet(); $fields->push( new TextField( 'Name', 'Name:' ) ); $fields->push( new TextField( 'Lastname', 'Lastname:' ) ); return $fields; } }
In the same DataObject, we need to define the fields for the form.
Next, we need to create the Page type for the Developers section.
class Developers_Page extends Page { // make the relation between the section and the list of coders static $has_one = array( 'MyDeveloper' => 'Developers_DataObject' ); // manage the relation in the CMS function getCMSFields() { // don't overwrite the main fields $fields = parent::getCMSFields(); $tablefield = new HasOneComplexTableField( $this, 'MyDeveloper', 'Developers_DataObject', array( 'Name' => 'Name', 'Lastname' => 'Lastname',), 'getCMSFields_forPopup'); // add a new tab in the CMS $fields->addFieldToTab( 'Root.Content.Coders', $tablefield ); return $fields; } }
Our next step is to create a new page "Developers".
Now we need to go to "Behaviour" and change the page type to Developers.
This new page will have a tab called "Coders", where we can edit the developers list.
Relationing a DataObject with a Module
editIn this case we'll make a relation between Movies and Cities. Then we'll allow the administrator to add new cities in a page type 'Movies'.
<?php /* * Local Cities Data Object * */ class LocalCitiesDataObject extends DataObject { static $db = array( "Name" => "Varchar(150)" ); // relation static $has_one = array( 'LocalCities' => 'Movies' // 1-n relation ); // edit form for the CMS function getCMSFields_forPopup() { $fields = new FieldSet(); $fields->push( new TextField( 'Name' ) ); return $fields; } } /** * Defines the Movies page type */ class Movies extends Page { // relation static $has_many = array( 'LocalCities' => 'LocalCitiesDataObject' // 1-n relation ); // records list in the CMS function getCMSFields() { // don't overwrite the defaults fields for the Page $fields = parent::getCMSFields(); // for handling 1-n relation $tablefield = new HasManyComplexTableField( $this, 'LocalCities', // the name of the relationship 'LocalCitiesDataObject', // the related table array( "Name" => "Name" ), 'getCMSFields_forPopup' // the function to build the add/edit form ); $fields->addFieldToTab( 'Root.Content.Cities', $tablefield ); return $fields; } }
Credits
editThis contribution is courtesy of the following coders:
- Guy Steuart
- Leonardo Alberto Celis