Skip to content

Instantly share code, notes, and snippets.

@hailwood
Created August 28, 2015 01:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hailwood/4c6b1121688df19daeb7 to your computer and use it in GitHub Desktop.
Save hailwood/4c6b1121688df19daeb7 to your computer and use it in GitHub Desktop.
<?php
/**
* Class HomePage
*
* @method ContentBlockSection TopContentBlockSection()
* @method ContentBlockSection MiddleContentBlockSection()
* @method ContentBlockSection BottomContentBlockSection()
*/
class HomePage extends Page {
private static $db = [
'HeaderText' => 'HTMLText'
];
private static $belongs_to = [
'TopContentBlockSection' => 'ContentBlockSection.Page',
'MiddleContentBlockSection' => 'ContentBlockSection.Page',
'BottomContentBlockSection' => 'ContentBlockSection.Page'
];
public function getCMSFields() {
$this->beforeUpdateCMSFields(function (FieldList $fields) {
$fields->addFieldToTab('Root.Main', HtmlEditorField::create('HeaderText')->setRows(5));
if ($this->Exists()) {
if (!$this->TopContentBlockSection()->exists()) {
$this->TopContentBlockSection()->Title = 'Top Content Blocks';
}
if (!$this->MiddleContentBlockSection()->exists()) {
$this->MiddleContentBlockSection()->Title = 'Middle Content Blocks';
}
if (!$this->BottomContentBlockSection()->exists()) {
$this->BottomContentBlockSection()->Title = 'Bottom Content Blocks';
}
$TopContentBlocksConfig = new GridFieldConfig_RelationEditor();
$TopContentBlocksConfig->addComponent(new GridFieldSortableRows('SortOrder'));
$fields->addFieldsToTab('Root.TopContentBlocks', [
TextField::create('TopContentBlockSectionTitle', 'Title', $this->TopContentBlockSection()->Title),
GridField::create("TopContentBlocksGridField", "Content Blocks", $this->TopContentBlockSection()->Blocks(), $TopContentBlocksConfig)
]);
$MiddleContentBlocksConfig = new GridFieldConfig_RelationEditor();
$MiddleContentBlocksConfig->addComponent(new GridFieldSortableRows('SortOrder'));
$fields->addFieldsToTab('Root.MiddleContentBlocks', [
TextField::create('MiddleContentBlockSectionTitle', 'Title', $this->MiddleContentBlockSection()->Title),
GridField::create("MiddleContentBlocksGridField", "Content Blocks", $this->MiddleContentBlockSection()->Blocks(), $MiddleContentBlocksConfig)
]);
$BottomContentBlocksConfig = new GridFieldConfig_RelationEditor();
$BottomContentBlocksConfig->addComponent(new GridFieldSortableRows('SortOrder'));
$fields->addFieldsToTab('Root.BottomContentBlocks', [
TextField::create('BottomContentBlockSectionTitle', 'Title', $this->BottomContentBlockSection()->Title),
GridField::create("BottomContentBlocksGridField", "Content Blocks", $this->BottomContentBlockSection()->Blocks(), $BottomContentBlocksConfig)
]);
}
});
$fields = parent::getCMSFields();
return $fields;
}
public function onBeforeWrite() {
parent::onBeforeWrite();
$topContentBlockSection = $this->TopContentBlockSection();
$middleContentBlockSection = $this->MiddleContentBlockSection();
$bottomContentBlockSection = $this->BottomContentBlockSection();
sd($this->TopContentBlockSectionTitle); // NULL :(
$topContentBlockSection->Title = $this->TopContentBlockSectionTitle;
$middleContentBlockSection->Title = $this->MiddleContentBlockSectionTitle;
$bottomContentBlockSection->Title = $this->BottomContentBlockSectionTitle;
$topContentBlockSection->write();
$middleContentBlockSection->write();
$bottomContentBlockSection->write();
}
}
class HomePage_Controller extends Page_Controller {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment