Extending Basic ‘Page type’ in the Silvestripe CMS

Now I’m extending the HomePage.php Page type.

  1. We add db arrays:
    <?php
    class HomePage extends Page {
        static $db = array(
        );
        static $has_one = array(
        );
    }
    class HomePage_Controller extends Page_Controller {
    }
    ?>
  2. We can use the database arrays to add extra fields to the database. Let’s one image, One Description:
    <?php
    class HomePage extends Page {
        static $db = array(
    	'Description' => 'Text'
        );
        static $has_one = array(
    	'Photo' => 'Image'
        );
    }
    class HomePage_Controller extends Page_Controller {
    }
    ?>
  3. We now have the extra fields in the database, but still no way of changing them. To add these fields to the CMS we have to override the getCMSFields() method, which is called by the CMS when it creates the form to edit a page. Add the method to the HomePage class like so:
    <?php
    class HomePage extends Page {
        static $db = array(
    	'Description' => 'Text'
        );
        static $has_one = array(
    	'Photo' => 'Image'
        );
        function getCMSFields() {
            $fields = parent::getCMSFields();
            $fields->addFieldToTab('Root.Content.Main', new ImageField('Photo'), 'Content');
            $fields->addFieldToTab('Root.Content.Main', new TextField('Description'), 'Content');
            return $fields;
        }
    }
    class HomePage_Controller extends Page_Controller {
    }
    ?>
  4. After you have added the arrays and CMS fields for your Pagetype don’t forget to build the database for it using /dev/build/:
    yoursite.com/dev/build/?flush=1

Looking for quality SilverStripe Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , | Posted under SilverStripe CMS | RSS 2.0

Author Spotlight

Teet Bergmann

Teet Bergmann

I'm a freelance web designer from Estonia. Mostly doing sites using Silverstripe, magento, prestashop, wordpress.

Leave a Reply

Your email address will not be published. Required fields are marked *


1 + = 10

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>