|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Widgets\Extensions; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Forms\CheckboxField; |
|
6
|
|
|
use SilverStripe\Forms\FieldList; |
|
7
|
|
|
use SilverStripe\ORM\DataExtension; |
|
8
|
|
|
use SilverStripe\Widgets\Forms\WidgetAreaEditor; |
|
9
|
|
|
use SilverStripe\Widgets\Model\WidgetArea; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Adds a single {@link WidgetArea} called "SideBar" to {@link Page} classes. |
|
13
|
|
|
* Adjust your templates to render the resulting |
|
14
|
|
|
* {@link WidgetArea} as required, through the $SideBarView placeholder. |
|
15
|
|
|
* |
|
16
|
|
|
* This extension is just an example on how to use the widgets functionality, |
|
17
|
|
|
* feel free to create your own relationships, naming conventions, etc. |
|
18
|
|
|
* without using this class. |
|
19
|
|
|
*/ |
|
20
|
|
|
class WidgetPageExtension extends DataExtension |
|
21
|
|
|
{ |
|
22
|
|
|
private static $db = array( |
|
|
|
|
|
|
23
|
|
|
'InheritSideBar' => 'Boolean', |
|
24
|
|
|
); |
|
25
|
|
|
|
|
26
|
|
|
private static $defaults = array( |
|
|
|
|
|
|
27
|
|
|
'InheritSideBar' => true |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
private static $has_one = array( |
|
|
|
|
|
|
31
|
|
|
'SideBar' => WidgetArea::class |
|
32
|
|
|
); |
|
33
|
|
|
|
|
34
|
|
|
public function updateCMSFields(FieldList $fields) |
|
35
|
|
|
{ |
|
36
|
|
|
$fields->addFieldToTab( |
|
37
|
|
|
"Root.Widgets", |
|
38
|
|
|
new CheckboxField("InheritSideBar", 'Inherit Sidebar From Parent') |
|
39
|
|
|
); |
|
40
|
|
|
$fields->addFieldToTab( |
|
41
|
|
|
"Root.Widgets", |
|
42
|
|
|
new WidgetAreaEditor("SideBar") |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return WidgetArea |
|
48
|
|
|
*/ |
|
49
|
|
|
public function SideBarView() |
|
50
|
|
|
{ |
|
51
|
|
|
if ($this->owner->InheritSideBar |
|
52
|
|
|
&& ($parent = $this->owner->getParent()) |
|
53
|
|
|
&& $parent->hasMethod('SideBarView') |
|
54
|
|
|
) { |
|
55
|
|
|
return $parent->SideBarView(); |
|
56
|
|
|
} elseif ($this->owner->SideBar()->exists()) { |
|
57
|
|
|
return $this->owner->SideBar(); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
public function onBeforeDuplicate($duplicatePage) |
|
62
|
|
|
{ |
|
63
|
|
|
if ($this->owner->hasField('SideBarID')) { |
|
64
|
|
|
$sideBar = $this->owner->getComponent('SideBar'); |
|
65
|
|
|
$duplicateWidgetArea = $sideBar->duplicate(); |
|
66
|
|
|
|
|
67
|
|
|
foreach ($sideBar->Items() as $originalWidget) { |
|
68
|
|
|
$widget = $originalWidget->duplicate(false); |
|
69
|
|
|
$widget->ParentID = $duplicateWidgetArea->ID; |
|
70
|
|
|
$widget->write(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$duplicatePage->SideBarID = $duplicateWidgetArea->ID; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $duplicatePage; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Support Translatable so that we don't link WidgetAreas across translations |
|
81
|
|
|
*/ |
|
82
|
|
|
public function onTranslatableCreate() |
|
83
|
|
|
{ |
|
84
|
|
|
//reset the sidebar ID |
|
85
|
|
|
$this->owner->SideBarID = 0; |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.