-
-
Save hailwood/4c6b1121688df19daeb7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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