1 | <?php |
||
8 | class ConfigurablePage extends Page |
||
9 | { |
||
10 | private static $many_many = [ |
||
11 | 'Fields' => 'Moo_EditableField', |
||
12 | ]; |
||
13 | private static $many_many_extraFields = [ |
||
14 | 'Fields' => [ |
||
15 | 'Value' => 'Text', |
||
16 | 'Sort' => 'Int', |
||
17 | 'Group' => 'Int', |
||
18 | ], |
||
19 | ]; |
||
20 | private static $has_one = [ |
||
21 | 'EditableFieldGroup' => 'Moo_EditableFieldGroup', |
||
22 | ]; |
||
23 | private static $singular_name = 'Configurable Page'; |
||
24 | private static $plural_name = 'Configurable Pages'; |
||
25 | private static $description = 'Create page with configurable fields'; |
||
26 | private static $icon = 'configurablepage/images/icon.png'; |
||
27 | |||
28 | /** |
||
29 | * An array of required field names. |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $requiredFields = []; |
||
34 | |||
35 | /** |
||
36 | * An instance of ManyManyList containing the current values from the configurable fields. |
||
37 | * |
||
38 | * @var ManyManyList |
||
39 | */ |
||
40 | protected $editableFields; |
||
41 | |||
42 | /** |
||
43 | * List of allowed child page types. |
||
44 | * |
||
45 | * @var array |
||
46 | */ |
||
47 | private static $allowed_children = ['ConfigurablePage', 'SiteTree']; |
||
48 | |||
49 | /** |
||
50 | * (non-PHPdoc). |
||
51 | * |
||
52 | * @see SiteTree::getCMSFields() |
||
53 | */ |
||
54 | public function getCMSFields() |
||
71 | |||
72 | /** |
||
73 | * Create tab to manage page fields. |
||
74 | * |
||
75 | * @param FieldList $fields |
||
76 | */ |
||
77 | protected function buildManageFieldsTab(ManyManyList $list, FieldList $fields) |
||
118 | |||
119 | /** |
||
120 | * Create tab to edit fields values. |
||
121 | * |
||
122 | * @param ManyManyList $list |
||
123 | * @param FieldList $fields |
||
124 | */ |
||
125 | protected function buildPageFieldsTab(ManyManyList $list, FieldList $fields) |
||
136 | |||
137 | /** |
||
138 | * Add an editable field to the fields tab. |
||
139 | * |
||
140 | * @param Moo_EditableField $editableField |
||
141 | * |
||
142 | * @return bool|FormField |
||
143 | */ |
||
144 | protected function getFieldFromEditableField(Moo_EditableField $editableField) |
||
175 | |||
176 | /** |
||
177 | * Set required fields. |
||
178 | * |
||
179 | * @return RequiredFields |
||
180 | */ |
||
181 | public function getCMSValidator() |
||
185 | |||
186 | public function onAfterWrite() |
||
187 | { |
||
188 | parent::onAfterWrite(); |
||
189 | |||
190 | // Skip on publishing |
||
191 | if (Versioned::get_live_stage() == Versioned::current_stage()) { |
||
192 | return; |
||
193 | } |
||
194 | |||
195 | // Update the values of all fields added from editable field |
||
196 | if ($this->ID && $this->manyMany('Fields') && $pageFields = $this->getEditableFields()) { |
||
197 | $pageFields->each(function (Moo_EditableField $pageField) use ($pageFields) { |
||
198 | // Set submitted value into the field |
||
199 | $field = $pageField->getFormField(); |
||
200 | if (!$field) { |
||
201 | return; |
||
202 | } |
||
203 | $field->setValue($this->{$pageField->Name}); |
||
204 | |||
205 | // Extra fields to be saved |
||
206 | $value = $field->Value(); |
||
207 | $sort = $pageField->Sort; |
||
208 | $group = $pageField->Group; |
||
209 | |||
210 | // Clone the editable field object |
||
211 | // Remove the current saved one |
||
212 | $pageFields->remove($pageField); |
||
213 | // Add the clone with the new extra data |
||
214 | $pageFields->add($pageField, ['Value' => $value, 'Sort' => $sort, 'Group' => $group]); |
||
215 | }); |
||
216 | } |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * Format the page Content. |
||
221 | * |
||
222 | * @return string |
||
223 | */ |
||
224 | public function Content() |
||
250 | |||
251 | /** |
||
252 | * Get an array of all of the editable fields for the view template. |
||
253 | * |
||
254 | * @return ManyManyList |
||
255 | */ |
||
256 | public function getEditableFields() |
||
280 | } |
||
281 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.