1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LAG\AdminBundle\Event\Listener\Menu; |
4
|
|
|
|
5
|
|
|
use LAG\AdminBundle\Admin\Resource\Registry\ResourceRegistryInterface; |
6
|
|
|
use LAG\AdminBundle\Event\Events\Configuration\MenuConfigurationEvent; |
7
|
|
|
use LAG\AdminBundle\Exception\Exception; |
8
|
|
|
|
9
|
|
|
class MenuItemConfigurationListener |
10
|
|
|
{ |
11
|
|
|
private ResourceRegistryInterface $registry; |
12
|
|
|
|
13
|
|
|
public function __construct(ResourceRegistryInterface $registry) |
14
|
|
|
{ |
15
|
|
|
$this->registry = $registry; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
public function __invoke(MenuConfigurationEvent $event): void |
19
|
|
|
{ |
20
|
|
|
$menuConfiguration = $event->getMenuConfiguration(); |
21
|
|
|
$menuName = $event->getMenuName(); |
22
|
|
|
$menuConfiguration['children'] = $menuConfiguration['children'] ?? []; |
23
|
|
|
|
24
|
|
|
foreach ($menuConfiguration['children'] as $itemName => $itemConfiguration) { |
25
|
|
|
if (null === $itemConfiguration) { |
26
|
|
|
$itemConfiguration = []; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
if ($menuName === 'top') { |
30
|
|
|
if (empty($itemConfiguration['attributes']['class'])) { |
31
|
|
|
$itemConfiguration['attributes']['class'] = ''; |
32
|
|
|
} |
33
|
|
|
$itemConfiguration['attributes']['class'] .= ''; |
34
|
|
|
$itemConfiguration['linkAttributes'] = []; |
35
|
|
|
} |
36
|
|
|
// When an url is set, nothing to add, the item menu can be build |
37
|
|
|
if (\array_key_exists('url', $itemConfiguration)) { |
38
|
|
|
continue; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
// If the key "admin' is missing, we try to find an admin resource with the same name |
42
|
|
|
if (!\array_key_exists('admin', $itemConfiguration) && $this->registry->has($itemName)) { |
43
|
|
|
$itemConfiguration['admin'] = $itemName; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
// The default admins action is list |
47
|
|
|
if (\array_key_exists('admin', $itemConfiguration) && empty($itemConfiguration['action'])) { |
48
|
|
|
$itemConfiguration['action'] = 'list'; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// At this point, an pair admin/action or an url or an admin should be defined |
52
|
|
|
if (!\array_key_exists('admin', $itemConfiguration)) { |
53
|
|
|
throw new Exception(sprintf( |
54
|
|
|
'The configuration of the children "%s" in the menu "%s" is invalid : no admin/action nor url configured, and no admin with the name "%s" exists', |
55
|
|
|
$itemName, |
56
|
|
|
$menuName, |
57
|
|
|
$itemName |
58
|
|
|
)); |
59
|
|
|
} |
60
|
|
|
$menuConfiguration['children'][$itemName] = $itemConfiguration; |
61
|
|
|
} |
62
|
|
|
$event->setMenuConfiguration($menuConfiguration); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|