Complex classes like AbstractAdmin 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 AbstractAdmin, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
61 | abstract class AbstractAdmin implements AdminInterface, DomainObjectInterface, AdminTreeInterface |
||
62 | { |
||
63 | public const CONTEXT_MENU = 'menu'; |
||
64 | public const CONTEXT_DASHBOARD = 'dashboard'; |
||
65 | |||
66 | public const CLASS_REGEX = |
||
67 | '@ |
||
68 | (?:([A-Za-z0-9]*)\\\)? # vendor name / app name |
||
69 | (Bundle\\\)? # optional bundle directory |
||
70 | ([A-Za-z0-9]+?)(?:Bundle)?\\\ # bundle name, with optional suffix |
||
71 | ( |
||
72 | Entity|Document|Model|PHPCR|CouchDocument|Phpcr| |
||
73 | Doctrine\\\Orm|Doctrine\\\Phpcr|Doctrine\\\MongoDB|Doctrine\\\CouchDB |
||
74 | )\\\(.*)@x'; |
||
75 | |||
76 | public const MOSAIC_ICON_CLASS = 'fa fa-th-large fa-fw'; |
||
77 | |||
78 | /** |
||
79 | * The list FieldDescription constructed from the configureListField method. |
||
80 | * |
||
81 | * @var FieldDescriptionInterface[] |
||
82 | */ |
||
83 | protected $listFieldDescriptions = []; |
||
84 | |||
85 | /** |
||
86 | * The show FieldDescription constructed from the configureShowFields method. |
||
87 | * |
||
88 | * @var FieldDescriptionInterface[] |
||
89 | */ |
||
90 | protected $showFieldDescriptions = []; |
||
91 | |||
92 | /** |
||
93 | * The list FieldDescription constructed from the configureFormField method. |
||
94 | * |
||
95 | * @var FieldDescriptionInterface[] |
||
96 | */ |
||
97 | protected $formFieldDescriptions = []; |
||
98 | |||
99 | /** |
||
100 | * The filter FieldDescription constructed from the configureFilterField method. |
||
101 | * |
||
102 | * @var FieldDescriptionInterface[] |
||
103 | */ |
||
104 | protected $filterFieldDescriptions = []; |
||
105 | |||
106 | /** |
||
107 | * NEXT_MAJOR: Remove this property. |
||
108 | * |
||
109 | * The number of result to display in the list. |
||
110 | * |
||
111 | * @deprecated since sonata-project/admin-bundle 3.67. |
||
112 | * |
||
113 | * @var int |
||
114 | */ |
||
115 | protected $maxPerPage = 32; |
||
116 | |||
117 | /** |
||
118 | * The maximum number of page numbers to display in the list. |
||
119 | * |
||
120 | * @var int |
||
121 | */ |
||
122 | protected $maxPageLinks = 25; |
||
123 | |||
124 | /** |
||
125 | * The base route name used to generate the routing information. |
||
126 | * |
||
127 | * @var string |
||
128 | */ |
||
129 | protected $baseRouteName; |
||
130 | |||
131 | /** |
||
132 | * The base route pattern used to generate the routing information. |
||
133 | * |
||
134 | * @var string |
||
135 | */ |
||
136 | protected $baseRoutePattern; |
||
137 | |||
138 | /** |
||
139 | * The base name controller used to generate the routing information. |
||
140 | * |
||
141 | * @var string |
||
142 | */ |
||
143 | protected $baseControllerName; |
||
144 | |||
145 | /** |
||
146 | * The label class name (used in the title/breadcrumb ...). |
||
147 | * |
||
148 | * @var string |
||
149 | */ |
||
150 | protected $classnameLabel; |
||
151 | |||
152 | /** |
||
153 | * The translation domain to be used to translate messages. |
||
154 | * |
||
155 | * @var string |
||
156 | */ |
||
157 | protected $translationDomain = 'messages'; |
||
158 | |||
159 | /** |
||
160 | * Options to set to the form (ie, validation_groups). |
||
161 | * |
||
162 | * @var array |
||
163 | */ |
||
164 | protected $formOptions = []; |
||
165 | |||
166 | /** |
||
167 | * NEXT_MAJOR: Remove this property. |
||
168 | * |
||
169 | * Default values to the datagrid. |
||
170 | * |
||
171 | * @deprecated since sonata-project/admin-bundle 3.67, use configureDefaultSortValues() instead. |
||
172 | * |
||
173 | * @var array |
||
174 | */ |
||
175 | protected $datagridValues = [ |
||
176 | '_page' => 1, |
||
177 | '_per_page' => 32, |
||
178 | ]; |
||
179 | |||
180 | /** |
||
181 | * NEXT_MAJOR: Remove this property. |
||
182 | * |
||
183 | * Predefined per page options. |
||
184 | * |
||
185 | * @deprecated since sonata-project/admin-bundle 3.67. |
||
186 | * |
||
187 | * @var array |
||
188 | */ |
||
189 | protected $perPageOptions = [16, 32, 64, 128, 256]; |
||
190 | |||
191 | /** |
||
192 | * Pager type. |
||
193 | * |
||
194 | * @var string |
||
195 | */ |
||
196 | protected $pagerType = Pager::TYPE_DEFAULT; |
||
197 | |||
198 | /** |
||
199 | * The code related to the admin. |
||
200 | * |
||
201 | * @var string |
||
202 | */ |
||
203 | protected $code; |
||
204 | |||
205 | /** |
||
206 | * The label. |
||
207 | * |
||
208 | * @var string |
||
209 | */ |
||
210 | protected $label; |
||
211 | |||
212 | /** |
||
213 | * Whether or not to persist the filters in the session. |
||
214 | * |
||
215 | * NEXT_MAJOR: remove this property |
||
216 | * |
||
217 | * @var bool |
||
218 | * |
||
219 | * @deprecated since sonata-project/admin-bundle 3.34, to be removed in 4.0. |
||
220 | */ |
||
221 | protected $persistFilters = false; |
||
222 | |||
223 | /** |
||
224 | * Array of routes related to this admin. |
||
225 | * |
||
226 | * @var RouteCollection |
||
227 | */ |
||
228 | protected $routes; |
||
229 | |||
230 | /** |
||
231 | * The subject only set in edit/update/create mode. |
||
232 | * |
||
233 | * @var object|null |
||
234 | */ |
||
235 | protected $subject; |
||
236 | |||
237 | /** |
||
238 | * Define a Collection of child admin, ie /admin/order/{id}/order-element/{childId}. |
||
239 | * |
||
240 | * @var array |
||
241 | */ |
||
242 | protected $children = []; |
||
243 | |||
244 | /** |
||
245 | * Reference the parent admin. |
||
246 | * |
||
247 | * @var AdminInterface|null |
||
248 | */ |
||
249 | protected $parent; |
||
250 | |||
251 | /** |
||
252 | * The base code route refer to the prefix used to generate the route name. |
||
253 | * |
||
254 | * NEXT_MAJOR: remove this attribute. |
||
255 | * |
||
256 | * @deprecated This attribute is deprecated since sonata-project/admin-bundle 3.24 and will be removed in 4.0 |
||
257 | * |
||
258 | * @var string |
||
259 | */ |
||
260 | protected $baseCodeRoute = ''; |
||
261 | |||
262 | /** |
||
263 | * NEXT_MAJOR: should be default array and private. |
||
264 | * |
||
265 | * @var string|array |
||
266 | */ |
||
267 | protected $parentAssociationMapping; |
||
268 | |||
269 | /** |
||
270 | * Reference the parent FieldDescription related to this admin |
||
271 | * only set for FieldDescription which is associated to an Sub Admin instance. |
||
272 | * |
||
273 | * @var FieldDescriptionInterface |
||
274 | */ |
||
275 | protected $parentFieldDescription; |
||
276 | |||
277 | /** |
||
278 | * If true then the current admin is part of the nested admin set (from the url). |
||
279 | * |
||
280 | * @var bool |
||
281 | */ |
||
282 | protected $currentChild = false; |
||
283 | |||
284 | /** |
||
285 | * The uniqid is used to avoid clashing with 2 admin related to the code |
||
286 | * ie: a Block linked to a Block. |
||
287 | * |
||
288 | * @var string |
||
289 | */ |
||
290 | protected $uniqid; |
||
291 | |||
292 | /** |
||
293 | * The Entity or Document manager. |
||
294 | * |
||
295 | * @var ModelManagerInterface |
||
296 | */ |
||
297 | protected $modelManager; |
||
298 | |||
299 | /** |
||
300 | * The current request object. |
||
301 | * |
||
302 | * @var Request|null |
||
303 | */ |
||
304 | protected $request; |
||
305 | |||
306 | /** |
||
307 | * The translator component. |
||
308 | * |
||
309 | * NEXT_MAJOR: remove this property |
||
310 | * |
||
311 | * @var \Symfony\Component\Translation\TranslatorInterface |
||
312 | * |
||
313 | * @deprecated since sonata-project/admin-bundle 3.9, to be removed with 4.0 |
||
314 | */ |
||
315 | protected $translator; |
||
316 | |||
317 | /** |
||
318 | * The related form contractor. |
||
319 | * |
||
320 | * @var FormContractorInterface |
||
321 | */ |
||
322 | protected $formContractor; |
||
323 | |||
324 | /** |
||
325 | * The related list builder. |
||
326 | * |
||
327 | * @var ListBuilderInterface |
||
328 | */ |
||
329 | protected $listBuilder; |
||
330 | |||
331 | /** |
||
332 | * The related view builder. |
||
333 | * |
||
334 | * @var ShowBuilderInterface |
||
335 | */ |
||
336 | protected $showBuilder; |
||
337 | |||
338 | /** |
||
339 | * The related datagrid builder. |
||
340 | * |
||
341 | * @var DatagridBuilderInterface |
||
342 | */ |
||
343 | protected $datagridBuilder; |
||
344 | |||
345 | /** |
||
346 | * @var RouteBuilderInterface |
||
347 | */ |
||
348 | protected $routeBuilder; |
||
349 | |||
350 | /** |
||
351 | * The datagrid instance. |
||
352 | * |
||
353 | * @var DatagridInterface|null |
||
354 | */ |
||
355 | protected $datagrid; |
||
356 | |||
357 | /** |
||
358 | * The router instance. |
||
359 | * |
||
360 | * @var RouteGeneratorInterface|null |
||
361 | */ |
||
362 | protected $routeGenerator; |
||
363 | |||
364 | /** |
||
365 | * The generated breadcrumbs. |
||
366 | * |
||
367 | * NEXT_MAJOR : remove this property |
||
368 | * |
||
369 | * @var array |
||
370 | */ |
||
371 | protected $breadcrumbs = []; |
||
372 | |||
373 | /** |
||
374 | * @var SecurityHandlerInterface |
||
375 | */ |
||
376 | protected $securityHandler; |
||
377 | |||
378 | /** |
||
379 | * @var ValidatorInterface |
||
380 | */ |
||
381 | protected $validator; |
||
382 | |||
383 | /** |
||
384 | * The configuration pool. |
||
385 | * |
||
386 | * @var Pool |
||
387 | */ |
||
388 | protected $configurationPool; |
||
389 | |||
390 | /** |
||
391 | * @var ItemInterface |
||
392 | */ |
||
393 | protected $menu; |
||
394 | |||
395 | /** |
||
396 | * @var FactoryInterface |
||
397 | */ |
||
398 | protected $menuFactory; |
||
399 | |||
400 | /** |
||
401 | * @var array<string, bool> |
||
402 | */ |
||
403 | protected $loaded = [ |
||
404 | 'view_fields' => false, // NEXT_MAJOR: Remove this unused value. |
||
405 | 'view_groups' => false, // NEXT_MAJOR: Remove this unused value. |
||
406 | 'routes' => false, |
||
407 | 'tab_menu' => false, |
||
408 | 'show' => false, |
||
409 | 'list' => false, |
||
410 | 'form' => false, |
||
411 | 'datagrid' => false, |
||
412 | ]; |
||
413 | |||
414 | /** |
||
415 | * @var string[] |
||
416 | */ |
||
417 | protected $formTheme = []; |
||
418 | |||
419 | /** |
||
420 | * @var string[] |
||
421 | */ |
||
422 | protected $filterTheme = []; |
||
423 | |||
424 | /** |
||
425 | * @var array<string, string> |
||
426 | * |
||
427 | * @deprecated since sonata-project/admin-bundle 3.34, will be dropped in 4.0. Use TemplateRegistry services instead |
||
428 | */ |
||
429 | protected $templates = []; |
||
430 | |||
431 | /** |
||
432 | * @var AdminExtensionInterface[] |
||
433 | */ |
||
434 | protected $extensions = []; |
||
435 | |||
436 | /** |
||
437 | * @var LabelTranslatorStrategyInterface |
||
438 | */ |
||
439 | protected $labelTranslatorStrategy; |
||
440 | |||
441 | /** |
||
442 | * Setting to true will enable preview mode for |
||
443 | * the entity and show a preview button in the |
||
444 | * edit/create forms. |
||
445 | * |
||
446 | * @var bool |
||
447 | */ |
||
448 | protected $supportsPreviewMode = false; |
||
449 | |||
450 | /** |
||
451 | * Roles and permissions per role. |
||
452 | * |
||
453 | * @var array 'role' => ['permission', 'permission'] |
||
454 | */ |
||
455 | protected $securityInformation = []; |
||
456 | |||
457 | protected $cacheIsGranted = []; |
||
458 | |||
459 | /** |
||
460 | * Action list for the search result. |
||
461 | * |
||
462 | * @var string[] |
||
463 | */ |
||
464 | protected $searchResultActions = ['edit', 'show']; |
||
465 | |||
466 | protected $listModes = [ |
||
467 | 'list' => [ |
||
468 | 'class' => 'fa fa-list fa-fw', |
||
469 | ], |
||
470 | 'mosaic' => [ |
||
471 | 'class' => self::MOSAIC_ICON_CLASS, |
||
472 | ], |
||
473 | ]; |
||
474 | |||
475 | /** |
||
476 | * The Access mapping. |
||
477 | * |
||
478 | * @var array [action1 => requiredRole1, action2 => [requiredRole2, requiredRole3]] |
||
479 | */ |
||
480 | protected $accessMapping = []; |
||
481 | |||
482 | /** |
||
483 | * @var MutableTemplateRegistryInterface |
||
484 | */ |
||
485 | private $templateRegistry; |
||
486 | |||
487 | /** |
||
488 | * The class name managed by the admin class. |
||
489 | * |
||
490 | * @var string |
||
491 | */ |
||
492 | private $class; |
||
493 | |||
494 | /** |
||
495 | * The subclasses supported by the admin class. |
||
496 | * |
||
497 | * @var array<string, string> |
||
498 | */ |
||
499 | private $subClasses = []; |
||
500 | |||
501 | /** |
||
502 | * The list collection. |
||
503 | * |
||
504 | * @var FieldDescriptionCollection|null |
||
505 | */ |
||
506 | private $list; |
||
507 | |||
508 | /** |
||
509 | * @var FieldDescriptionCollection|null |
||
510 | */ |
||
511 | private $show; |
||
512 | |||
513 | /** |
||
514 | * @var Form|null |
||
515 | */ |
||
516 | private $form; |
||
517 | |||
518 | /** |
||
519 | * The cached base route name. |
||
520 | * |
||
521 | * @var string |
||
522 | */ |
||
523 | private $cachedBaseRouteName; |
||
524 | |||
525 | /** |
||
526 | * The cached base route pattern. |
||
527 | * |
||
528 | * @var string |
||
529 | */ |
||
530 | private $cachedBaseRoutePattern; |
||
531 | |||
532 | /** |
||
533 | * The form group disposition. |
||
534 | * |
||
535 | * NEXT_MAJOR: must have `[]` as default value and remove the possibility to |
||
536 | * hold boolean values. |
||
537 | * |
||
538 | * @var array|bool |
||
539 | */ |
||
540 | private $formGroups = false; |
||
541 | |||
542 | /** |
||
543 | * The form tabs disposition. |
||
544 | * |
||
545 | * NEXT_MAJOR: must have `[]` as default value and remove the possibility to |
||
546 | * hold boolean values. |
||
547 | * |
||
548 | * @var array|bool |
||
549 | */ |
||
550 | private $formTabs = false; |
||
551 | |||
552 | /** |
||
553 | * The view group disposition. |
||
554 | * |
||
555 | * NEXT_MAJOR: must have `[]` as default value and remove the possibility to |
||
556 | * hold boolean values. |
||
557 | * |
||
558 | * @var array|bool |
||
559 | */ |
||
560 | private $showGroups = false; |
||
561 | |||
562 | /** |
||
563 | * The view tab disposition. |
||
564 | * |
||
565 | * NEXT_MAJOR: must have `[]` as default value and remove the possibility to |
||
566 | * hold boolean values. |
||
567 | * |
||
568 | * @var array|bool |
||
569 | */ |
||
570 | private $showTabs = false; |
||
571 | |||
572 | /** |
||
573 | * The manager type to use for the admin. |
||
574 | * |
||
575 | * @var string |
||
576 | */ |
||
577 | private $managerType; |
||
578 | |||
579 | /** |
||
580 | * The breadcrumbsBuilder component. |
||
581 | * |
||
582 | * @var BreadcrumbsBuilderInterface |
||
583 | */ |
||
584 | private $breadcrumbsBuilder; |
||
585 | |||
586 | /** |
||
587 | * Component responsible for persisting filters. |
||
588 | * |
||
589 | * @var FilterPersisterInterface|null |
||
590 | */ |
||
591 | private $filterPersister; |
||
592 | |||
593 | /** |
||
594 | * @param string $code |
||
595 | * @param string $class |
||
596 | * @param string|null $baseControllerName |
||
597 | */ |
||
598 | public function __construct($code, $class, $baseControllerName = null) |
||
599 | { |
||
600 | if (!\is_string($code)) { |
||
601 | @trigger_error(sprintf( |
||
|
|||
602 | 'Passing other type than string as argument 1 for method %s() is deprecated since' |
||
603 | .' sonata-project/admin-bundle 3.65. It will accept only string in version 4.0.', |
||
604 | __METHOD__ |
||
605 | ), E_USER_DEPRECATED); |
||
606 | } |
||
607 | $this->code = $code; |
||
608 | if (!\is_string($class)) { |
||
609 | @trigger_error(sprintf( |
||
610 | 'Passing other type than string as argument 2 for method %s() is deprecated since' |
||
611 | .' sonata-project/admin-bundle 3.65. It will accept only string in version 4.0.', |
||
612 | __METHOD__ |
||
613 | ), E_USER_DEPRECATED); |
||
614 | } |
||
615 | $this->class = $class; |
||
616 | if (null !== $baseControllerName && !\is_string($baseControllerName)) { |
||
617 | @trigger_error(sprintf( |
||
618 | 'Passing other type than string or null as argument 3 for method %s() is deprecated since' |
||
619 | .' sonata-project/admin-bundle 3.65. It will accept only string and null in version 4.0.', |
||
620 | __METHOD__ |
||
621 | ), E_USER_DEPRECATED); |
||
622 | } |
||
623 | $this->baseControllerName = $baseControllerName; |
||
624 | |||
625 | // NEXT_MAJOR: Remove this line. |
||
626 | $this->predefinePerPageOptions(); |
||
627 | |||
628 | // NEXT_MAJOR: Remove this line. |
||
629 | $this->datagridValues['_per_page'] = $this->maxPerPage; |
||
630 | } |
||
631 | |||
632 | /** |
||
633 | * {@inheritdoc} |
||
634 | */ |
||
635 | public function getExportFormats() |
||
636 | { |
||
637 | return [ |
||
638 | 'json', 'xml', 'csv', 'xls', |
||
639 | ]; |
||
640 | } |
||
641 | |||
642 | /** |
||
643 | * @return array |
||
644 | */ |
||
645 | public function getExportFields() |
||
646 | { |
||
647 | $fields = $this->getModelManager()->getExportFields($this->getClass()); |
||
648 | |||
649 | foreach ($this->getExtensions() as $extension) { |
||
650 | if (method_exists($extension, 'configureExportFields')) { |
||
651 | $fields = $extension->configureExportFields($this, $fields); |
||
652 | } |
||
653 | } |
||
654 | |||
655 | return $fields; |
||
656 | } |
||
657 | |||
658 | public function getDataSourceIterator() |
||
659 | { |
||
660 | $datagrid = $this->getDatagrid(); |
||
661 | $datagrid->buildPager(); |
||
662 | |||
663 | $fields = []; |
||
664 | |||
665 | foreach ($this->getExportFields() as $key => $field) { |
||
666 | $label = $this->getTranslationLabel($field, 'export', 'label'); |
||
667 | $transLabel = $this->trans($label); |
||
668 | |||
669 | // NEXT_MAJOR: Remove this hack, because all field labels will be translated with the major release |
||
670 | // No translation key exists |
||
671 | if ($transLabel === $label) { |
||
672 | $fields[$key] = $field; |
||
673 | } else { |
||
674 | $fields[$transLabel] = $field; |
||
675 | } |
||
676 | } |
||
677 | |||
678 | return $this->getModelManager()->getDataSourceIterator($datagrid, $fields); |
||
679 | } |
||
680 | |||
681 | public function validate(ErrorElement $errorElement, $object) |
||
682 | { |
||
683 | } |
||
684 | |||
685 | /** |
||
686 | * define custom variable. |
||
687 | */ |
||
688 | public function initialize() |
||
689 | { |
||
690 | if (!$this->classnameLabel) { |
||
691 | /* NEXT_MAJOR: remove cast to string, null is not supposed to be |
||
692 | supported but was documented as such */ |
||
693 | $this->classnameLabel = substr( |
||
694 | (string) $this->getClass(), |
||
695 | strrpos((string) $this->getClass(), '\\') + 1 |
||
696 | ); |
||
697 | } |
||
698 | |||
699 | // NEXT_MAJOR: Remove this line. |
||
700 | $this->baseCodeRoute = $this->getCode(); |
||
701 | |||
702 | $this->configure(); |
||
703 | } |
||
704 | |||
705 | public function configure() |
||
706 | { |
||
707 | } |
||
708 | |||
709 | public function update($object) |
||
710 | { |
||
711 | $this->preUpdate($object); |
||
712 | foreach ($this->extensions as $extension) { |
||
713 | $extension->preUpdate($this, $object); |
||
714 | } |
||
715 | |||
716 | $result = $this->getModelManager()->update($object); |
||
717 | // BC compatibility |
||
718 | if (null !== $result) { |
||
719 | $object = $result; |
||
720 | } |
||
721 | |||
722 | $this->postUpdate($object); |
||
723 | foreach ($this->extensions as $extension) { |
||
724 | $extension->postUpdate($this, $object); |
||
725 | } |
||
726 | |||
727 | return $object; |
||
728 | } |
||
729 | |||
730 | public function create($object) |
||
731 | { |
||
732 | $this->prePersist($object); |
||
733 | foreach ($this->extensions as $extension) { |
||
734 | $extension->prePersist($this, $object); |
||
735 | } |
||
736 | |||
737 | $result = $this->getModelManager()->create($object); |
||
738 | // BC compatibility |
||
739 | if (null !== $result) { |
||
740 | $object = $result; |
||
741 | } |
||
742 | |||
743 | $this->postPersist($object); |
||
744 | foreach ($this->extensions as $extension) { |
||
745 | $extension->postPersist($this, $object); |
||
746 | } |
||
747 | |||
748 | $this->createObjectSecurity($object); |
||
749 | |||
750 | return $object; |
||
751 | } |
||
752 | |||
753 | public function delete($object) |
||
754 | { |
||
755 | $this->preRemove($object); |
||
756 | foreach ($this->extensions as $extension) { |
||
757 | $extension->preRemove($this, $object); |
||
758 | } |
||
759 | |||
760 | $this->getSecurityHandler()->deleteObjectSecurity($this, $object); |
||
761 | $this->getModelManager()->delete($object); |
||
762 | |||
763 | $this->postRemove($object); |
||
764 | foreach ($this->extensions as $extension) { |
||
765 | $extension->postRemove($this, $object); |
||
766 | } |
||
767 | } |
||
768 | |||
769 | /** |
||
770 | * @param object $object |
||
771 | */ |
||
772 | public function preValidate($object) |
||
773 | { |
||
774 | } |
||
775 | |||
776 | public function preUpdate($object) |
||
777 | { |
||
778 | } |
||
779 | |||
780 | public function postUpdate($object) |
||
781 | { |
||
782 | } |
||
783 | |||
784 | public function prePersist($object) |
||
785 | { |
||
786 | } |
||
787 | |||
788 | public function postPersist($object) |
||
789 | { |
||
790 | } |
||
791 | |||
792 | public function preRemove($object) |
||
793 | { |
||
794 | } |
||
795 | |||
796 | public function postRemove($object) |
||
797 | { |
||
798 | } |
||
799 | |||
800 | public function preBatchAction($actionName, ProxyQueryInterface $query, array &$idx, $allElements) |
||
801 | { |
||
802 | } |
||
803 | |||
804 | public function getFilterParameters() |
||
805 | { |
||
806 | $parameters = []; |
||
807 | |||
808 | // build the values array |
||
809 | if ($this->hasRequest()) { |
||
810 | $filters = $this->request->query->get('filter', []); |
||
811 | if (isset($filters['_page'])) { |
||
812 | $filters['_page'] = (int) $filters['_page']; |
||
813 | } |
||
814 | if (isset($filters['_per_page'])) { |
||
815 | $filters['_per_page'] = (int) $filters['_per_page']; |
||
816 | } |
||
817 | |||
818 | // if filter persistence is configured |
||
819 | // NEXT_MAJOR: remove `$this->persistFilters !== false` from the condition |
||
820 | if (false !== $this->persistFilters && null !== $this->filterPersister) { |
||
821 | // if reset filters is asked, remove from storage |
||
822 | if ('reset' === $this->request->query->get('filters')) { |
||
823 | $this->filterPersister->reset($this->getCode()); |
||
824 | } |
||
825 | |||
826 | // if no filters, fetch from storage |
||
827 | // otherwise save to storage |
||
828 | if (empty($filters)) { |
||
829 | $filters = $this->filterPersister->get($this->getCode()); |
||
830 | } else { |
||
831 | $this->filterPersister->set($this->getCode(), $filters); |
||
832 | } |
||
833 | } |
||
834 | |||
835 | $parameters = array_merge( |
||
836 | $this->getModelManager()->getDefaultSortValues($this->getClass()), |
||
837 | $this->datagridValues, // NEXT_MAJOR: Remove this line. |
||
838 | $this->getDefaultSortValues(), |
||
839 | $this->getDefaultFilterValues(), |
||
840 | $filters |
||
841 | ); |
||
842 | |||
843 | if (!$this->determinedPerPageValue($parameters['_per_page'])) { |
||
844 | $parameters['_per_page'] = $this->getMaxPerPage(); |
||
845 | } |
||
846 | |||
847 | // always force the parent value |
||
848 | if ($this->isChild() && $this->getParentAssociationMapping()) { |
||
849 | $name = str_replace('.', '__', $this->getParentAssociationMapping()); |
||
850 | $parameters[$name] = ['value' => $this->request->get($this->getParent()->getIdParameter())]; |
||
851 | } |
||
852 | } |
||
853 | |||
854 | return $parameters; |
||
855 | } |
||
856 | |||
857 | /** |
||
858 | * NEXT_MAJOR: Change the visibility to protected (similar to buildShow, buildForm, ...). |
||
859 | */ |
||
860 | public function buildDatagrid() |
||
861 | { |
||
862 | if ($this->loaded['datagrid']) { |
||
863 | return; |
||
864 | } |
||
865 | |||
866 | $this->loaded['datagrid'] = true; |
||
867 | |||
868 | $filterParameters = $this->getFilterParameters(); |
||
869 | |||
870 | // transform _sort_by from a string to a FieldDescriptionInterface for the datagrid. |
||
871 | if (isset($filterParameters['_sort_by']) && \is_string($filterParameters['_sort_by'])) { |
||
872 | if ($this->hasListFieldDescription($filterParameters['_sort_by'])) { |
||
873 | $filterParameters['_sort_by'] = $this->getListFieldDescription($filterParameters['_sort_by']); |
||
874 | } else { |
||
875 | $filterParameters['_sort_by'] = $this->getModelManager()->getNewFieldDescriptionInstance( |
||
876 | $this->getClass(), |
||
877 | $filterParameters['_sort_by'], |
||
878 | [] |
||
879 | ); |
||
880 | |||
881 | $this->getListBuilder()->buildField(null, $filterParameters['_sort_by'], $this); |
||
882 | } |
||
883 | } |
||
884 | |||
885 | // initialize the datagrid |
||
886 | $this->datagrid = $this->getDatagridBuilder()->getBaseDatagrid($this, $filterParameters); |
||
887 | |||
888 | $this->datagrid->getPager()->setMaxPageLinks($this->maxPageLinks); |
||
889 | |||
890 | $mapper = new DatagridMapper($this->getDatagridBuilder(), $this->datagrid, $this); |
||
891 | |||
892 | // build the datagrid filter |
||
893 | $this->configureDatagridFilters($mapper); |
||
894 | |||
895 | // ok, try to limit to add parent filter |
||
896 | if ($this->isChild() && $this->getParentAssociationMapping() && !$mapper->has($this->getParentAssociationMapping())) { |
||
897 | $mapper->add($this->getParentAssociationMapping(), null, [ |
||
898 | 'show_filter' => false, |
||
899 | 'label' => false, |
||
900 | 'field_type' => ModelHiddenType::class, |
||
901 | 'field_options' => [ |
||
902 | 'model_manager' => $this->getModelManager(), |
||
903 | ], |
||
904 | 'operator_type' => HiddenType::class, |
||
905 | ], null, null, [ |
||
906 | 'admin_code' => $this->getParent()->getCode(), |
||
907 | ]); |
||
908 | } |
||
909 | |||
910 | foreach ($this->getExtensions() as $extension) { |
||
911 | $extension->configureDatagridFilters($mapper); |
||
912 | } |
||
913 | } |
||
914 | |||
915 | /** |
||
916 | * Returns the name of the parent related field, so the field can be use to set the default |
||
917 | * value (ie the parent object) or to filter the object. |
||
918 | * |
||
919 | * @throws \InvalidArgumentException |
||
920 | * |
||
921 | * @return string|null |
||
922 | */ |
||
923 | public function getParentAssociationMapping() |
||
924 | { |
||
925 | // NEXT_MAJOR: remove array check |
||
926 | if (\is_array($this->parentAssociationMapping) && $this->isChild()) { |
||
927 | $parent = $this->getParent()->getCode(); |
||
928 | |||
929 | if (\array_key_exists($parent, $this->parentAssociationMapping)) { |
||
930 | return $this->parentAssociationMapping[$parent]; |
||
931 | } |
||
932 | |||
933 | throw new \InvalidArgumentException(sprintf( |
||
934 | 'There\'s no association between %s and %s.', |
||
935 | $this->getCode(), |
||
936 | $this->getParent()->getCode() |
||
937 | )); |
||
938 | } |
||
939 | |||
940 | // NEXT_MAJOR: remove this line |
||
941 | return $this->parentAssociationMapping; |
||
942 | } |
||
943 | |||
944 | /** |
||
945 | * @param string $code |
||
946 | * @param string $value |
||
947 | */ |
||
948 | final public function addParentAssociationMapping($code, $value) |
||
949 | { |
||
950 | $this->parentAssociationMapping[$code] = $value; |
||
951 | } |
||
952 | |||
953 | /** |
||
954 | * Returns the baseRoutePattern used to generate the routing information. |
||
955 | * |
||
956 | * @throws \RuntimeException |
||
957 | * |
||
958 | * @return string the baseRoutePattern used to generate the routing information |
||
959 | */ |
||
960 | public function getBaseRoutePattern() |
||
961 | { |
||
962 | if (null !== $this->cachedBaseRoutePattern) { |
||
963 | return $this->cachedBaseRoutePattern; |
||
964 | } |
||
965 | |||
966 | if ($this->isChild()) { // the admin class is a child, prefix it with the parent route pattern |
||
967 | $baseRoutePattern = $this->baseRoutePattern; |
||
968 | if (!$this->baseRoutePattern) { |
||
969 | preg_match(self::CLASS_REGEX, $this->class, $matches); |
||
970 | |||
971 | if (!$matches) { |
||
972 | throw new \RuntimeException(sprintf( |
||
973 | 'Please define a default `baseRoutePattern` value for the admin class `%s`', |
||
974 | static::class |
||
975 | )); |
||
976 | } |
||
977 | $baseRoutePattern = $this->urlize($matches[5], '-'); |
||
978 | } |
||
979 | |||
980 | $this->cachedBaseRoutePattern = sprintf( |
||
981 | '%s/%s/%s', |
||
982 | $this->getParent()->getBaseRoutePattern(), |
||
983 | $this->getParent()->getRouterIdParameter(), |
||
984 | $baseRoutePattern |
||
985 | ); |
||
986 | } elseif ($this->baseRoutePattern) { |
||
987 | $this->cachedBaseRoutePattern = $this->baseRoutePattern; |
||
988 | } else { |
||
989 | preg_match(self::CLASS_REGEX, $this->class, $matches); |
||
990 | |||
991 | if (!$matches) { |
||
992 | throw new \RuntimeException(sprintf( |
||
993 | 'Please define a default `baseRoutePattern` value for the admin class `%s`', |
||
994 | static::class |
||
995 | )); |
||
996 | } |
||
997 | |||
998 | $this->cachedBaseRoutePattern = sprintf( |
||
999 | '/%s%s/%s', |
||
1000 | empty($matches[1]) ? '' : $this->urlize($matches[1], '-').'/', |
||
1001 | $this->urlize($matches[3], '-'), |
||
1002 | $this->urlize($matches[5], '-') |
||
1003 | ); |
||
1004 | } |
||
1005 | |||
1006 | return $this->cachedBaseRoutePattern; |
||
1007 | } |
||
1008 | |||
1009 | /** |
||
1010 | * Returns the baseRouteName used to generate the routing information. |
||
1011 | * |
||
1012 | * @throws \RuntimeException |
||
1013 | * |
||
1014 | * @return string the baseRouteName used to generate the routing information |
||
1015 | */ |
||
1016 | public function getBaseRouteName() |
||
1017 | { |
||
1018 | if (null !== $this->cachedBaseRouteName) { |
||
1019 | return $this->cachedBaseRouteName; |
||
1020 | } |
||
1021 | |||
1022 | if ($this->isChild()) { // the admin class is a child, prefix it with the parent route name |
||
1023 | $baseRouteName = $this->baseRouteName; |
||
1024 | if (!$this->baseRouteName) { |
||
1025 | preg_match(self::CLASS_REGEX, $this->class, $matches); |
||
1026 | |||
1027 | if (!$matches) { |
||
1028 | throw new \RuntimeException(sprintf( |
||
1029 | 'Cannot automatically determine base route name,' |
||
1030 | .' please define a default `baseRouteName` value for the admin class `%s`', |
||
1031 | static::class |
||
1032 | )); |
||
1033 | } |
||
1034 | $baseRouteName = $this->urlize($matches[5]); |
||
1035 | } |
||
1036 | |||
1037 | $this->cachedBaseRouteName = sprintf( |
||
1038 | '%s_%s', |
||
1039 | $this->getParent()->getBaseRouteName(), |
||
1040 | $baseRouteName |
||
1041 | ); |
||
1042 | } elseif ($this->baseRouteName) { |
||
1043 | $this->cachedBaseRouteName = $this->baseRouteName; |
||
1044 | } else { |
||
1045 | preg_match(self::CLASS_REGEX, $this->class, $matches); |
||
1046 | |||
1047 | if (!$matches) { |
||
1048 | throw new \RuntimeException(sprintf( |
||
1049 | 'Cannot automatically determine base route name,' |
||
1050 | .' please define a default `baseRouteName` value for the admin class `%s`', |
||
1051 | static::class |
||
1052 | )); |
||
1053 | } |
||
1054 | |||
1055 | $this->cachedBaseRouteName = sprintf( |
||
1056 | 'admin_%s%s_%s', |
||
1057 | empty($matches[1]) ? '' : $this->urlize($matches[1]).'_', |
||
1058 | $this->urlize($matches[3]), |
||
1059 | $this->urlize($matches[5]) |
||
1060 | ); |
||
1061 | } |
||
1062 | |||
1063 | return $this->cachedBaseRouteName; |
||
1064 | } |
||
1065 | |||
1066 | /** |
||
1067 | * urlize the given word. |
||
1068 | * |
||
1069 | * @param string $word |
||
1070 | * @param string $sep the separator |
||
1071 | * |
||
1072 | * @return string |
||
1073 | */ |
||
1074 | public function urlize($word, $sep = '_') |
||
1075 | { |
||
1076 | return strtolower(preg_replace('/[^a-z0-9_]/i', $sep.'$1', $word)); |
||
1077 | } |
||
1078 | |||
1079 | public function getClass() |
||
1080 | { |
||
1081 | if ($this->hasActiveSubClass()) { |
||
1082 | if ($this->hasParentFieldDescription()) { |
||
1083 | throw new \RuntimeException('Feature not implemented: an embedded admin cannot have subclass'); |
||
1084 | } |
||
1085 | |||
1086 | $subClass = $this->getRequest()->query->get('subclass'); |
||
1087 | |||
1088 | if (!$this->hasSubClass($subClass)) { |
||
1089 | throw new \RuntimeException(sprintf('Subclass "%s" is not defined.', $subClass)); |
||
1090 | } |
||
1091 | |||
1092 | return $this->getSubClass($subClass); |
||
1093 | } |
||
1094 | |||
1095 | // see https://github.com/sonata-project/SonataCoreBundle/commit/247eeb0a7ca7211142e101754769d70bc402a5b4 |
||
1096 | if ($this->subject && \is_object($this->subject)) { |
||
1097 | return ClassUtils::getClass($this->subject); |
||
1098 | } |
||
1099 | |||
1100 | return $this->class; |
||
1101 | } |
||
1102 | |||
1103 | public function getSubClasses() |
||
1104 | { |
||
1105 | return $this->subClasses; |
||
1106 | } |
||
1107 | |||
1108 | /** |
||
1109 | * NEXT_MAJOR: remove this method. |
||
1110 | */ |
||
1111 | public function addSubClass($subClass) |
||
1112 | { |
||
1113 | @trigger_error(sprintf( |
||
1114 | 'Method "%s" is deprecated since sonata-project/admin-bundle 3.30 and will be removed in 4.0.', |
||
1115 | __METHOD__ |
||
1116 | ), E_USER_DEPRECATED); |
||
1117 | |||
1118 | if (!\in_array($subClass, $this->subClasses, true)) { |
||
1119 | $this->subClasses[] = $subClass; |
||
1120 | } |
||
1121 | } |
||
1122 | |||
1123 | public function setSubClasses(array $subClasses) |
||
1124 | { |
||
1125 | $this->subClasses = $subClasses; |
||
1126 | } |
||
1127 | |||
1128 | public function hasSubClass($name) |
||
1129 | { |
||
1130 | return isset($this->subClasses[$name]); |
||
1131 | } |
||
1132 | |||
1133 | public function hasActiveSubClass() |
||
1134 | { |
||
1135 | if (\count($this->subClasses) > 0 && $this->request) { |
||
1136 | return null !== $this->getRequest()->query->get('subclass'); |
||
1137 | } |
||
1138 | |||
1139 | return false; |
||
1140 | } |
||
1141 | |||
1142 | public function getActiveSubClass() |
||
1143 | { |
||
1144 | if (!$this->hasActiveSubClass()) { |
||
1145 | @trigger_error(sprintf( |
||
1146 | 'Calling %s() when there is no active subclass is deprecated since sonata-project/admin-bundle 3.52' |
||
1147 | .' and will throw an exception in 4.0.' |
||
1148 | .' Use %s::hasActiveSubClass() to know if there is an active subclass.', |
||
1149 | __METHOD__, |
||
1150 | __CLASS__ |
||
1151 | ), E_USER_DEPRECATED); |
||
1152 | // NEXT_MAJOR : remove the previous `trigger_error()` call, the `return null` statement, uncomment the following exception and declare string as return type |
||
1153 | // throw new \LogicException(sprintf( |
||
1154 | // 'Admin "%s" has no active subclass.', |
||
1155 | // static::class |
||
1156 | // )); |
||
1157 | |||
1158 | return null; |
||
1159 | } |
||
1160 | |||
1161 | return $this->getSubClass($this->getActiveSubclassCode()); |
||
1162 | } |
||
1163 | |||
1164 | public function getActiveSubclassCode() |
||
1165 | { |
||
1166 | if (!$this->hasActiveSubClass()) { |
||
1167 | @trigger_error(sprintf( |
||
1168 | 'Calling %s() when there is no active subclass is deprecated since sonata-project/admin-bundle 3.52' |
||
1169 | .' and will throw an exception in 4.0.' |
||
1170 | .' Use %s::hasActiveSubClass() to know if there is an active subclass.', |
||
1171 | __METHOD__, |
||
1172 | __CLASS__ |
||
1173 | ), E_USER_DEPRECATED); |
||
1174 | // NEXT_MAJOR : remove the previous `trigger_error()` call, the `return null` statement, uncomment the following exception and declare string as return type |
||
1175 | // throw new \LogicException(sprintf( |
||
1176 | // 'Admin "%s" has no active subclass.', |
||
1177 | // static::class |
||
1178 | // )); |
||
1179 | |||
1180 | return null; |
||
1181 | } |
||
1182 | |||
1183 | $subClass = $this->getRequest()->query->get('subclass'); |
||
1184 | |||
1185 | if (!$this->hasSubClass($subClass)) { |
||
1186 | @trigger_error(sprintf( |
||
1187 | 'Calling %s() when there is no active subclass is deprecated since sonata-project/admin-bundle 3.52' |
||
1188 | .' and will throw an exception in 4.0.' |
||
1189 | .' Use %s::hasActiveSubClass() to know if there is an active subclass.', |
||
1190 | __METHOD__, |
||
1191 | __CLASS__ |
||
1192 | ), E_USER_DEPRECATED); |
||
1193 | // NEXT_MAJOR : remove the previous `trigger_error()` call, the `return null` statement, uncomment the following exception and declare string as return type |
||
1194 | // throw new \LogicException(sprintf( |
||
1195 | // 'Admin "%s" has no active subclass.', |
||
1196 | // static::class |
||
1197 | // )); |
||
1198 | |||
1199 | return null; |
||
1200 | } |
||
1201 | |||
1202 | return $subClass; |
||
1203 | } |
||
1204 | |||
1205 | public function getBatchActions() |
||
1206 | { |
||
1207 | $actions = []; |
||
1208 | |||
1209 | if ($this->hasRoute('delete') && $this->hasAccess('delete')) { |
||
1210 | $actions['delete'] = [ |
||
1211 | 'label' => 'action_delete', |
||
1212 | 'translation_domain' => 'SonataAdminBundle', |
||
1213 | 'ask_confirmation' => true, // by default always true |
||
1214 | ]; |
||
1215 | } |
||
1216 | |||
1217 | $actions = $this->configureBatchActions($actions); |
||
1218 | |||
1219 | foreach ($this->getExtensions() as $extension) { |
||
1220 | // NEXT_MAJOR: remove method check |
||
1221 | if (method_exists($extension, 'configureBatchActions')) { |
||
1222 | $actions = $extension->configureBatchActions($this, $actions); |
||
1223 | } |
||
1224 | } |
||
1225 | |||
1226 | foreach ($actions as $name => &$action) { |
||
1227 | if (!\array_key_exists('label', $action)) { |
||
1228 | $action['label'] = $this->getTranslationLabel($name, 'batch', 'label'); |
||
1229 | } |
||
1230 | |||
1231 | if (!\array_key_exists('translation_domain', $action)) { |
||
1232 | $action['translation_domain'] = $this->getTranslationDomain(); |
||
1233 | } |
||
1234 | } |
||
1235 | |||
1236 | return $actions; |
||
1237 | } |
||
1238 | |||
1239 | public function getRoutes() |
||
1240 | { |
||
1241 | $this->buildRoutes(); |
||
1242 | |||
1243 | return $this->routes; |
||
1244 | } |
||
1245 | |||
1246 | public function getRouterIdParameter() |
||
1247 | { |
||
1248 | return sprintf('{%s}', $this->getIdParameter()); |
||
1249 | } |
||
1250 | |||
1251 | public function getIdParameter() |
||
1252 | { |
||
1253 | $parameter = 'id'; |
||
1254 | |||
1255 | for ($i = 0; $i < $this->getChildDepth(); ++$i) { |
||
1256 | $parameter = sprintf('child%s', ucfirst($parameter)); |
||
1257 | } |
||
1258 | |||
1259 | return $parameter; |
||
1260 | } |
||
1261 | |||
1262 | public function hasRoute($name) |
||
1263 | { |
||
1264 | if (!$this->routeGenerator) { |
||
1265 | throw new \RuntimeException('RouteGenerator cannot be null'); |
||
1266 | } |
||
1267 | |||
1268 | return $this->routeGenerator->hasAdminRoute($this, $name); |
||
1269 | } |
||
1270 | |||
1271 | /** |
||
1272 | * @param string $name |
||
1273 | * @param string|null $adminCode |
||
1274 | * |
||
1275 | * @return bool |
||
1276 | */ |
||
1277 | public function isCurrentRoute($name, $adminCode = null) |
||
1278 | { |
||
1279 | if (!$this->hasRequest()) { |
||
1280 | return false; |
||
1281 | } |
||
1282 | |||
1283 | $request = $this->getRequest(); |
||
1284 | $route = $request->get('_route'); |
||
1285 | |||
1286 | if ($adminCode) { |
||
1287 | $admin = $this->getConfigurationPool()->getAdminByAdminCode($adminCode); |
||
1288 | } else { |
||
1289 | $admin = $this; |
||
1290 | } |
||
1291 | |||
1292 | if (!$admin) { |
||
1293 | return false; |
||
1294 | } |
||
1295 | |||
1296 | return sprintf('%s_%s', $admin->getBaseRouteName(), $name) === $route; |
||
1297 | } |
||
1298 | |||
1299 | public function generateObjectUrl($name, $object, array $parameters = [], $referenceType = RoutingUrlGeneratorInterface::ABSOLUTE_PATH) |
||
1300 | { |
||
1301 | $parameters['id'] = $this->getUrlSafeIdentifier($object); |
||
1302 | |||
1303 | return $this->generateUrl($name, $parameters, $referenceType); |
||
1304 | } |
||
1305 | |||
1306 | public function generateUrl($name, array $parameters = [], $referenceType = RoutingUrlGeneratorInterface::ABSOLUTE_PATH) |
||
1307 | { |
||
1308 | return $this->routeGenerator->generateUrl($this, $name, $parameters, $referenceType); |
||
1309 | } |
||
1310 | |||
1311 | public function generateMenuUrl($name, array $parameters = [], $referenceType = RoutingUrlGeneratorInterface::ABSOLUTE_PATH) |
||
1312 | { |
||
1313 | return $this->routeGenerator->generateMenuUrl($this, $name, $parameters, $referenceType); |
||
1314 | } |
||
1315 | |||
1316 | final public function setTemplateRegistry(MutableTemplateRegistryInterface $templateRegistry) |
||
1317 | { |
||
1318 | $this->templateRegistry = $templateRegistry; |
||
1319 | } |
||
1320 | |||
1321 | /** |
||
1322 | * @param array<string, string> $templates |
||
1323 | */ |
||
1324 | public function setTemplates(array $templates) |
||
1325 | { |
||
1326 | // NEXT_MAJOR: Remove this line |
||
1327 | $this->templates = $templates; |
||
1328 | |||
1329 | $this->getTemplateRegistry()->setTemplates($templates); |
||
1330 | } |
||
1331 | |||
1332 | /** |
||
1333 | * @param string $name |
||
1334 | * @param string $template |
||
1335 | */ |
||
1336 | public function setTemplate($name, $template) |
||
1337 | { |
||
1338 | // NEXT_MAJOR: Remove this line |
||
1339 | $this->templates[$name] = $template; |
||
1340 | |||
1341 | $this->getTemplateRegistry()->setTemplate($name, $template); |
||
1342 | } |
||
1343 | |||
1344 | /** |
||
1345 | * @deprecated since sonata-project/admin-bundle 3.34, will be dropped in 4.0. Use TemplateRegistry services instead |
||
1346 | * |
||
1347 | * @return array<string, string> |
||
1348 | */ |
||
1349 | public function getTemplates() |
||
1350 | { |
||
1351 | return $this->getTemplateRegistry()->getTemplates(); |
||
1352 | } |
||
1353 | |||
1354 | /** |
||
1355 | * @deprecated since sonata-project/admin-bundle 3.34, will be dropped in 4.0. Use TemplateRegistry services instead |
||
1356 | * |
||
1357 | * @param string $name |
||
1358 | * |
||
1359 | * @return string|null |
||
1360 | */ |
||
1361 | public function getTemplate($name) |
||
1362 | { |
||
1363 | return $this->getTemplateRegistry()->getTemplate($name); |
||
1364 | } |
||
1365 | |||
1366 | public function getNewInstance() |
||
1367 | { |
||
1368 | $object = $this->getModelManager()->getModelInstance($this->getClass()); |
||
1369 | |||
1370 | $this->appendParentObject($object); |
||
1371 | |||
1372 | foreach ($this->getExtensions() as $extension) { |
||
1373 | $extension->alterNewInstance($this, $object); |
||
1374 | } |
||
1375 | |||
1376 | return $object; |
||
1377 | } |
||
1378 | |||
1379 | public function getFormBuilder() |
||
1380 | { |
||
1381 | $this->formOptions['data_class'] = $this->getClass(); |
||
1382 | |||
1383 | $formBuilder = $this->getFormContractor()->getFormBuilder( |
||
1384 | $this->getUniqid(), |
||
1385 | $this->formOptions |
||
1386 | ); |
||
1387 | |||
1388 | $this->defineFormBuilder($formBuilder); |
||
1389 | |||
1390 | return $formBuilder; |
||
1391 | } |
||
1392 | |||
1393 | /** |
||
1394 | * This method is being called by the main admin class and the child class, |
||
1395 | * the getFormBuilder is only call by the main admin class. |
||
1396 | */ |
||
1397 | public function defineFormBuilder(FormBuilderInterface $formBuilder) |
||
1398 | { |
||
1399 | if (!$this->hasSubject()) { |
||
1400 | @trigger_error(sprintf( |
||
1401 | 'Calling %s() when there is no subject is deprecated since sonata-project/admin-bundle 3.65' |
||
1402 | .' and will throw an exception in 4.0. Use %s::setSubject() to set the subject.', |
||
1403 | __METHOD__, |
||
1404 | __CLASS__ |
||
1405 | ), E_USER_DEPRECATED); |
||
1406 | // NEXT_MAJOR : remove the previous `trigger_error()` call and uncomment the following exception |
||
1407 | // throw new \LogicException(sprintf( |
||
1408 | // 'Admin "%s" has no subject.', |
||
1409 | // static::class |
||
1410 | // )); |
||
1411 | } |
||
1412 | |||
1413 | $mapper = new FormMapper($this->getFormContractor(), $formBuilder, $this); |
||
1414 | |||
1415 | $this->configureFormFields($mapper); |
||
1416 | |||
1417 | foreach ($this->getExtensions() as $extension) { |
||
1418 | $extension->configureFormFields($mapper); |
||
1419 | } |
||
1420 | |||
1421 | $this->attachInlineValidator(); |
||
1422 | } |
||
1423 | |||
1424 | public function attachAdminClass(FieldDescriptionInterface $fieldDescription) |
||
1425 | { |
||
1426 | $pool = $this->getConfigurationPool(); |
||
1427 | |||
1428 | $adminCode = $fieldDescription->getOption('admin_code'); |
||
1429 | |||
1430 | if (null !== $adminCode) { |
||
1431 | if (!$pool->hasAdminByAdminCode($adminCode)) { |
||
1432 | return; |
||
1433 | } |
||
1434 | |||
1435 | $admin = $pool->getAdminByAdminCode($adminCode); |
||
1436 | } else { |
||
1437 | if (!$pool->hasAdminByClass($fieldDescription->getTargetModel())) { |
||
1438 | return; |
||
1439 | } |
||
1440 | |||
1441 | $admin = $pool->getAdminByClass($fieldDescription->getTargetModel()); |
||
1442 | } |
||
1443 | |||
1444 | if ($this->hasRequest()) { |
||
1445 | $admin->setRequest($this->getRequest()); |
||
1446 | } |
||
1447 | |||
1448 | $fieldDescription->setAssociationAdmin($admin); |
||
1449 | } |
||
1450 | |||
1451 | public function getObject($id) |
||
1452 | { |
||
1453 | $object = $this->getModelManager()->find($this->getClass(), $id); |
||
1454 | foreach ($this->getExtensions() as $extension) { |
||
1455 | $extension->alterObject($this, $object); |
||
1456 | } |
||
1457 | |||
1458 | return $object; |
||
1459 | } |
||
1460 | |||
1461 | public function getForm() |
||
1462 | { |
||
1463 | $this->buildForm(); |
||
1464 | |||
1465 | return $this->form; |
||
1466 | } |
||
1467 | |||
1468 | public function getList() |
||
1469 | { |
||
1470 | $this->buildList(); |
||
1471 | |||
1472 | return $this->list; |
||
1473 | } |
||
1474 | |||
1475 | /** |
||
1476 | * @final since sonata-project/admin-bundle 3.63.0 |
||
1477 | */ |
||
1478 | public function createQuery($context = 'list') |
||
1479 | { |
||
1480 | if (\func_num_args() > 0) { |
||
1481 | @trigger_error(sprintf( |
||
1482 | 'The $context argument of %s is deprecated since 3.3, to be removed in 4.0.', |
||
1483 | __METHOD__ |
||
1484 | ), E_USER_DEPRECATED); |
||
1485 | } |
||
1486 | |||
1487 | $query = $this->getModelManager()->createQuery($this->getClass()); |
||
1488 | |||
1489 | $query = $this->configureQuery($query); |
||
1490 | foreach ($this->extensions as $extension) { |
||
1491 | $extension->configureQuery($this, $query, $context); |
||
1492 | } |
||
1493 | |||
1494 | return $query; |
||
1495 | } |
||
1496 | |||
1497 | public function getDatagrid() |
||
1498 | { |
||
1499 | $this->buildDatagrid(); |
||
1500 | |||
1501 | return $this->datagrid; |
||
1502 | } |
||
1503 | |||
1504 | public function buildTabMenu($action, ?AdminInterface $childAdmin = null) |
||
1505 | { |
||
1506 | if ($this->loaded['tab_menu']) { |
||
1507 | return $this->menu; |
||
1508 | } |
||
1509 | |||
1510 | $this->loaded['tab_menu'] = true; |
||
1511 | |||
1512 | $menu = $this->menuFactory->createItem('root'); |
||
1513 | $menu->setChildrenAttribute('class', 'nav navbar-nav'); |
||
1514 | $menu->setExtra('translation_domain', $this->translationDomain); |
||
1515 | |||
1516 | // Prevents BC break with KnpMenuBundle v1.x |
||
1517 | if (method_exists($menu, 'setCurrentUri')) { |
||
1518 | $menu->setCurrentUri($this->getRequest()->getBaseUrl().$this->getRequest()->getPathInfo()); |
||
1519 | } |
||
1520 | |||
1521 | $this->configureTabMenu($menu, $action, $childAdmin); |
||
1522 | |||
1523 | foreach ($this->getExtensions() as $extension) { |
||
1524 | $extension->configureTabMenu($this, $menu, $action, $childAdmin); |
||
1525 | } |
||
1526 | |||
1527 | $this->menu = $menu; |
||
1528 | |||
1529 | return $this->menu; |
||
1530 | } |
||
1531 | |||
1532 | public function buildSideMenu($action, ?AdminInterface $childAdmin = null) |
||
1533 | { |
||
1534 | return $this->buildTabMenu($action, $childAdmin); |
||
1535 | } |
||
1536 | |||
1537 | /** |
||
1538 | * @param string $action |
||
1539 | * |
||
1540 | * @return ItemInterface |
||
1541 | */ |
||
1542 | public function getSideMenu($action, ?AdminInterface $childAdmin = null) |
||
1543 | { |
||
1544 | if ($this->isChild()) { |
||
1545 | return $this->getParent()->getSideMenu($action, $this); |
||
1546 | } |
||
1547 | |||
1548 | $this->buildSideMenu($action, $childAdmin); |
||
1549 | |||
1550 | return $this->menu; |
||
1551 | } |
||
1552 | |||
1553 | /** |
||
1554 | * Returns the root code. |
||
1555 | * |
||
1556 | * @return string the root code |
||
1557 | */ |
||
1558 | public function getRootCode() |
||
1559 | { |
||
1560 | return $this->getRoot()->getCode(); |
||
1561 | } |
||
1562 | |||
1563 | /** |
||
1564 | * Returns the master admin. |
||
1565 | * |
||
1566 | * @return AbstractAdmin the root admin class |
||
1567 | */ |
||
1568 | public function getRoot() |
||
1569 | { |
||
1570 | if (!$this->hasParentFieldDescription()) { |
||
1571 | return $this; |
||
1572 | } |
||
1573 | |||
1574 | return $this->getParentFieldDescription()->getAdmin()->getRoot(); |
||
1575 | } |
||
1576 | |||
1577 | public function setBaseControllerName($baseControllerName) |
||
1578 | { |
||
1579 | $this->baseControllerName = $baseControllerName; |
||
1580 | } |
||
1581 | |||
1582 | public function getBaseControllerName() |
||
1583 | { |
||
1584 | return $this->baseControllerName; |
||
1585 | } |
||
1586 | |||
1587 | /** |
||
1588 | * @param string $label |
||
1589 | */ |
||
1590 | public function setLabel($label) |
||
1591 | { |
||
1592 | $this->label = $label; |
||
1593 | } |
||
1594 | |||
1595 | public function getLabel() |
||
1596 | { |
||
1597 | return $this->label; |
||
1598 | } |
||
1599 | |||
1600 | /** |
||
1601 | * @param bool $persist |
||
1602 | * |
||
1603 | * NEXT_MAJOR: remove this method |
||
1604 | * |
||
1605 | * @deprecated since sonata-project/admin-bundle 3.34, to be removed in 4.0. |
||
1606 | */ |
||
1607 | public function setPersistFilters($persist) |
||
1608 | { |
||
1609 | @trigger_error(sprintf( |
||
1610 | 'The %s method is deprecated since version 3.34 and will be removed in 4.0.', |
||
1611 | __METHOD__ |
||
1612 | ), E_USER_DEPRECATED); |
||
1613 | |||
1614 | $this->persistFilters = $persist; |
||
1615 | } |
||
1616 | |||
1617 | public function setFilterPersister(?FilterPersisterInterface $filterPersister = null) |
||
1618 | { |
||
1619 | $this->filterPersister = $filterPersister; |
||
1620 | // NEXT_MAJOR remove the deprecated property will be removed. Needed for persisted filter condition. |
||
1621 | $this->persistFilters = true; |
||
1622 | } |
||
1623 | |||
1624 | /** |
||
1625 | * NEXT_MAJOR: Remove this method. |
||
1626 | * |
||
1627 | * @deprecated since sonata-project/admin-bundle 3.67, to be removed in 4.0. |
||
1628 | * |
||
1629 | * @param int $maxPerPage |
||
1630 | */ |
||
1631 | public function setMaxPerPage($maxPerPage) |
||
1632 | { |
||
1633 | @trigger_error(sprintf( |
||
1634 | 'The method %s is deprecated since sonata-project/admin-bundle 3.67 and will be removed in 4.0.', |
||
1635 | __METHOD__ |
||
1636 | ), E_USER_DEPRECATED); |
||
1637 | |||
1638 | $this->maxPerPage = $maxPerPage; |
||
1639 | } |
||
1640 | |||
1641 | /** |
||
1642 | * @return int |
||
1643 | */ |
||
1644 | public function getMaxPerPage() |
||
1645 | { |
||
1646 | // NEXT_MAJOR: Remove this line and uncomment the following. |
||
1647 | return $this->maxPerPage; |
||
1648 | // $sortValues = $this->getModelManager()->getDefaultSortValues($this->class); |
||
1649 | |||
1650 | // return $sortValues['_per_page'] ?? 25; |
||
1651 | } |
||
1652 | |||
1653 | /** |
||
1654 | * @param int $maxPageLinks |
||
1655 | */ |
||
1656 | public function setMaxPageLinks($maxPageLinks) |
||
1657 | { |
||
1658 | $this->maxPageLinks = $maxPageLinks; |
||
1659 | } |
||
1660 | |||
1661 | /** |
||
1662 | * @return int |
||
1663 | */ |
||
1664 | public function getMaxPageLinks() |
||
1665 | { |
||
1666 | return $this->maxPageLinks; |
||
1667 | } |
||
1668 | |||
1669 | public function getFormGroups() |
||
1670 | { |
||
1671 | if (!\is_array($this->formGroups) && 'sonata_deprecation_mute' !== (\func_get_args()[0] ?? null)) { |
||
1672 | @trigger_error(sprintf( |
||
1673 | 'Returning other type than array in method %s() is deprecated since sonata-project/admin-bundle 3.65.' |
||
1674 | .' It will return only array in version 4.0.', |
||
1675 | __METHOD__ |
||
1676 | ), E_USER_DEPRECATED); |
||
1677 | } |
||
1678 | |||
1679 | return $this->formGroups; |
||
1680 | } |
||
1681 | |||
1682 | public function setFormGroups(array $formGroups) |
||
1683 | { |
||
1684 | $this->formGroups = $formGroups; |
||
1685 | } |
||
1686 | |||
1687 | public function removeFieldFromFormGroup($key) |
||
1688 | { |
||
1689 | foreach ($this->formGroups as $name => $formGroup) { |
||
1690 | unset($this->formGroups[$name]['fields'][$key]); |
||
1691 | |||
1692 | if (empty($this->formGroups[$name]['fields'])) { |
||
1693 | unset($this->formGroups[$name]); |
||
1694 | } |
||
1695 | } |
||
1696 | } |
||
1697 | |||
1698 | /** |
||
1699 | * @param string $group |
||
1700 | */ |
||
1701 | public function reorderFormGroup($group, array $keys) |
||
1702 | { |
||
1703 | // NEXT_MAJOR: Remove the argument "sonata_deprecation_mute" in the following call. |
||
1704 | $formGroups = $this->getFormGroups('sonata_deprecation_mute'); |
||
1705 | $formGroups[$group]['fields'] = array_merge(array_flip($keys), $formGroups[$group]['fields']); |
||
1706 | $this->setFormGroups($formGroups); |
||
1707 | } |
||
1708 | |||
1709 | public function getFormTabs() |
||
1710 | { |
||
1711 | if (!\is_array($this->formTabs) && 'sonata_deprecation_mute' !== (\func_get_args()[0] ?? null)) { |
||
1712 | @trigger_error(sprintf( |
||
1713 | 'Returning other type than array in method %s() is deprecated since sonata-project/admin-bundle 3.65.' |
||
1714 | .' It will return only array in version 4.0.', |
||
1715 | __METHOD__ |
||
1716 | ), E_USER_DEPRECATED); |
||
1717 | } |
||
1718 | |||
1719 | return $this->formTabs; |
||
1720 | } |
||
1721 | |||
1722 | public function setFormTabs(array $formTabs) |
||
1723 | { |
||
1724 | $this->formTabs = $formTabs; |
||
1725 | } |
||
1726 | |||
1727 | public function getShowTabs() |
||
1728 | { |
||
1729 | if (!\is_array($this->showTabs) && 'sonata_deprecation_mute' !== (\func_get_args()[0] ?? null)) { |
||
1730 | @trigger_error(sprintf( |
||
1731 | 'Returning other type than array in method %s() is deprecated since sonata-project/admin-bundle 3.65.' |
||
1732 | .' It will return only array in version 4.0.', |
||
1733 | __METHOD__ |
||
1734 | ), E_USER_DEPRECATED); |
||
1735 | } |
||
1736 | |||
1737 | return $this->showTabs; |
||
1738 | } |
||
1739 | |||
1740 | public function setShowTabs(array $showTabs) |
||
1744 | |||
1745 | public function getShowGroups() |
||
1746 | { |
||
1747 | if (!\is_array($this->showGroups) && 'sonata_deprecation_mute' !== (\func_get_args()[0] ?? null)) { |
||
1748 | @trigger_error(sprintf( |
||
1749 | 'Returning other type than array in method %s() is deprecated since sonata-project/admin-bundle 3.65.' |
||
1750 | .' It will return only array in version 4.0.', |
||
1751 | __METHOD__ |
||
1752 | ), E_USER_DEPRECATED); |
||
1753 | } |
||
1754 | |||
1755 | return $this->showGroups; |
||
1756 | } |
||
1757 | |||
1758 | public function setShowGroups(array $showGroups) |
||
1759 | { |
||
1760 | $this->showGroups = $showGroups; |
||
1761 | } |
||
1762 | |||
1763 | public function reorderShowGroup($group, array $keys) |
||
1764 | { |
||
1765 | // NEXT_MAJOR: Remove the argument "sonata_deprecation_mute" in the following call. |
||
1766 | $showGroups = $this->getShowGroups('sonata_deprecation_mute'); |
||
1767 | $showGroups[$group]['fields'] = array_merge(array_flip($keys), $showGroups[$group]['fields']); |
||
1768 | $this->setShowGroups($showGroups); |
||
1769 | } |
||
1770 | |||
1771 | public function setParentFieldDescription(FieldDescriptionInterface $parentFieldDescription) |
||
1772 | { |
||
1773 | $this->parentFieldDescription = $parentFieldDescription; |
||
1774 | } |
||
1775 | |||
1776 | public function getParentFieldDescription() |
||
1777 | { |
||
1778 | if (!$this->hasParentFieldDescription()) { |
||
1779 | @trigger_error(sprintf( |
||
1780 | 'Calling %s() when there is no parent field description is deprecated since' |
||
1781 | .' sonata-project/admin-bundle 3.66 and will throw an exception in 4.0.' |
||
1782 | .' Use %s::hasParentFieldDescription() to know if there is a parent field description.', |
||
1783 | __METHOD__, |
||
1784 | __CLASS__ |
||
1785 | ), E_USER_DEPRECATED); |
||
1786 | // NEXT_MAJOR : remove the previous `trigger_error()` call, the `return null` statement, uncomment the following exception and declare FieldDescriptionInterface as return type |
||
1787 | // throw new \LogicException(sprintf( |
||
1788 | // 'Admin "%s" has no parent field description.', |
||
1789 | // static::class |
||
1790 | // )); |
||
1791 | |||
1792 | return null; |
||
1793 | } |
||
1794 | |||
1795 | return $this->parentFieldDescription; |
||
1796 | } |
||
1797 | |||
1798 | public function hasParentFieldDescription() |
||
1799 | { |
||
1800 | return $this->parentFieldDescription instanceof FieldDescriptionInterface; |
||
1801 | } |
||
1802 | |||
1803 | public function setSubject($subject) |
||
1804 | { |
||
1805 | if (\is_object($subject) && !is_a($subject, $this->getClass(), true)) { |
||
1806 | $message = <<<'EOT' |
||
1807 | You are trying to set entity an instance of "%s", |
||
1808 | which is not the one registered with this admin class ("%s"). |
||
1809 | This is deprecated since 3.5 and will no longer be supported in 4.0. |
||
1810 | EOT; |
||
1811 | |||
1812 | // NEXT_MAJOR : throw an exception instead |
||
1813 | @trigger_error(sprintf($message, \get_class($subject), $this->getClass()), E_USER_DEPRECATED); |
||
1814 | } |
||
1815 | |||
1816 | $this->subject = $subject; |
||
1817 | } |
||
1818 | |||
1819 | public function getSubject() |
||
1839 | |||
1840 | public function hasSubject() |
||
1852 | |||
1853 | public function getFormFieldDescriptions() |
||
1859 | |||
1860 | public function getFormFieldDescription($name) |
||
1861 | { |
||
1862 | $this->buildForm(); |
||
1863 | |||
1864 | if (!$this->hasFormFieldDescription($name)) { |
||
1865 | @trigger_error(sprintf( |
||
1866 | 'Calling %s() when there is no form field description is deprecated since' |
||
1867 | .' sonata-project/admin-bundle 3.69 and will throw an exception in 4.0.' |
||
1868 | .' Use %s::hasFormFieldDescription() to know if there is a form field description.', |
||
1869 | __METHOD__, |
||
1870 | __CLASS__ |
||
1871 | ), E_USER_DEPRECATED); |
||
1872 | // NEXT_MAJOR : remove the previous `trigger_error()` call, the `return null` statement, uncomment the following exception and declare FieldDescriptionInterface as return type |
||
1873 | // throw new \LogicException(sprintf( |
||
1874 | // 'Admin "%s" has no form field description for the field %s.', |
||
1875 | // static::class, |
||
1876 | // $name |
||
1877 | // )); |
||
1878 | |||
1879 | return null; |
||
1880 | } |
||
1881 | |||
1882 | return $this->formFieldDescriptions[$name]; |
||
1883 | } |
||
1884 | |||
1885 | /** |
||
1886 | * Returns true if the admin has a FieldDescription with the given $name. |
||
1887 | * |
||
1888 | * @param string $name |
||
1889 | * |
||
1890 | * @return bool |
||
1891 | */ |
||
1892 | public function hasFormFieldDescription($name) |
||
1893 | { |
||
1894 | $this->buildForm(); |
||
1895 | |||
1896 | return \array_key_exists($name, $this->formFieldDescriptions) ? true : false; |
||
1897 | } |
||
1898 | |||
1899 | public function addFormFieldDescription($name, FieldDescriptionInterface $fieldDescription) |
||
1900 | { |
||
1901 | $this->formFieldDescriptions[$name] = $fieldDescription; |
||
1902 | } |
||
1903 | |||
1904 | /** |
||
1905 | * remove a FieldDescription. |
||
1906 | * |
||
1907 | * @param string $name |
||
1908 | */ |
||
1909 | public function removeFormFieldDescription($name) |
||
1910 | { |
||
1911 | unset($this->formFieldDescriptions[$name]); |
||
1912 | } |
||
1913 | |||
1914 | /** |
||
1915 | * build and return the collection of form FieldDescription. |
||
1916 | * |
||
1917 | * @return FieldDescriptionInterface[] collection of form FieldDescription |
||
1918 | */ |
||
1919 | public function getShowFieldDescriptions() |
||
1920 | { |
||
1921 | $this->buildShow(); |
||
1922 | |||
1923 | return $this->showFieldDescriptions; |
||
1924 | } |
||
1925 | |||
1926 | /** |
||
1927 | * Returns the form FieldDescription with the given $name. |
||
1928 | * |
||
1929 | * @param string $name |
||
1930 | * |
||
1931 | * @return FieldDescriptionInterface |
||
1932 | */ |
||
1933 | public function getShowFieldDescription($name) |
||
1934 | { |
||
1935 | $this->buildShow(); |
||
1936 | |||
1937 | if (!$this->hasShowFieldDescription($name)) { |
||
1938 | @trigger_error(sprintf( |
||
1939 | 'Calling %s() when there is no show field description is deprecated since' |
||
1940 | .' sonata-project/admin-bundle 3.69 and will throw an exception in 4.0.' |
||
1941 | .' Use %s::hasFormFieldDescription() to know if there is a show field description.', |
||
1942 | __METHOD__, |
||
1943 | __CLASS__ |
||
1944 | ), E_USER_DEPRECATED); |
||
1945 | // NEXT_MAJOR : remove the previous `trigger_error()` call, the `return null` statement, uncomment the following exception and declare FieldDescriptionInterface as return type |
||
1946 | // throw new \LogicException(sprintf( |
||
1947 | // 'Admin "%s" has no show field description for the field %s.', |
||
1948 | // static::class, |
||
1949 | // $name |
||
1957 | |||
1958 | public function hasShowFieldDescription($name) |
||
1964 | |||
1965 | public function addShowFieldDescription($name, FieldDescriptionInterface $fieldDescription) |
||
1969 | |||
1970 | public function removeShowFieldDescription($name) |
||
1974 | |||
1975 | public function getListFieldDescriptions() |
||
1981 | |||
1982 | public function getListFieldDescription($name) |
||
2007 | |||
2008 | public function hasListFieldDescription($name) |
||
2014 | |||
2015 | public function addListFieldDescription($name, FieldDescriptionInterface $fieldDescription) |
||
2019 | |||
2020 | public function removeListFieldDescription($name) |
||
2024 | |||
2025 | public function getFilterFieldDescription($name) |
||
2049 | |||
2050 | public function hasFilterFieldDescription($name) |
||
2056 | |||
2057 | public function addFilterFieldDescription($name, FieldDescriptionInterface $fieldDescription) |
||
2061 | |||
2062 | public function removeFilterFieldDescription($name) |
||
2066 | |||
2067 | public function getFilterFieldDescriptions() |
||
2073 | |||
2074 | public function addChild(AdminInterface $child) |
||
2106 | |||
2107 | public function hasChild($code) |
||
2111 | |||
2112 | public function getChildren() |
||
2116 | |||
2117 | public function getChild($code) |
||
2138 | |||
2139 | public function setParent(AdminInterface $parent) |
||
2143 | |||
2144 | public function getParent() |
||
2164 | |||
2165 | final public function getRootAncestor() |
||
2175 | |||
2176 | final public function getChildDepth() |
||
2188 | |||
2189 | final public function getCurrentLeafChildAdmin() |
||
2203 | |||
2204 | public function isChild() |
||
2208 | |||
2209 | /** |
||
2210 | * Returns true if the admin has children, false otherwise. |
||
2211 | * |
||
2212 | * @return bool if the admin has children |
||
2213 | */ |
||
2214 | public function hasChildren() |
||
2218 | |||
2219 | public function setUniqid($uniqid) |
||
2223 | |||
2224 | public function getUniqid() |
||
2232 | |||
2233 | /** |
||
2234 | * Returns the classname label. |
||
2235 | * |
||
2236 | * @return string the classname label |
||
2237 | */ |
||
2238 | public function getClassnameLabel() |
||
2242 | |||
2243 | public function getPersistentParameters() |
||
2262 | |||
2263 | /** |
||
2264 | * @param string $name |
||
2265 | * |
||
2266 | * @return mixed|null |
||
2267 | */ |
||
2268 | public function getPersistentParameter($name) |
||
2274 | |||
2275 | public function getBreadcrumbs($action) |
||
2286 | |||
2287 | /** |
||
2288 | * Generates the breadcrumbs array. |
||
2289 | * |
||
2290 | * Note: the method will be called by the top admin instance (parent => child) |
||
2291 | * |
||
2292 | * @param string $action |
||
2293 | * |
||
2294 | * @return array |
||
2295 | */ |
||
2296 | public function buildBreadcrumbs($action, ?ItemInterface $menu = null) |
||
2310 | |||
2311 | /** |
||
2312 | * NEXT_MAJOR : remove this method. |
||
2313 | * |
||
2314 | * @return BreadcrumbsBuilderInterface |
||
2315 | */ |
||
2316 | final public function getBreadcrumbsBuilder() |
||
2331 | |||
2332 | /** |
||
2333 | * NEXT_MAJOR : remove this method. |
||
2334 | * |
||
2335 | * @return AbstractAdmin |
||
2336 | */ |
||
2337 | final public function setBreadcrumbsBuilder(BreadcrumbsBuilderInterface $value) |
||
2348 | |||
2349 | public function setCurrentChild($currentChild) |
||
2353 | |||
2354 | /** |
||
2355 | * NEXT_MAJOR: Remove this method. |
||
2356 | * |
||
2357 | * @deprecated since sonata-project/admin-bundle 3.65, to be removed in 4.0 |
||
2358 | */ |
||
2359 | public function getCurrentChild() |
||
2370 | |||
2371 | public function isCurrentChild(): bool |
||
2375 | |||
2376 | /** |
||
2377 | * Returns the current child admin instance. |
||
2378 | * |
||
2379 | * @return AdminInterface|null the current child admin instance |
||
2380 | */ |
||
2381 | public function getCurrentChildAdmin() |
||
2391 | |||
2392 | public function trans($id, array $parameters = [], $domain = null, $locale = null) |
||
2403 | |||
2404 | /** |
||
2405 | * Translate a message id. |
||
2406 | * |
||
2407 | * NEXT_MAJOR: remove this method |
||
2408 | * |
||
2409 | * @param string $id |
||
2410 | * @param int $count |
||
2411 | * @param string|null $domain |
||
2412 | * @param string|null $locale |
||
2413 | * |
||
2414 | * @return string the translated string |
||
2415 | * |
||
2416 | * @deprecated since sonata-project/admin-bundle 3.9, to be removed with 4.0 |
||
2417 | */ |
||
2418 | public function transChoice($id, $count, array $parameters = [], $domain = null, $locale = null) |
||
2429 | |||
2430 | public function setTranslationDomain($translationDomain) |
||
2434 | |||
2435 | public function getTranslationDomain() |
||
2439 | |||
2440 | /** |
||
2441 | * {@inheritdoc} |
||
2442 | * |
||
2443 | * NEXT_MAJOR: remove this method |
||
2444 | * |
||
2445 | * @deprecated since sonata-project/admin-bundle 3.9, to be removed with 4.0 |
||
2446 | */ |
||
2447 | public function setTranslator(TranslatorInterface $translator) |
||
2459 | |||
2460 | /** |
||
2461 | * {@inheritdoc} |
||
2462 | * |
||
2463 | * NEXT_MAJOR: remove this method |
||
2464 | * |
||
2465 | * @deprecated since sonata-project/admin-bundle 3.9, to be removed with 4.0 |
||
2466 | */ |
||
2467 | public function getTranslator() |
||
2476 | |||
2477 | public function getTranslationLabel($label, $context = '', $type = '') |
||
2481 | |||
2482 | public function setRequest(Request $request) |
||
2490 | |||
2491 | public function getRequest() |
||
2500 | |||
2501 | public function hasRequest() |
||
2505 | |||
2506 | public function setFormContractor(FormContractorInterface $formBuilder) |
||
2510 | |||
2511 | /** |
||
2512 | * @return FormContractorInterface |
||
2513 | */ |
||
2514 | public function getFormContractor() |
||
2518 | |||
2519 | public function setDatagridBuilder(DatagridBuilderInterface $datagridBuilder) |
||
2523 | |||
2524 | public function getDatagridBuilder() |
||
2528 | |||
2529 | public function setListBuilder(ListBuilderInterface $listBuilder) |
||
2533 | |||
2534 | public function getListBuilder() |
||
2538 | |||
2539 | public function setShowBuilder(ShowBuilderInterface $showBuilder) |
||
2543 | |||
2544 | /** |
||
2545 | * @return ShowBuilderInterface |
||
2546 | */ |
||
2547 | public function getShowBuilder() |
||
2551 | |||
2552 | public function setConfigurationPool(Pool $configurationPool) |
||
2556 | |||
2557 | /** |
||
2558 | * @return Pool |
||
2559 | */ |
||
2560 | public function getConfigurationPool() |
||
2564 | |||
2565 | public function setRouteGenerator(RouteGeneratorInterface $routeGenerator) |
||
2569 | |||
2570 | /** |
||
2571 | * @return RouteGeneratorInterface |
||
2572 | */ |
||
2573 | public function getRouteGenerator() |
||
2577 | |||
2578 | public function getCode() |
||
2582 | |||
2583 | /** |
||
2584 | * NEXT_MAJOR: Remove this function. |
||
2585 | * |
||
2586 | * @deprecated This method is deprecated since sonata-project/admin-bundle 3.24 and will be removed in 4.0 |
||
2587 | * |
||
2588 | * @param string $baseCodeRoute |
||
2589 | */ |
||
2590 | public function setBaseCodeRoute($baseCodeRoute) |
||
2599 | |||
2600 | public function getBaseCodeRoute() |
||
2622 | |||
2623 | public function getModelManager() |
||
2627 | |||
2628 | public function setModelManager(ModelManagerInterface $modelManager) |
||
2632 | |||
2633 | public function getManagerType() |
||
2637 | |||
2638 | /** |
||
2639 | * @param string $type |
||
2640 | */ |
||
2641 | public function setManagerType($type) |
||
2645 | |||
2646 | public function getObjectIdentifier() |
||
2650 | |||
2651 | /** |
||
2652 | * Set the roles and permissions per role. |
||
2653 | */ |
||
2654 | public function setSecurityInformation(array $information) |
||
2658 | |||
2659 | public function getSecurityInformation() |
||
2663 | |||
2664 | /** |
||
2665 | * Return the list of permissions the user should have in order to display the admin. |
||
2666 | * |
||
2667 | * @param string $context |
||
2668 | * |
||
2669 | * @return array |
||
2670 | */ |
||
2671 | public function getPermissionsShow($context) |
||
2680 | |||
2681 | public function showIn($context) |
||
2690 | |||
2691 | public function createObjectSecurity($object) |
||
2695 | |||
2696 | public function setSecurityHandler(SecurityHandlerInterface $securityHandler) |
||
2700 | |||
2701 | public function getSecurityHandler() |
||
2705 | |||
2706 | public function isGranted($name, $object = null) |
||
2717 | |||
2718 | public function getUrlSafeIdentifier($model) |
||
2722 | |||
2723 | public function getNormalizedIdentifier($model) |
||
2727 | |||
2728 | public function id($model) |
||
2732 | |||
2733 | public function setValidator($validator) |
||
2745 | |||
2746 | public function getValidator() |
||
2750 | |||
2751 | public function getShow() |
||
2757 | |||
2758 | public function setFormTheme(array $formTheme) |
||
2762 | |||
2763 | public function getFormTheme() |
||
2767 | |||
2768 | public function setFilterTheme(array $filterTheme) |
||
2772 | |||
2773 | public function getFilterTheme() |
||
2777 | |||
2778 | public function addExtension(AdminExtensionInterface $extension) |
||
2782 | |||
2783 | public function getExtensions() |
||
2787 | |||
2788 | public function setMenuFactory(FactoryInterface $menuFactory) |
||
2792 | |||
2793 | public function getMenuFactory() |
||
2797 | |||
2798 | public function setRouteBuilder(RouteBuilderInterface $routeBuilder) |
||
2802 | |||
2803 | public function getRouteBuilder() |
||
2807 | |||
2808 | public function toString($object) |
||
2820 | |||
2821 | public function setLabelTranslatorStrategy(LabelTranslatorStrategyInterface $labelTranslatorStrategy) |
||
2825 | |||
2826 | public function getLabelTranslatorStrategy() |
||
2830 | |||
2831 | public function supportsPreviewMode() |
||
2835 | |||
2836 | /** |
||
2837 | * NEXT_MAJOR: Remove this. |
||
2838 | * |
||
2839 | * @deprecated since sonata-project/admin-bundle 3.67, to be removed in 4.0. |
||
2840 | * |
||
2841 | * Set custom per page options. |
||
2842 | */ |
||
2843 | public function setPerPageOptions(array $options) |
||
2852 | |||
2853 | /** |
||
2854 | * Returns predefined per page options. |
||
2855 | * |
||
2856 | * @return array |
||
2857 | */ |
||
2858 | public function getPerPageOptions() |
||
2870 | |||
2871 | /** |
||
2872 | * Set pager type. |
||
2873 | * |
||
2874 | * @param string $pagerType |
||
2875 | */ |
||
2876 | public function setPagerType($pagerType) |
||
2880 | |||
2881 | /** |
||
2882 | * Get pager type. |
||
2883 | * |
||
2884 | * @return string |
||
2885 | */ |
||
2886 | public function getPagerType() |
||
2890 | |||
2891 | /** |
||
2892 | * Returns true if the per page value is allowed, false otherwise. |
||
2893 | * |
||
2894 | * @param int $perPage |
||
2895 | * |
||
2896 | * @return bool |
||
2897 | */ |
||
2898 | public function determinedPerPageValue($perPage) |
||
2902 | |||
2903 | public function isAclEnabled() |
||
2907 | |||
2908 | public function getObjectMetadata($object) |
||
2912 | |||
2913 | public function getListModes() |
||
2917 | |||
2918 | public function setListMode($mode) |
||
2926 | |||
2927 | public function getListMode() |
||
2935 | |||
2936 | public function getAccessMapping() |
||
2940 | |||
2941 | public function checkAccess($action, $object = null) |
||
2963 | |||
2964 | /** |
||
2965 | * Hook to handle access authorization, without throw Exception. |
||
2966 | * |
||
2967 | * @param string $action |
||
2968 | * @param object $object |
||
2969 | * |
||
2970 | * @return bool |
||
2971 | */ |
||
2972 | public function hasAccess($action, $object = null) |
||
2992 | |||
2993 | /** |
||
2994 | * @param string $action |
||
2995 | * @param object|null $object |
||
2996 | * |
||
2997 | * @return array |
||
2998 | */ |
||
2999 | public function configureActionButtons($action, $object = null) |
||
3073 | |||
3074 | /** |
||
3075 | * @param string $action |
||
3076 | * @param object $object |
||
3077 | * |
||
3078 | * @return array |
||
3079 | */ |
||
3080 | public function getActionButtons($action, $object = null) |
||
3093 | |||
3094 | /** |
||
3095 | * Get the list of actions that can be accessed directly from the dashboard. |
||
3096 | * |
||
3097 | * @return array |
||
3098 | */ |
||
3099 | public function getDashboardActions() |
||
3126 | |||
3127 | /** |
||
3128 | * Setting to true will enable mosaic button for the admin screen. |
||
3129 | * Setting to false will hide mosaic button for the admin screen. |
||
3130 | * |
||
3131 | * @param bool $isShown |
||
3132 | */ |
||
3133 | final public function showMosaicButton($isShown) |
||
3141 | |||
3142 | /** |
||
3143 | * @param object $object |
||
3144 | */ |
||
3145 | final public function getSearchResultLink($object) |
||
3155 | |||
3156 | /** |
||
3157 | * Checks if a filter type is set to a default value. |
||
3158 | * |
||
3159 | * @param string $name |
||
3160 | * |
||
3161 | * @return bool |
||
3162 | */ |
||
3163 | final public function isDefaultFilter($name) |
||
3174 | |||
3175 | /** |
||
3176 | * Check object existence and access, without throw Exception. |
||
3177 | * |
||
3178 | * @param string $action |
||
3179 | * @param object $object |
||
3180 | * |
||
3181 | * @return bool |
||
3182 | */ |
||
3183 | public function canAccessObject($action, $object) |
||
3187 | |||
3188 | protected function configureQuery(ProxyQueryInterface $query): ProxyQueryInterface |
||
3192 | |||
3193 | /** |
||
3194 | * @return MutableTemplateRegistryInterface |
||
3195 | */ |
||
3196 | final protected function getTemplateRegistry() |
||
3200 | |||
3201 | /** |
||
3202 | * Returns a list of default sort values. |
||
3203 | * |
||
3204 | * @return array{_page?: int, _per_page?: int, _sort_by?: string, _sort_order?: string} |
||
3205 | */ |
||
3206 | final protected function getDefaultSortValues(): array |
||
3221 | |||
3222 | /** |
||
3223 | * Returns a list of default filters. |
||
3224 | * |
||
3225 | * @return array |
||
3226 | */ |
||
3227 | final protected function getDefaultFilterValues() |
||
3242 | |||
3243 | protected function configureFormFields(FormMapper $form) |
||
3246 | |||
3247 | protected function configureListFields(ListMapper $list) |
||
3250 | |||
3251 | protected function configureDatagridFilters(DatagridMapper $filter) |
||
3254 | |||
3255 | protected function configureShowFields(ShowMapper $show) |
||
3258 | |||
3259 | protected function configureRoutes(RouteCollection $collection) |
||
3262 | |||
3263 | /** |
||
3264 | * Allows you to customize batch actions. |
||
3265 | * |
||
3266 | * @param array $actions List of actions |
||
3267 | * |
||
3268 | * @return array |
||
3269 | */ |
||
3270 | protected function configureBatchActions($actions) |
||
3274 | |||
3275 | /** |
||
3276 | * NEXT_MAJOR: remove this method. |
||
3277 | * |
||
3278 | * @deprecated Use configureTabMenu instead |
||
3279 | */ |
||
3280 | protected function configureSideMenu(ItemInterface $menu, $action, ?AdminInterface $childAdmin = null) |
||
3283 | |||
3284 | /** |
||
3285 | * Configures the tab menu in your admin. |
||
3286 | * |
||
3287 | * @param string $action |
||
3288 | */ |
||
3289 | protected function configureTabMenu(ItemInterface $menu, $action, ?AdminInterface $childAdmin = null) |
||
3295 | |||
3296 | /** |
||
3297 | * build the view FieldDescription array. |
||
3298 | */ |
||
3299 | protected function buildShow() |
||
3316 | |||
3317 | /** |
||
3318 | * build the list FieldDescription array. |
||
3319 | */ |
||
3320 | protected function buildList() |
||
3377 | |||
3378 | /** |
||
3379 | * Build the form FieldDescription collection. |
||
3380 | */ |
||
3381 | protected function buildForm() |
||
3396 | |||
3397 | /** |
||
3398 | * Gets the subclass corresponding to the given name. |
||
3399 | * |
||
3400 | * @param string $name The name of the sub class |
||
3401 | * |
||
3402 | * @return string the subclass |
||
3403 | */ |
||
3404 | protected function getSubClass($name) |
||
3413 | |||
3414 | /** |
||
3415 | * Attach the inline validator to the model metadata, this must be done once per admin. |
||
3416 | */ |
||
3417 | protected function attachInlineValidator() |
||
3454 | |||
3455 | /** |
||
3456 | * NEXT_MAJOR: Remove this function. |
||
3457 | * |
||
3458 | * @deprecated since sonata-project/admin-bundle 3.67, to be removed in 4.0. |
||
3459 | * |
||
3460 | * Predefine per page options. |
||
3461 | */ |
||
3462 | protected function predefinePerPageOptions() |
||
3468 | |||
3469 | /** |
||
3470 | * Return list routes with permissions name. |
||
3471 | * |
||
3472 | * @return array<string, string> |
||
3473 | */ |
||
3474 | protected function getAccess() |
||
3499 | |||
3500 | /** |
||
3501 | * Configures a list of default filters. |
||
3502 | */ |
||
3503 | protected function configureDefaultFilterValues(array &$filterValues) |
||
3506 | |||
3507 | /** |
||
3508 | * Configures a list of default sort values. |
||
3509 | * |
||
3510 | * Example: |
||
3511 | * $sortValues['_sort_by'] = 'foo' |
||
3512 | * $sortValues['_sort_order'] = 'DESC' |
||
3513 | */ |
||
3514 | protected function configureDefaultSortValues(array &$sortValues) |
||
3517 | |||
3518 | /** |
||
3519 | * Set the parent object, if any, to the provided object. |
||
3520 | */ |
||
3521 | final protected function appendParentObject(object $object): void |
||
3549 | |||
3550 | /** |
||
3551 | * Build all the related urls to the current admin. |
||
3552 | */ |
||
3553 | private function buildRoutes(): void |
||
3576 | } |
||
3577 | |||
3579 |
If you suppress an error, we recommend checking for the error condition explicitly: