Add twig extension for menu_get
This commit is contained in:
parent
6bead2fc57
commit
c6b2d114ba
3 changed files with 92 additions and 35 deletions
|
|
@ -16,3 +16,9 @@ services:
|
||||||
$menu_id: "main"
|
$menu_id: "main"
|
||||||
tags:
|
tags:
|
||||||
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
|
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
|
||||||
|
|
||||||
|
Catalyst\MenuBundle\Twig\MenuExtension:
|
||||||
|
arguments:
|
||||||
|
$menu_gen: "@catalyst_menu.generator"
|
||||||
|
tags:
|
||||||
|
- { name: twig.extension }
|
||||||
|
|
|
||||||
|
|
@ -42,12 +42,15 @@ class Generator
|
||||||
|
|
||||||
error_log(print_r($data, true));
|
error_log(print_r($data, true));
|
||||||
|
|
||||||
// first layer contains all the instances in config
|
// error_log(print_r($data, true));
|
||||||
foreach ($data as $instance_data)
|
|
||||||
{
|
// 1st layer are the groups and the parents
|
||||||
// 2nd layer are the groups and the parents
|
// NOTE: extension already merged and sorted the groups for us
|
||||||
foreach ($instance_data as $group => $group_data)
|
foreach ($data as $group => $group_data)
|
||||||
{
|
{
|
||||||
|
// store orphans here in case their parents have not yet been defined
|
||||||
|
$orphans = [];
|
||||||
|
|
||||||
// initialize group data
|
// initialize group data
|
||||||
if (!isset($pdata[$group]))
|
if (!isset($pdata[$group]))
|
||||||
{
|
{
|
||||||
|
|
@ -60,6 +63,9 @@ class Generator
|
||||||
$index = $pdata[$group]['index'];
|
$index = $pdata[$group]['index'];
|
||||||
$menu = $pdata[$group]['menu'];
|
$menu = $pdata[$group]['menu'];
|
||||||
|
|
||||||
|
error_log(print_r($group_data, true));
|
||||||
|
|
||||||
|
// 2nd layer is group data
|
||||||
foreach ($group_data as $mi_data)
|
foreach ($group_data as $mi_data)
|
||||||
{
|
{
|
||||||
// check params
|
// check params
|
||||||
|
|
@ -67,18 +73,36 @@ class Generator
|
||||||
$mi_data['icon'] = null;
|
$mi_data['icon'] = null;
|
||||||
|
|
||||||
// instantiate
|
// instantiate
|
||||||
|
error_log(print_r($mi_data, true));
|
||||||
$mi = $this->newItem($index, $mi_data['id'], $mi_data['label'], $mi_data['icon']);
|
$mi = $this->newItem($index, $mi_data['id'], $mi_data['label'], $mi_data['icon']);
|
||||||
|
|
||||||
// acl
|
// acl
|
||||||
if (isset($mi_data['acl']))
|
if (isset($mi_data['acl']))
|
||||||
$mi->setACLKey($mi_data['acl']);
|
$mi->setACLKey($mi_data['acl']);
|
||||||
|
|
||||||
|
// check for orphans
|
||||||
|
if (isset($orphans[$mi_data['id']]))
|
||||||
|
{
|
||||||
|
foreach ($orphans[$mi_data['id']] as $orphan)
|
||||||
|
{
|
||||||
|
$mi->addChild($orphan);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// check parent
|
// check parent
|
||||||
if (isset($mi_data['parent']) && $mi_data['parent'] != null)
|
if (isset($mi_data['parent']) && $mi_data['parent'] != null)
|
||||||
{
|
{
|
||||||
$parent = $index->get($mi_data['parent']);
|
$parent = $index->get($mi_data['parent']);
|
||||||
|
|
||||||
|
// if parent has not been set, add to orphans
|
||||||
if ($parent == null)
|
if ($parent == null)
|
||||||
|
{
|
||||||
|
if (!isset($orphans[$mi_data['parent']]))
|
||||||
|
$orphans[$mi_data['parent']] = [];
|
||||||
|
|
||||||
|
$orphans[$mi_data['parent']][] = $mi;
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$parent->addChild($mi);
|
$parent->addChild($mi);
|
||||||
}
|
}
|
||||||
|
|
@ -86,7 +110,6 @@ class Generator
|
||||||
$menu->add($mi);
|
$menu->add($mi);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
error_log(print_r($pdata, true));
|
error_log(print_r($pdata, true));
|
||||||
|
|
||||||
|
|
|
||||||
28
Twig/MenuExtension.php
Normal file
28
Twig/MenuExtension.php
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Catalyst\MenuBundle\Twig;
|
||||||
|
|
||||||
|
use Twig\Extension\AbstractExtension;
|
||||||
|
use Twig\TwigFunction;
|
||||||
|
|
||||||
|
class MenuExtension extends AbstractExtension
|
||||||
|
{
|
||||||
|
protected $menu_gen;
|
||||||
|
|
||||||
|
public function __construct($menu_gen)
|
||||||
|
{
|
||||||
|
$this->menu_gen = $menu_gen;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getFunctions()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
new TwigFunction('menu_get', [$this, 'getMenu']),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getMenu($menu_group)
|
||||||
|
{
|
||||||
|
return $this->menu_gen->getMenu($menu_group);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue