Total Complexity | 49 |
Total Lines | 401 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like GridFieldDetailForm often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use GridFieldDetailForm, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
35 | class GridFieldDetailForm implements GridField_URLHandler |
||
36 | { |
||
37 | |||
38 | use Extensible, Injectable, GridFieldStateAware; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $template = null; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $name; |
||
49 | |||
50 | /** |
||
51 | * @var bool |
||
52 | */ |
||
53 | protected $showPagination; |
||
54 | |||
55 | /** |
||
56 | * @var bool |
||
57 | */ |
||
58 | protected $showAdd; |
||
59 | |||
60 | /** |
||
61 | * @var Validator The form validator used for both add and edit fields. |
||
62 | */ |
||
63 | protected $validator; |
||
64 | |||
65 | /** |
||
66 | * @var FieldList Falls back to {@link DataObject->getCMSFields()} if not defined. |
||
67 | */ |
||
68 | protected $fields; |
||
69 | |||
70 | /** |
||
71 | * @var string |
||
72 | */ |
||
73 | protected $itemRequestClass; |
||
74 | |||
75 | /** |
||
76 | * If true, will redirect to missing records if they are found elsewhere |
||
77 | * @var bool |
||
78 | */ |
||
79 | protected $redirectMissingRecords = false; |
||
80 | |||
81 | /** |
||
82 | * @var callable With two parameters: $form and $component |
||
83 | */ |
||
84 | protected $itemEditFormCallback; |
||
85 | |||
86 | public function getURLHandlers($gridField) |
||
87 | { |
||
88 | return [ |
||
89 | 'item/$ID' => 'handleItem' |
||
90 | ]; |
||
91 | } |
||
92 | |||
93 | /** |
||
94 | * Create a popup component. The two arguments will specify how the popup form's HTML and |
||
95 | * behaviour is created. The given controller will be customised, putting the edit form into the |
||
96 | * template with the given name. |
||
97 | * |
||
98 | * The arguments are experimental API's to support partial content to be passed back to whatever |
||
99 | * controller who wants to display the getCMSFields |
||
100 | * |
||
101 | * @param string $name The name of the edit form to place into the pop-up form |
||
102 | * @param bool $showPagination Whether the `Previous` and `Next` buttons should display or not, leave as null to use default |
||
103 | * @param bool $showAdd Whether the `Add` button should display or not, leave as null to use default |
||
104 | */ |
||
105 | public function __construct($name = null, $showPagination = null, $showAdd = null) |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * |
||
114 | * @param GridField $gridField |
||
115 | * @param HTTPRequest $request |
||
116 | * @return HTTPResponse |
||
117 | */ |
||
118 | public function handleItem($gridField, $request) |
||
119 | { |
||
120 | // Our getController could either give us a true Controller, if this is the top-level GridField. |
||
121 | // It could also give us a RequestHandler in the form of GridFieldDetailForm_ItemRequest if this is a |
||
122 | // nested GridField. |
||
123 | $requestHandler = $gridField->getForm()->getController(); |
||
124 | $record = $this->getRecordFromRequest($gridField, $request); |
||
125 | if (!$record) { |
||
|
|||
126 | // Look for the record elsewhere in the CMS |
||
127 | $redirectDest = $this->getLostRecordRedirection($gridField, $request); |
||
128 | // Don't allow infinite redirections |
||
129 | if ($redirectDest) { |
||
130 | // Mark the remainder of the URL as parsed to trigger an immediate redirection |
||
131 | while (!$request->allParsed()) { |
||
132 | $request->shift(); |
||
133 | } |
||
134 | return (new HTTPResponse())->redirect($redirectDest); |
||
135 | } |
||
136 | |||
137 | return $requestHandler->httpError(404, 'That record was not found'); |
||
138 | } |
||
139 | $handler = $this->getItemRequestHandler($gridField, $record, $requestHandler); |
||
140 | $manager = $this->getStateManager(); |
||
141 | if ($gridStateStr = $manager->getStateFromRequest($gridField, $request)) { |
||
142 | $gridField->getState(false)->setValue($gridStateStr); |
||
143 | } |
||
144 | |||
145 | // if no validator has been set on the GridField then use the Validators from the record. |
||
146 | if (!$this->getValidator()) { |
||
147 | $this->setValidator($record->getCMSCompositeValidator()); |
||
148 | } |
||
149 | |||
150 | return $handler->handleRequest($request); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @param GridField $gridField |
||
155 | * @param HTTPRequest $request |
||
156 | * @return DataObject|null |
||
157 | */ |
||
158 | protected function getRecordFromRequest(GridField $gridField, HTTPRequest $request): ?DataObject |
||
170 | } |
||
171 | |||
172 | /** |
||
173 | * Try and find another URL at which the given record can be edited. |
||
174 | * If redirectMissingRecords is true and the record has a CMSEditLink method, that value will be returned. |
||
175 | * This only works when the list passed to the GridField is a {@link DataList}. |
||
176 | * |
||
177 | * @param $gridField The current GridField |
||
178 | * @param $id The ID of the DataObject to open |
||
179 | */ |
||
180 | public function getLostRecordRedirection(GridField $gridField, HTTPRequest $request, ?int $id = null): ?string |
||
181 | { |
||
182 | |||
183 | if (!$this->redirectMissingRecords) { |
||
184 | return null; |
||
185 | } |
||
186 | |||
187 | // If not supplied, look up the ID from the request |
||
188 | if ($id === null && is_numeric($request->param('ID'))) { |
||
189 | $id = (int)$request->param('ID'); |
||
190 | } |
||
191 | |||
192 | if (!$id) { |
||
193 | return null; |
||
194 | } |
||
195 | |||
196 | $list = $gridField->getList(); |
||
197 | if (!$list instanceof DataList) { |
||
198 | throw new \LogicException('List is not of type DataList, cannot determine redirection target'); |
||
199 | } |
||
200 | |||
201 | $existing = DataObject::get($list->dataClass())->byID($id); |
||
202 | if ($existing && $existing->hasMethod('CMSEditLink')) { |
||
203 | $link = $existing->CMSEditLink(); |
||
204 | } |
||
205 | |||
206 | if ($link && $link == $request->getURL()) { |
||
207 | throw new \LogicException(sprintf( |
||
208 | 'Infinite redirection to "%s" detected in GridFieldDetailForm->getLostRecordRedirection()', |
||
209 | $link |
||
210 | )); |
||
211 | } |
||
212 | |||
213 | return $link; |
||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Build a request handler for the given record |
||
218 | * |
||
219 | * @param GridField $gridField |
||
220 | * @param DataObject $record |
||
221 | * @param RequestHandler $requestHandler |
||
222 | * @return GridFieldDetailForm_ItemRequest |
||
223 | */ |
||
224 | protected function getItemRequestHandler($gridField, $record, $requestHandler) |
||
239 | } |
||
240 | |||
241 | /** |
||
242 | * @param string $template |
||
243 | * @return $this |
||
244 | */ |
||
245 | public function setTemplate($template) |
||
246 | { |
||
247 | $this->template = $template; |
||
248 | return $this; |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * @return String |
||
253 | */ |
||
254 | public function getTemplate() |
||
257 | } |
||
258 | |||
259 | /** |
||
260 | * @param string $name |
||
261 | * @return $this |
||
262 | */ |
||
263 | public function setName($name) |
||
264 | { |
||
265 | $this->name = $name; |
||
266 | return $this; |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * @return String |
||
271 | */ |
||
272 | public function getName() |
||
273 | { |
||
274 | return $this->name; |
||
275 | } |
||
276 | |||
277 | /** |
||
278 | * Enable redirection to missing records. |
||
279 | * |
||
280 | * If a GridField shows a filtered list, and the DataObject is not in the list but exists in the |
||
281 | * database, and the DataObject has a CMSEditLink method, then the system will redirect to the |
||
282 | * URL returned by that method. |
||
283 | */ |
||
284 | public function setRedirectMissingRecords(bool $redirectMissingRecords): self |
||
285 | { |
||
286 | $this->redirectMissingRecords = $redirectMissingRecords; |
||
287 | return $this; |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Return the status of redirection to missing records. |
||
292 | * @see setRedirectMissingRecordssetRedirectMissingRecords |
||
293 | */ |
||
294 | public function getRedirectMissingRecords(): bool |
||
295 | { |
||
296 | return $this->redirectMissingRecords; |
||
297 | } |
||
298 | |||
299 | /** |
||
300 | * @return bool |
||
301 | */ |
||
302 | protected function getDefaultShowPagination() |
||
303 | { |
||
304 | $formActionsConfig = GridFieldDetailForm_ItemRequest::config()->get('formActions'); |
||
305 | return isset($formActionsConfig['showPagination']) ? (bool) $formActionsConfig['showPagination'] : false; |
||
306 | } |
||
307 | |||
308 | /** |
||
309 | * @return bool |
||
310 | */ |
||
311 | public function getShowPagination() |
||
312 | { |
||
313 | if ($this->showPagination === null) { |
||
314 | return $this->getDefaultShowPagination(); |
||
315 | } |
||
316 | |||
317 | return (bool) $this->showPagination; |
||
318 | } |
||
319 | |||
320 | /** |
||
321 | * @param bool|null $showPagination |
||
322 | * @return GridFieldDetailForm |
||
323 | */ |
||
324 | public function setShowPagination($showPagination) |
||
328 | } |
||
329 | |||
330 | /** |
||
331 | * @return bool |
||
332 | */ |
||
333 | protected function getDefaultShowAdd() |
||
334 | { |
||
335 | $formActionsConfig = GridFieldDetailForm_ItemRequest::config()->get('formActions'); |
||
336 | return isset($formActionsConfig['showAdd']) ? (bool) $formActionsConfig['showAdd'] : false; |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * @return bool |
||
341 | */ |
||
342 | public function getShowAdd() |
||
343 | { |
||
344 | if ($this->showAdd === null) { |
||
345 | return $this->getDefaultShowAdd(); |
||
346 | } |
||
347 | |||
348 | return (bool) $this->showAdd; |
||
349 | } |
||
350 | |||
351 | /** |
||
352 | * @param bool|null $showAdd |
||
353 | * @return GridFieldDetailForm |
||
354 | */ |
||
355 | public function setShowAdd($showAdd) |
||
356 | { |
||
357 | $this->showAdd = $showAdd; |
||
358 | return $this; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * @param Validator $validator |
||
363 | * @return $this |
||
364 | */ |
||
365 | public function setValidator(Validator $validator) |
||
369 | } |
||
370 | |||
371 | /** |
||
372 | * @return Validator |
||
373 | */ |
||
374 | public function getValidator() |
||
375 | { |
||
376 | return $this->validator; |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * @param FieldList $fields |
||
381 | * @return $this |
||
382 | */ |
||
383 | public function setFields(FieldList $fields) |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * @return FieldList |
||
391 | */ |
||
392 | public function getFields() |
||
395 | } |
||
396 | |||
397 | /** |
||
398 | * @param string $class |
||
399 | * @return $this |
||
400 | */ |
||
401 | public function setItemRequestClass($class) |
||
402 | { |
||
403 | $this->itemRequestClass = $class; |
||
404 | return $this; |
||
405 | } |
||
406 | |||
407 | /** |
||
408 | * @return string name of {@see GridFieldDetailForm_ItemRequest} subclass |
||
409 | */ |
||
410 | public function getItemRequestClass() |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * @param Closure $cb Make changes on the edit form after constructing it. |
||
422 | * @return $this |
||
423 | */ |
||
424 | public function setItemEditFormCallback(Closure $cb) |
||
428 | } |
||
429 | |||
430 | /** |
||
431 | * @return Closure |
||
432 | */ |
||
433 | public function getItemEditFormCallback() |
||
436 | } |
||
437 | } |
||
438 |