SilverStripe

From Wikibooks, open books for an open world
Jump to: navigation, search


The intent of this book is to be a set of solutions for common problems in the SilverStripe CMS ('content management system').

About SilverStripe[edit]

SilverStripe is an MVC (model/view/controller) Framework and Content Management System based on PHP5 and MySQL5.

Building a module[edit]

Loading 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[edit]

Let'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[edit]

In 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[edit]

This contribution is courtesy of the following coders:

  • Guy Steuart
  • Leonardo Alberto Celis