From 25e74dfcf9b25276956206fa1c18c2f15290230a Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Sun, 5 Apr 2020 11:52:30 +0800 Subject: [PATCH] Add menu_link twig function to generate link from menu Item --- Resources/config/services.yaml | 2 +- Service/Generator.php | 18 +++--------------- Twig/MenuExtension.php | 25 ++++++++++++++++++++++++- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/Resources/config/services.yaml b/Resources/config/services.yaml index 4ea3ee2..87cc4b2 100644 --- a/Resources/config/services.yaml +++ b/Resources/config/services.yaml @@ -7,7 +7,6 @@ services: class: Catalyst\MenuBundle\Service\Generator autowire: true arguments: - $router: "@router.default" $menu_data: [] Catalyst\MenuBundle\EventListener\MenuAnnotationListener: @@ -18,6 +17,7 @@ services: Catalyst\MenuBundle\Twig\MenuExtension: arguments: + $router: "@router.default" $menu_gen: "@catalyst_menu.generator" tags: - { name: twig.extension } diff --git a/Service/Generator.php b/Service/Generator.php index 873c7e7..401626a 100644 --- a/Service/Generator.php +++ b/Service/Generator.php @@ -17,14 +17,12 @@ class Generator protected $index; protected $menu; - protected $router; protected $menu_data; - public function __construct(RouterInterface $router, $menu_data) + public function __construct($menu_data) { $this->index = new Collection(); $this->menu = new Collection(); - $this->router = $router; $this->menu_data = $this->processConfigs($menu_data); } @@ -129,18 +127,8 @@ class Generator { $mi = new Item(); $mi->setID($id) - ->setLabel($label); - - // TODO: have a way to set manual links or specify route parameters - try - { - $mi->setLink($this->router->generate($id)); - } - catch (RouteNotFoundException $e) - { - // no route, set to # - $mi->setLink('javascript:;'); - } + ->setLabel($label) + ->setRoute($id); if ($icon != null) $mi->setIcon($icon); diff --git a/Twig/MenuExtension.php b/Twig/MenuExtension.php index a757d82..e50c684 100644 --- a/Twig/MenuExtension.php +++ b/Twig/MenuExtension.php @@ -5,12 +5,17 @@ namespace Catalyst\MenuBundle\Twig; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; +use Symfony\Component\Routing\Exception\RouteNotFoundException; +use Catalyst\MenuBundle\Service\Generator; + class MenuExtension extends AbstractExtension { + protected $router; protected $menu_gen; - public function __construct($menu_gen) + public function __construct($router, Generator $menu_gen) { + $this->router = $router; $this->menu_gen = $menu_gen; } @@ -18,6 +23,7 @@ class MenuExtension extends AbstractExtension { return [ new TwigFunction('menu_get', [$this, 'getMenu']), + new TwigFunction('menu_link', [$this, 'getMenuLink']), ]; } @@ -30,4 +36,21 @@ class MenuExtension extends AbstractExtension return []; } + + public function getMenuLink($mi) + { + // generate a link URL from a menu item + // NOTE: right now we only use routes + + try + { + $link = $this->router->generate($mi->getID()); + } + catch (RouteNotFoundException $e) + { + $link = ''; + } + + return $link; + } }