Resources: SilverStripe
Adding a CMS tooltip
24 November 2014
Don't worry about writing bloated documentation - just write the tips where they're useful! Tooltips on tricky CMS fields make life a great deal easier, and have the bonus of acting as developer documentation as well.
TextField::create('MyText', 'My Text Label')
->setDescription('More <strong>detailed</strong> help')
->addExtraClass('cms-description-tooltip');
Add field to a many_many relationship
19 November 2014
Sorting dataobjects in a many_many requires the sort to be on the relationship, rather than the parent or child dataobject. You can add any fields to this relationship database with $many_many_extraFields.
MyClass.php
static $many_many_extraFields = array( 'RelationshipFieldName' => array( 'FieldName' => 'FieldType' ) );
Custom HTML in GridField
11 August 2014
GridFields are great, but sometimes you need to jazz them up with your own HTML or images to make it more user-friendly. Especially when handling huge tables!
You can create custom HTML using the following method in your DataObject:
static $summary_fields = array(
'ColumnDataHTML' => 'Column Heading'
);
function ColumnDataHTML(){
$html = HTMLText::create();
$html->setValue('<div style="background-color: '.$this->Colour.'; width: 100px; height: 100px;"> </div>');
return $html;
}
Access Controller function from Model
14 July 2014
Got functionality that lives in the controller that you need to access from an object's Model? Let's say your function is called MyFunction, this is how it would play out:
MyObjectClassName.php:
// return the function of the same name, from the controller
public function MyFunction(){
$class = $this->ClassName . "_Controller";
$controller = new $class($this);
return $controller->MyFunction();
}
Remove Add button from GridField
19 February 2014
Sometimes you need to be able to edit and delete GridField records, but you don't want the big green "Add New Object" button at the top. Here's how you remove it:
$config = GridFieldConfig_RecordEditor::create();
$config->removeComponentsByType($config->getComponentByType('GridFieldAddNewButton'));
Ignoring system timezone
9 October 2013
I've been banging my head against the wall figuring out why my time-sensitive website isn't displaying the date and time correctly. I had ensured php.ini represented the correct timezone, but no luck. It turns out SilverStripe can manage it's own timezone setting (it seems to pick and choose when to overwrite the system). Crack open your _config.php file and add the following:
_config.php
date_default_timezone_set('Pacific/Auckland');
Quickly Compile a SilverStripe website
30 August 2013
When you're frequently building SilverStripe websites, you manage to create small efficiencies to make life a bit easier. To construct a SilverStripe website quickly, I have written a quick bash script that compiles the basic SilverStripe repositories into a install-ready git repository.
Specify an UploadField's default folder
7 May 2013
Instead of scrambling around the /assets folder trying to find the folder that contains your assets, set the default folder to start the search from with one sneaky line in your class:
// create field
$thumbnailField = new UploadField( 'Picture', 'My Picture' );
// set /assets/MyPictures as default folder
$thumbnailField->setFolderName('MyPictures');
// add field to $fields object
$fields->addFieldToTab("Root.Main", $thumbnailField );
Run function within article content
6 May 2013
Sometimes you need to execute a function within a page article, and the easiest way is to write your functionality in your Controller (ie Page.php, Page_Controller class) and drop in a unique string within the article. In the demo below I have created a unique string '$DemoFunction' to write in my article content, which will be replaced by the content of the function DemoFunction();
In the Page_Controller class (mysite/code/Page.php)
function Content() {
// get current content
$newContent = $this->Content;
// do replacements
if( substr_count($newContent, '$DemoFunction') > 0 )
$newContent = str_replace('$DemoFunction', $this->DemoFunction(), $newContent);
return $newContent;
}
Reset Admin Password
2 October 2012
Locked yourself out of SilverStripe? Naturally, as the legitimate administrator you will have FTP access, so simply add this line to your /mysite/_config.php file to reset your 'admin' user password to 'password'.
/mysite/_config.php
Security::setDefaultAdmin('admin','password');