1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* A list contains nested {@link BaseElement} such as a list of related files. |
5
|
|
|
* |
6
|
|
|
* @package elemental |
7
|
|
|
*/ |
8
|
|
|
class ElementList extends BaseElement |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
private static $db = array( |
|
|
|
|
12
|
|
|
'ListDescription' => 'HTMLText' |
13
|
|
|
); |
14
|
|
|
|
15
|
|
|
private static $has_many = array( |
|
|
|
|
16
|
|
|
'Elements' => 'BaseElement' |
17
|
|
|
); |
18
|
|
|
|
19
|
|
|
private static $duplicate_relations = array( |
|
|
|
|
20
|
|
|
'Elements' |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
private static $extensions = array( |
|
|
|
|
24
|
|
|
'ElementPublishChildren' |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
private static $title = "Element List Element"; |
|
|
|
|
28
|
|
|
|
29
|
|
|
private static $description = "Orderable list of elements"; |
|
|
|
|
30
|
|
|
|
31
|
|
|
private static $enable_title_in_template = true; |
|
|
|
|
32
|
|
|
|
33
|
|
|
public function getCMSFields() |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
$elements = $this->Elements(); |
|
|
|
|
36
|
|
|
$isInDb = $this->isInDB(); |
37
|
|
|
$allowed = $this->config()->get('allowed_elements'); |
38
|
|
|
$disallowed = (array) $this->config()->get('disallowed_elements'); |
39
|
|
|
|
40
|
|
|
$this->beforeUpdateCMSFields(function ($fields) use ($elements, $isInDb, $allowed, $disallowed) { |
41
|
|
|
$fields->removeByName('Root.Elements'); |
42
|
|
|
$fields->removeByName('Elements'); |
43
|
|
|
|
44
|
|
|
$desc = HTMLEditorField::create('ListDescription', 'List Description'); |
45
|
|
|
$desc->setRightTitle('Optional'); |
46
|
|
|
$fields->addFieldToTab('Root.Main', $desc); |
47
|
|
|
|
48
|
|
|
if ($isInDb) { |
49
|
|
|
$adder = new GridFieldAddNewMultiClass(); |
50
|
|
|
|
51
|
|
|
if (is_array($allowed)) { |
52
|
|
|
$list = $allowed; |
53
|
|
|
} else { |
54
|
|
|
$classes = ClassInfo::subclassesFor('BaseElement'); |
55
|
|
|
$list = array(); |
56
|
|
|
unset($classes['BaseElement']); |
57
|
|
|
|
58
|
|
|
foreach ($classes as $class) { |
|
|
|
|
59
|
|
|
if (in_array($class, $disallowed)) { |
60
|
|
|
continue; |
61
|
|
|
} |
62
|
|
|
$list[$class] = singleton($class)->i18n_singular_name(); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
asort($list); |
67
|
|
|
|
68
|
|
|
$adder->setClasses($list); |
69
|
|
|
|
70
|
|
|
$config = GridFieldConfig_RecordEditor::create(100); |
71
|
|
|
$config->addComponent(new GridFieldSortableRows('Sort')); |
72
|
|
|
$config->removeComponentsByType('GridFieldAddNewButton'); |
73
|
|
|
$config->addComponent($adder); |
74
|
|
|
|
75
|
|
|
$config->removeComponentsByType('GridFieldDetailForm'); |
76
|
|
|
$config->addComponent(new VersionedDataObjectDetailsForm()); |
77
|
|
|
|
78
|
|
|
$widgetArea = new GridField( |
79
|
|
|
'Elements', |
80
|
|
|
Config::inst()->get("ElementPageExtension", 'elements_title'), |
81
|
|
|
$elements, |
82
|
|
|
$config |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
$fields->addFieldToTab('Root.Main', $widgetArea); |
86
|
|
|
} else { |
87
|
|
|
$fields->addFieldToTab('Root.Main', LiteralField::create('warn', '<p class="message notice">Once you save this object you will be able to add items</p>')); |
88
|
|
|
} |
89
|
|
|
}); |
90
|
|
|
|
91
|
|
|
return parent::getCMSFields(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Used in template instead of {@link Widgets()} to wrap each widget in its |
96
|
|
|
* controller, making it easier to access and process form logic and |
97
|
|
|
* actions stored in {@link WidgetController}. |
98
|
|
|
* |
99
|
|
|
* @return SS_List - Collection of {@link WidgetController} instances. |
100
|
|
|
*/ |
101
|
|
|
public function WidgetControllers() { |
102
|
|
|
$controllers = new ArrayList(); |
103
|
|
|
|
104
|
|
|
foreach($this->Elements()->filter('Enabled', 1) as $widget) { |
|
|
|
|
105
|
|
|
$controller = $widget->getController(); |
106
|
|
|
|
107
|
|
|
$controller->init(); |
108
|
|
|
$controllers->push($controller); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return $controllers; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.