Code samples

Let's assume you had a website with 'normal' pages and now wanted some other pages that displayed biographies for people working at a company. You can use SilverStripe Framework to extend the normal pages into staff pages, and add new fields, such as role, email address, photo, etc:

<?php
class StaffPage extends Page {
private static $db = array(
'Role' => 'Text'
);
private static $has_one = array(
'StaffPhoto' => 'Image'
);

public function getCMSFields() {
$fields = parent::getCMSFields();
$fields->addFieldToTab('Root.Staff', TextField::create("Role"));
$fields->addFieldToTab('Root.Staff', UploadField::create("StaffPhoto", "Staff photo"));
$fields->addFieldToTab('Root.Staff', TextField::create("Role")); $fields->addFieldToTab('Root.Staff', UploadField::create("StaffPhoto"));

return $fields;
}
}

The above will automatically do a number of things:

  • Add a new PHP class to your project, based on the regular Page class, to which you can attach custom business logic and functionality.
  • Add new columns to the database schema as necessary (after visiting the 'build' URL http://<yoursite>/dev/build/).
  • Lets you insert the template codes $Role and $StaffPhoto in your templates in to display the content.
  • Update the CMS user interface to display a text field and an image upload field on a new “Staff” tab.

To see more code, we suggest:

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.