Skip to content

Instantly share code, notes, and snippets.

@sunnysideup
Last active September 21, 2016 23:58
Show Gist options
  • Save sunnysideup/d1d9a24ba92f14078441daa510da4c37 to your computer and use it in GitHub Desktop.
Save sunnysideup/d1d9a24ba92f14078441daa510da4c37 to your computer and use it in GitHub Desktop.
Making Silverstripe Modules fit for purpose
<?php
//..
// below we are updating the private static `my_array_static` in
// the `MyClassName` class.
// Because it is an array, you can only add additional entries to the
// end of the array, if you are keen to add a new entry at the beginning
// or, for example, remove one entry - then this is the way to go.
$newArray = array(
'A',
'B',
'C'
);
Config::inst()->update(
'MyClassName',
'my_array_static',
$newArray
);
Object::useCustomClass('MyModuleClass', 'MyClass');
<?php
class ExampleBuildTask extends BuildTask
{
protected $title = 'Add more data to our example';
protected $description = 'Takes the Example Objects and populates the example fields';
protected $enabled = true;
public function run($request)
{
//.. take the Example Objects and populates the example fields here
DB::alteration_message('Done now');
}
}
<?php
class MyModuleClassExtension extends DataExtension
{
private static $db = array(
'MyAdditionalField' => 'HTMLText'
);
function updateCMSFields(FieldList $fields) {
$fields->removeFieldFromTab('Root.Main', 'Content');
}
}
<?php
class MyPageWhichExtendsModulePage extends ModulePage
{
//..
}
class MyPageWhichExtendsModulePage_Controller extends ModulePage_Controller
{
function init()
{
parent::init();
//block a CSS file
Requirements::block('module_name/css/FancyModuleCSS.css');
//replace a JS file
Requirements::block('module_name/javascript/FancyModuleJS.js');
Requirements::javascript('themes/themename_module_name/javascript/FancyModuleJS.js');
}
}
---
Name: module_name_fixes
After: 'framework/*','cms/*', ‘module-name/*’
---
#set a variable
MyClass:
my_private_static_var: 21
# use a custom class
Injector:
ModuleClass:
class: MyClass
ModuleClass:
extensions:
- MyModuleClassExtension
---
Only:
environment: 'dev'
---
SSViewer:
source_file_comments: true
<?php
class MyDataObject extends DataObject {
/**
* this method can be added to any
* page type and/or DataObject
* and it will run every time you do a dev/build
*/
function requireDefaultRecords()
{
parent::requireDefaultRecords();
DB::alteration_message('Setting first MyDataObject.Foo to Bar to make it shine', 'created');
$myObject = MyDataObject::get()->first();
$myObject->Foo = 'bar';
$myObject->write();
}
}
en:
LeftAndMain:
PERMALREADY: ‘Sorry, but the CMS is not for you.'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment