silverstripe-widgets/code/extension/WidgetPageExtension.php
Mark-M d56c5398ac FIX #58 on page duplicate
Fixing issue #58 where WidgetAreas end up being linked to multiple pages at once
2013-07-08 13:42:54 +01:00

70 lines
1.7 KiB
PHP

<?php
/**
* Adds a single {@link WidgetArea} called "SideBar" to {@link Page} classes.
* Adjust your templates to render the resulting
* {@link WidgetArea} as required, through the $SideBarView placeholder.
*
* This extension is just an example on how to use the widgets functionality,
* feel free to create your own relationships, naming conventions, etc.
* without using this class.
*/
class WidgetPageExtension extends DataExtension {
private static $db = array(
'InheritSideBar' => 'Boolean',
);
private static $defaults = array(
'InheritSideBar' => true
);
private static $has_one = array(
'SideBar' => 'WidgetArea'
);
public function updateCMSFields(FieldList $fields) {
$fields->addFieldToTab(
"Root.Widgets",
new CheckboxField("InheritSideBar", 'Inherit Sidebar From Parent')
);
$fields->addFieldToTab(
"Root.Widgets",
new WidgetAreaEditor("SideBar")
);
}
/**
* @return WidgetArea
*/
public function SideBarView() {
if(
$this->owner->InheritSideBar
&& $this->owner->getParent()
&& $this->owner->getParent()->hasMethod('SideBar')
) {
return $this->owner->getParent()->SideBar();
} elseif($this->owner->SideBar()->exists()){
return $this->owner->SideBar();
}
}
public function onBeforeDuplicate($duplicatePage) {
if($this->owner->hasField('SideBarID')) {
$sideBar = $this->owner->getComponent('SideBar');
$duplicateWidgetArea = $sideBar->duplicate();
foreach($sideBar->Items() as $originalWidget) {
$widget = $originalWidget->duplicate(false);
$widget->ParentID = $duplicateWidgetArea->ID;
$widget->write();
}
$duplicatePage->SideBarID = $duplicateWidgetArea->ID;
}
return $duplicatePage;
}
}