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
|
class: Catalyst\MenuBundle\Service\Generator
|
||||||
autowire: true
|
autowire: true
|
||||||
arguments:
|
arguments:
|
||||||
$router: "@router.default"
|
|
||||||
$menu_data: []
|
$menu_data: []
|
||||||
|
|
||||||
Catalyst\MenuBundle\EventListener\MenuAnnotationListener:
|
Catalyst\MenuBundle\EventListener\MenuAnnotationListener:
|
||||||
|
|
@ -18,6 +17,7 @@ services:
|
||||||
|
|
||||||
Catalyst\MenuBundle\Twig\MenuExtension:
|
Catalyst\MenuBundle\Twig\MenuExtension:
|
||||||
arguments:
|
arguments:
|
||||||
|
$router: "@router.default"
|
||||||
$menu_gen: "@catalyst_menu.generator"
|
$menu_gen: "@catalyst_menu.generator"
|
||||||
tags:
|
tags:
|
||||||
- { name: twig.extension }
|
- { name: twig.extension }
|
||||||
|
|
|
||||||
|
|
@ -17,14 +17,12 @@ class Generator
|
||||||
protected $index;
|
protected $index;
|
||||||
protected $menu;
|
protected $menu;
|
||||||
|
|
||||||
protected $router;
|
|
||||||
protected $menu_data;
|
protected $menu_data;
|
||||||
|
|
||||||
public function __construct(RouterInterface $router, $menu_data)
|
public function __construct($menu_data)
|
||||||
{
|
{
|
||||||
$this->index = new Collection();
|
$this->index = new Collection();
|
||||||
$this->menu = new Collection();
|
$this->menu = new Collection();
|
||||||
$this->router = $router;
|
|
||||||
$this->menu_data = $this->processConfigs($menu_data);
|
$this->menu_data = $this->processConfigs($menu_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -129,18 +127,8 @@ class Generator
|
||||||
{
|
{
|
||||||
$mi = new Item();
|
$mi = new Item();
|
||||||
$mi->setID($id)
|
$mi->setID($id)
|
||||||
->setLabel($label);
|
->setLabel($label)
|
||||||
|
->setRoute($id);
|
||||||
// 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:;');
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($icon != null)
|
if ($icon != null)
|
||||||
$mi->setIcon($icon);
|
$mi->setIcon($icon);
|
||||||
|
|
|
||||||
|
|
@ -5,12 +5,17 @@ namespace Catalyst\MenuBundle\Twig;
|
||||||
use Twig\Extension\AbstractExtension;
|
use Twig\Extension\AbstractExtension;
|
||||||
use Twig\TwigFunction;
|
use Twig\TwigFunction;
|
||||||
|
|
||||||
|
use Symfony\Component\Routing\Exception\RouteNotFoundException;
|
||||||
|
use Catalyst\MenuBundle\Service\Generator;
|
||||||
|
|
||||||
class MenuExtension extends AbstractExtension
|
class MenuExtension extends AbstractExtension
|
||||||
{
|
{
|
||||||
|
protected $router;
|
||||||
protected $menu_gen;
|
protected $menu_gen;
|
||||||
|
|
||||||
public function __construct($menu_gen)
|
public function __construct($router, Generator $menu_gen)
|
||||||
{
|
{
|
||||||
|
$this->router = $router;
|
||||||
$this->menu_gen = $menu_gen;
|
$this->menu_gen = $menu_gen;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -18,6 +23,7 @@ class MenuExtension extends AbstractExtension
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
new TwigFunction('menu_get', [$this, 'getMenu']),
|
new TwigFunction('menu_get', [$this, 'getMenu']),
|
||||||
|
new TwigFunction('menu_link', [$this, 'getMenuLink']),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -30,4 +36,21 @@ class MenuExtension extends AbstractExtension
|
||||||
|
|
||||||
return [];
|
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