From e91ddfae33ca7f71b51f5b00aff6db6cdd138f51 Mon Sep 17 00:00:00 2001 From: Jean-Francois Monfort Date: Thu, 4 Mar 2021 11:39:20 +0100 Subject: [PATCH] Add priority tag support For MenuBuilder --- .../CompilerPass/MenuCompilerPass.php | 23 +++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/DependencyInjection/CompilerPass/MenuCompilerPass.php b/DependencyInjection/CompilerPass/MenuCompilerPass.php index b6b66a755..a8d87d950 100644 --- a/DependencyInjection/CompilerPass/MenuCompilerPass.php +++ b/DependencyInjection/CompilerPass/MenuCompilerPass.php @@ -36,13 +36,28 @@ class MenuCompilerPass implements CompilerPassInterface . "container.", MenuComposer::class)); } - $menuComposerDefinition = $container->getDefinition('chill.main.menu_composer'); - + $menuComposerDefinition = $container->getDefinition('chill.main.menu_composer'); + + $services = []; foreach ($container->findTaggedServiceIds('chill.menu_builder') as $id => $tags) { - $class = $container->getDefinition($id)->getClass(); + $services[] = [ + 'id' => $id, + 'priority' => $tags[0]['priority'] ?? 100, + ]; + } + + usort($services, function ($a, $b) { + if ($a['priority'] == $b['priority']) { + return 0; + } + return ($a['priority'] < $b['priority']) ? -1 : 1; + }); + + foreach ($services as $service) { + $class = $container->getDefinition($service['id'])->getClass(); foreach ($class::getMenuIds() as $menuId) { $menuComposerDefinition - ->addMethodCall('addLocalMenuBuilder', [new Reference($id), $menuId]); + ->addMethodCall('addLocalMenuBuilder', [new Reference($service['id']), $menuId]); } } }