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