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 = [ |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
23 | 'InheritSideBar' => 'Boolean', |
||
24 | ]; |
||
25 | |||
26 | private static $defaults = [ |
||
0 ignored issues
–
show
|
|||
27 | 'InheritSideBar' => true |
||
28 | ]; |
||
29 | |||
30 | private static $has_one = [ |
||
0 ignored issues
–
show
|
|||
31 | 'SideBar' => WidgetArea::class, |
||
32 | ]; |
||
33 | |||
34 | private static $owns = [ |
||
0 ignored issues
–
show
|
|||
35 | 'SideBar', |
||
36 | ]; |
||
37 | |||
38 | private static $cascade_deletes = [ |
||
0 ignored issues
–
show
|
|||
39 | 'SideBar', |
||
40 | ]; |
||
41 | |||
42 | public function updateCMSFields(FieldList $fields) |
||
43 | { |
||
44 | $fields->addFieldToTab( |
||
45 | "Root.Widgets", |
||
46 | new CheckboxField("InheritSideBar", _t(__CLASS__ . '.INHERITSIDEBAR', 'Inherit Sidebar From Parent')) |
||
47 | ); |
||
48 | $fields->addFieldToTab( |
||
49 | "Root.Widgets", |
||
50 | new WidgetAreaEditor("SideBar") |
||
51 | ); |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @return WidgetArea |
||
56 | */ |
||
57 | public function SideBarView() |
||
58 | { |
||
59 | if ($this->owner->InheritSideBar |
||
60 | && ($parent = $this->owner->getParent()) |
||
61 | && $parent->hasMethod('SideBarView') |
||
62 | ) { |
||
63 | return $parent->SideBarView(); |
||
64 | } elseif ($this->owner->SideBar()->exists()) { |
||
65 | return $this->owner->SideBar(); |
||
66 | } |
||
67 | } |
||
68 | |||
69 | public function onBeforeDuplicate($duplicatePage) |
||
70 | { |
||
71 | if ($this->owner->hasField('SideBarID')) { |
||
72 | $sideBar = $this->owner->getComponent('SideBar'); |
||
73 | $duplicateWidgetArea = $sideBar->duplicate(); |
||
74 | |||
75 | foreach ($sideBar->Items() as $originalWidget) { |
||
76 | $widget = $originalWidget->duplicate(false); |
||
77 | $widget->ParentID = $duplicateWidgetArea->ID; |
||
78 | $widget->write(); |
||
79 | } |
||
80 | |||
81 | $duplicatePage->SideBarID = $duplicateWidgetArea->ID; |
||
82 | } |
||
83 | |||
84 | return $duplicatePage; |
||
85 | } |
||
86 | } |
||
87 |