forked from jaedb/ModuleManager
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModuleSiteTreeExtension.php
More file actions
executable file
·131 lines (100 loc) · 3.94 KB
/
ModuleSiteTreeExtension.php
File metadata and controls
executable file
·131 lines (100 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace PlasticStudio\ModuleManager;
use SilverStripe\ORM\ArrayList;
use Silverstripe\Core\Extension;
use SilverStripe\Core\Config\Config;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\CheckboxField;
use SilverStripe\Forms\GridField\GridField;
use SilverStripe\Forms\GridField\GridFieldConfig_RelationEditor;
use SilverStripe\Forms\GridField\GridFieldAddNewButton;
use SilverStripe\Forms\GridField\GridFieldPageCount;
use Symbiote\GridFieldExtensions\GridFieldAddNewMultiClass;
use Symbiote\GridFieldExtensions\GridFieldOrderableRows;
class ModuleSiteTreeExtension extends Extension {
private static $db = [
'InheritModules' => 'Boolean'
];
private static $defaults = [
'InheritModules' => 0
];
private static $many_many = [
'Modules' => Module::class
];
private static $many_many_extraFields = [
'Modules' => [
'SortOrder' => 'Int'
]
];
/**
* Build CMS fields for all pages
* @param $fields = FieldList of standard fields
* @return FieldList obj
**/
public function updateCMSFields(FieldList $fields){
if ($this->owner->InheritModules){
$tab_name = "Root.Modules";
} else {
$tab_name = "Root.Modules (".$this->owner->Modules()->count().")";
}
$fields->removeByName('Modules');
$fields->addFieldToTab($tab_name, $inherit_field = CheckboxField::create('InheritModules','Inherit modules')->setDescription('Inherit <strong>all</strong> modules from the parent page. If the parent page is also set to inherit, then we go further up the hierarchy.'));
// When we inherit, the page's modules become irrelevant
if (!$this->owner->InheritModules){
$gridfield_config = GridFieldConfig_RelationEditor::create();
$gridfield = GridField::create("Modules", "Modules", $this->owner->Modules(), $gridfield_config);
$gridfield->addExtraClass('modulemanager-modules-field');
$gridfield_config->addComponent(new GridFieldOrderableRows('SortOrder'));
$gridfield_config->addComponent(new GridFieldAddNewMultiClass());
$gridfield_config->removeComponentsByType(GridFieldAddNewButton::class);
$gridfield_config->removeComponentsByType(GridFieldPageCount::class);
$fields->addFieldToTab($tab_name, $gridfield);
}
return $fields;
}
/**
* Get all the modules attached to this page (across all ModuleAreas)
* If set to inherit, we merge the parent's modules with our own
* @return ArrayList
**/
public function PageModules(){
$page = $this->owner;
// check for inheritance by recursively searching
while ($page->InheritModules && $page->ParentID > 0){
$page = $page->parent();
}
// We're at the top, but we're still set to inherit, so let's inherit an array of nothing
if ($page->InheritModules){
return ArrayList::create();
}
return $page->Modules()->sort('SortOrder ASC');
}
/**
* Get all modules for a specific position
* @param $alias = string (the alias of the ModulePosition)
* @param $limit = int (limit the number of modules to show, optional)
* @return HTMLText
**/
public function ModulePosition($alias, $limit = false){
$positions = Config::inst()->get(ModuleManager::class, 'positions');
if (!$positions || !isset($positions[$alias])){
user_error("Module position \"".$alias."\" doesn't exist. Have setup your custom positions in your config.yml?",E_USER_NOTICE);
}
// get this page's module list for specified position
$modules = $this->PageModules()->Filter(['Position' => $alias]);
// if we have no modules, then nothing doing
if (count($modules) <= 0){
return false;
}
// allow limiting number of modules, per position
if ($limit){
$modules = $modules->limit($limit);
}
// store them in a template array (for template loop)
$items = array(
'Position' => $alias,
'Modules' => $modules
);
return $this->owner->customise($items)->renderWith('ModulePosition');
}
}