Skip to content

Instantly share code, notes, and snippets.

@mcccclean
Created March 20, 2017 05:33
Show Gist options
  • Save mcccclean/f7b5b6c1c9224726ad1b0bec246da960 to your computer and use it in GitHub Desktop.
Save mcccclean/f7b5b6c1c9224726ad1b0bec246da960 to your computer and use it in GitHub Desktop.
<?php
// if you add a new file...
// make sure the '<' is the FIRST THING IN THE FILE
// no whitespace!! nothing! '<' ONLY OR THE WHOLE DANG SITE BREAKS
// call your class something reasonable. use 'extends' to get all the properties
// of another type of page, or just Page if it's its own new thing
class MyNewPagetype extends Page {
// $db - for data that you can represent with a single field
public static $db = array(
'SimpleText' => 'Varchar(40)', // string, max 40 characters
'Byline' => 'Varchar(200)', // string, max 200 characters
'Wysiwyg' => 'HTMLText', // it's obvious!
'Counter' => 'Int', // a number
'FlashyModeEnabled' => 'Bool', // true/false
);
// $has_one - for links to other objects!
public static $has_one = array(
'HeroImage' => 'Image', // an image
'CallToActionPage' => 'Page', // a link to another page
'SignupPage' => 'SignupPage', // you can restrict by pagetype
);
// (note that it's fine to use the same name for field and type)
// in a template, these properties will all be available just by their name:
// $SimpleText, $Byline, $Wysiwyg, $HeroImage, etc
// or some more complex examples:
// <a href="$SignupPage.Link">$SignupPage.MenuTitle</a>
// <% if Byline %>
// <p class="byline">$Byline</p>
// <% end_if %>
// -- note that you don't need the $ inside an <% if %> !
// there's also has_many but it's probably a bit out of scope for this cheatsheet
public static $has_many = array();
// here's where we add the fields to the admin interface
public function getCmsFields() {
$f = parent::getCmsFields(); // get the field from the parent type
// add one field.
$f->addFieldToTab('Root.Main', TextField::create('SimpleText', 'Simple text'));
// 'Root.Main' is the tab to add to.
// TextField (and most fields) take 2 parameters:
// - the data field to hook into
// - the label to put on the field
// They will usually be pretty much the same.
// add multiple fields at once
$f->addFieldsToTab('Root.Main', array(
TextField::create('Counter', 'Counter'), // fine to use a text field for int
CheckboxField::create('FlashyModeEnabled', 'Flashy mode'),
HTMLEditorField::create('Wysiwyg', 'Edit your content here')
));
// you can add a third parameter to addField(s)ToTab that is the name of another field
// that already exists.
// addFieldToTab will now add the new field _before_ that field in the CMS.
// (otherwise it'll add it at the bottom of the tab)
$f->addFieldToTab('Root.Main', TextField::create('Byline', 'Byline'), 'SimpleText');
// upload a file (the UploadField can handle anything, but because it's defined as
// an image in $has_one, Silverstripe will automatically restrict uploads to images for us
$f->addFieldToTab(
'Root.Main',
UploadField::create('HeroImage', 'Hero image')
);
// something a bit more complex -- linking to another page in the site
$f->addFieldToTab(
'Root.Main',
TreeDropdownField::create( // use a TreeDropdownField to pick a page
'CallToActionPageID', // gotta add 'ID' to the field name for TreeDropdownFields! watch out!
'Call to action page', // just a label
'Page' // we can filter by page types or just put 'SiteTree' to allow anything
)
);
// we're done! pass the updated field to the cms
return $f;
}
// if you're adding/editing fields inside a DataExtension instead of a Page (for eg in a CustomSiteConfig)
// it's a tiny bit different. you still use $db and $has_one exactly the same, but you don't use getCmsFields.
// Instead...
public function updateCMSFields(FieldList $fields) {
// $fields = parent::updateCMSFields() <-- we don't need to do this!
$fields->addFieldToTab(...);
// return $fields <-- we don't need to do this either
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment