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
|
|
|
|
10
|
|
|
class LeftMenuConfigurationListener |
11
|
|
|
{ |
12
|
|
|
private ResourceRegistryInterface $registry; |
13
|
|
|
|
14
|
|
|
public function __construct(ResourceRegistryInterface $registry) |
15
|
|
|
{ |
16
|
|
|
$this->registry = $registry; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function __invoke(MenuConfigurationEvent $event): void |
20
|
|
|
{ |
21
|
|
|
$menuConfiguration = $event->getMenuConfiguration(); |
22
|
|
|
$menuConfiguration['attributes']['id'] = 'accordionSidebar'; |
23
|
|
|
$menuConfiguration['attributes']['class'] = 'navbar-nav bg-gradient-primary sidebar sidebar-dark accordion'; |
24
|
|
|
$menuConfiguration['extras']['brand'] = true; |
25
|
|
|
$menuConfiguration['extras']['homepage'] = true; |
26
|
|
|
|
27
|
|
|
if (isset($menuConfiguration['children']) && \is_array($menuConfiguration['children'])) { |
28
|
|
|
foreach ($menuConfiguration['children'] as $index => $item) { |
29
|
|
|
if (empty($item['attributes']['class'])) { |
30
|
|
|
$item['attributes']['class'] = 'nav-item'; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (empty($item['linkAttributes']['class'])) { |
34
|
|
|
$item['linkAttributes']['class'] = 'nav-link'; |
35
|
|
|
} |
36
|
|
|
$menuConfiguration['children'][$index] = $item; |
37
|
|
|
} |
38
|
|
|
} else { |
39
|
|
|
$menuConfiguration['children'] = []; |
40
|
|
|
|
41
|
|
|
// The default main menu is composed by links to the list action of each admin resource |
42
|
|
|
foreach ($this->registry->all() as $resourceName => $resource) { |
43
|
|
|
if (empty($resource->getConfiguration()['actions']) || !\array_key_exists('list', $resource->getConfiguration()['actions'])) { |
44
|
|
|
continue; |
45
|
|
|
} |
46
|
|
|
$menuConfiguration['children'][$resourceName] = [ |
47
|
|
|
'admin' => $resourceName, |
48
|
|
|
'action' => 'list', |
49
|
|
|
'attributes' => [ |
50
|
|
|
'class' => 'nav-item', |
51
|
|
|
], |
52
|
|
|
]; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
$event->setMenuConfiguration($menuConfiguration); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|