Instantly share code, notes, and snippets.

Embed
What would you like to do?
SilverStripe Cheat Sheet

DataObject Snippet

<?php
/**
* DataObjectSnippet
*
* @author Anselm Christophersen <ac@anselm.dk>
* @date March 2016
*/
class DataObjectSnippet extends DataObject
{
private static $singular_name = '';
private static $plural_name = '';
private static $db = [
'Name' => 'Varchar(255)',
'Test' => 'Boolean',
'SortOrder' => 'Int',
'Type' => "Enum('Homework, Assignment')"
];
private static $has_one = [
'Thumbnail' => 'Image',
'Parent' => 'Page'
];
private static $has_many = [];
private static $many_many = [];
private static $belongs_many_many = [];
private static $many_many_extraFields = [];
private static $field_labels = [
'Name' => 'Name Description'
];
private static $summary_fields = [
'Name' => 'Name',
'Parent.Title',
'TinyThumbPhoto' => 'Thumbnail'
];
//CRUD settings
static $can_create = Boolean;
public function canCreate($member = null)
{
return false;
}
public function canView($member = null)
{
return false;
}
public function canEdit($member = null)
{
return false;
}
public function canDelete($member = null)
{
return false;
}
//defaults
private static $default_sort = 'Sort ASC, Name ASC';
private static $defaults = array();//use fieldName => Default Value
public function populateDefaults()
{
parent::populateDefaults();
}
public function TinyThumbPhoto()
{
return $this->Thumbnail()->CroppedImage(100,100);
}
/**
* Getter examples:
*/
public function getFieldAsValue($fieldName)
{
return $this->getField($fieldName);
}
public function getFieldAsDbObject($fieldName)
{
return $this->dbObject($fieldName);
}
public function getFieldAsObject($fieldName)
{
return $this->obj($fieldName);
}
public function getCustomField()
{
$myUsefullField = DBField::create($className = "Currency", $value = 1111.11);
$formattedField = $myUsefullField->Nice();
//
//conversions
$newValue = Convert::$functionName($oldValue);
//examples are: raw2att, raw2xml, raw2json, raw2sql, json2obj, json2array, linkIfMatch
}
/**
* Saving
*/
protected function onBeforeWrite()
{
parent::onBeforeWrite();
}
protected function onAfterWrite()
{
parent::onAfterWrite();
$fields = $this->getChangedFields();
if (isset ($fields['MyField']['before'])) {
die("My field used to be: ".$fields['MyField']['before']);
}
}
public function requireDefaultRecords()
{
parent::requireDefaultRecords();
}
public function populateDefaults()
{
parent::populateDefaults();
}
/**
* Relations
*/
//static $many_many = array("Categories" => "Category");
function addCategories($additionalCategories)
{
$existingCategories = $this->Categories();
// method 1: Add many by iteration
foreach($additionalCategories as $category) {
$existingCategories->add($category);
}
// method 2: Add many by ID-List
$existingCategories->addMany(array(1,2,45,745));
}
}

Page Snippet

Use this as a start for you Page type, and delete whatever you don't need

<?php
/**
* Page Snippet
* Use this as a start for you Page type, and delete whatever you don't need
*
* @author Anselm Christophersen <ac@anselm.dk>
* @date February 2016
*/
class PageSnippet extends SiteTree {
private static $singular_name = '';
private static $plural_name = '';
private static $description = '';
private static $icon = 'cms/images/treeicons/page-gold';
private static $allowed_children = ['array', 'of', 'pagetypes']; //can also be "none";
private static $can_be_root = true;
private static $hide_ancestor = "MyUselessPage";
private static $db = [
"SubTitle" => "Varchar(255)",
"Author" => "Varchar(255)",
"Date" => "Date",
"ImagePosition" => "Enum('Top, Left','Left')",
];
private static $has_one = [
'Image' => 'Page_Image',
'ThumbnailImage' => 'Page_Image',
];
private static $has_many = [];
private static $many_many = array();
private static $belongs_many_many = array();
/**
* CMS Fields
* NOTE: Some of this is still from ss2x - might need updates
*/
function getCMSFields() {
$fields = parent::getCMSFields();
//
//Content tab
$fields->addFieldToTab("Root.Main",
new TextField("SubTitle", "Subtitle"), "MenuTitle"
);
$fields->addFieldToTab("Root.Main",
new TextField("Author","Author(s)"), "Content"
);
$dateField = new DateField("Date","Date");
$dateField->setConfig("showcalendar", true);
$fields->addFieldToTab("Root.Main", $dateField, "Content");
//
//Metadata tab
//
//Image tab
$fields->addFieldToTab("Root.Image",
new ImageUploadField('Image')
);
$fields->addFieldToTab("Root.Image",
new ImageUploadField('ThumbnailImage')
);
$fields->addFieldToTab("Root.Image",
new DropdownField("ImagePosition", "Image Position",
singleton('Page')->dbObject('ImagePosition')->enumValues()
)
);
//
//Removing unneccesary settings
$fields->removeFieldFromTab("Root", "Gallery");
$fields->removeFieldFromTab("Root", "Sidebar");
return $fields;
}
function FormattedImage(){
$img = $this->Image();
$resized = $img->SetHeight(300);
return $resized;
}
}
class Page_Controller extends ContentController{
function init() {
parent::init();
}
function BodyCssClass(){
return $this->ClassName;
}
}
class Page_Image extends Image {
function generateFeaturelistImageLeft($gd){
return $gd->resizeByHeight(220);
}
function generateFeaturelistImageTop($gd){
return $gd->croppedResize(390,170);
}
function generateAppetizerImage($gd){
//$img = $gd->croppedResize(130,160);
//resizing to height = 200
$img = $gd->resizeByHeight(200);
$width = $img->getWidth();
//cropping from left corner, if too wide
if ($width > 160) {
$img = $img->crop(0, 5, 150, 200);
}
//additional cropping
$img = $img->croppedResize(120,160);
//greyscale
$img = $img->greyscale(50,50,50);
//return "nothing";
return $img;
}
function generateMiniature($gd){
return $gd->resizeRatio(110,110);
}
}

TaskSnippet

<?php
/**
* TaskSnippet
* Run like this:
* php public/framework/cli-script.php /dev/tasks/TaskSnippet
*
*/
class TaskSnippet extends BuildTask
{
public $description = 'This is a snippet';
/**
* @param SS_HTTPRequest $request
*/
public function run($request)
{
echo 'hey, world';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment