Add menu_link twig function to generate link from menu Item
This commit is contained in:
parent
b13defed11
commit
25e74dfcf9
3 changed files with 28 additions and 17 deletions
|
|
@ -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 }
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue