From eb79fbf0eab85ac0936f68848d97b711ae2b075d Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Sun, 5 Apr 2020 11:01:37 +0800 Subject: [PATCH] Remove requirement of global variables for menu loading in twig --- Annotation/Menu.php | 1 + EventListener/MenuAnnotationListener.php | 17 ++++++++--------- Resources/config/services.yaml | 1 - Service/Generator.php | 15 ++++++++++++--- Twig/MenuExtension.php | 7 ++++++- 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/Annotation/Menu.php b/Annotation/Menu.php index e8001ae..09df340 100644 --- a/Annotation/Menu.php +++ b/Annotation/Menu.php @@ -9,5 +9,6 @@ use Annotation; */ class Menu { + public $group = 'main'; // default to main menu group public $selected; } diff --git a/EventListener/MenuAnnotationListener.php b/EventListener/MenuAnnotationListener.php index 9ba9c6e..8a785b9 100644 --- a/EventListener/MenuAnnotationListener.php +++ b/EventListener/MenuAnnotationListener.php @@ -17,14 +17,12 @@ class MenuAnnotationListener protected $annot_reader; protected $menu_gen; protected $twig; - protected $menu_id; - public function __construct(Reader $annot_reader, MenuGenerator $menu_gen, TwigEnvironment $twig, $menu_id) + public function __construct(Reader $annot_reader, MenuGenerator $menu_gen, TwigEnvironment $twig) { $this->annot_reader = $annot_reader; $this->menu_gen = $menu_gen; $this->twig = $twig; - $this->menu_id = $menu_id; } public function onKernelController(ControllerEvent $event) @@ -57,20 +55,21 @@ class MenuAnnotationListener if ($annotation == null) return; - $this->selectMenu($annotation->selected); + $this->selectMenu($annotation->group, $annotation->selected); } - protected function selectMenu($selected) + protected function selectMenu($group_id, $selected) { + $this->menu_gen->setSelected($group_id, $selected); + + /* // get menu - $menu = $this->menu_gen->getMenu($this->menu_id); + $menu = $this->menu_gen->getMenu($group_id); // set menu selected $sel = $menu['index']->get($selected); if ($sel != null) $sel->setSelected(); - - // create twig global variable - $this->twig->addGlobal('menu_' . $this->menu_id, $menu); + */ } } diff --git a/Resources/config/services.yaml b/Resources/config/services.yaml index 8630c9c..4ea3ee2 100644 --- a/Resources/config/services.yaml +++ b/Resources/config/services.yaml @@ -13,7 +13,6 @@ services: Catalyst\MenuBundle\EventListener\MenuAnnotationListener: arguments: $menu_gen: "@catalyst_menu.generator" - $menu_id: "main" tags: - { name: kernel.event_listener, event: kernel.controller, method: onKernelController } diff --git a/Service/Generator.php b/Service/Generator.php index cd6dda9..873c7e7 100644 --- a/Service/Generator.php +++ b/Service/Generator.php @@ -36,6 +36,18 @@ class Generator return $this->menu_data[$menu_key]; } + public function setSelected($menu_key, $id) + { + if (!isset($this->menu_data[$menu_key])) + return false; + + $sel = $this->menu_data[$menu_key]['index']->get($id); + if ($sel != null) + $sel->setSelected(); + + return true; + } + protected function processConfigs($data) { $pdata = []; @@ -63,8 +75,6 @@ class Generator $index = $pdata[$group]['index']; $menu = $pdata[$group]['menu']; - error_log(print_r($group_data, true)); - // 2nd layer is group data foreach ($group_data as $mi_data) { @@ -73,7 +83,6 @@ class Generator $mi_data['icon'] = null; // instantiate - error_log(print_r($mi_data, true)); $mi = $this->newItem($index, $mi_data['id'], $mi_data['label'], $mi_data['icon']); // acl diff --git a/Twig/MenuExtension.php b/Twig/MenuExtension.php index 9ca7989..a757d82 100644 --- a/Twig/MenuExtension.php +++ b/Twig/MenuExtension.php @@ -23,6 +23,11 @@ class MenuExtension extends AbstractExtension public function getMenu($menu_group) { - return $this->menu_gen->getMenu($menu_group); + $menu_data = $this->menu_gen->getMenu($menu_group); + + if (isset($menu_data['menu'])) + return $menu_data['menu']; + + return []; } }