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"
|
||||
tags:
|
||||
- { name: kernel.event_listener, event: kernel.controller, method: onKernelController }
|
||||
|
||||
Catalyst\MenuBundle\Twig\MenuExtension:
|
||||
arguments:
|
||||
$menu_gen: "@catalyst_menu.generator"
|
||||
tags:
|
||||
- { name: twig.extension }
|
||||
|
|
|
|||
|
|
@ -42,49 +42,72 @@ class Generator
|
|||
|
||||
error_log(print_r($data, true));
|
||||
|
||||
// first layer contains all the instances in config
|
||||
foreach ($data as $instance_data)
|
||||
// error_log(print_r($data, true));
|
||||
|
||||
// 1st layer are the groups and the parents
|
||||
// NOTE: extension already merged and sorted the groups for us
|
||||
foreach ($data as $group => $group_data)
|
||||
{
|
||||
// 2nd layer are the groups and the parents
|
||||
foreach ($instance_data as $group => $group_data)
|
||||
// store orphans here in case their parents have not yet been defined
|
||||
$orphans = [];
|
||||
|
||||
// initialize group data
|
||||
if (!isset($pdata[$group]))
|
||||
{
|
||||
// initialize group data
|
||||
if (!isset($pdata[$group]))
|
||||
{
|
||||
$pdata[$group] = [
|
||||
'menu' => new Collection(),
|
||||
'index' => new Collection(),
|
||||
];
|
||||
}
|
||||
$pdata[$group] = [
|
||||
'menu' => new Collection(),
|
||||
'index' => new Collection(),
|
||||
];
|
||||
}
|
||||
|
||||
$index = $pdata[$group]['index'];
|
||||
$menu = $pdata[$group]['menu'];
|
||||
$index = $pdata[$group]['index'];
|
||||
$menu = $pdata[$group]['menu'];
|
||||
|
||||
foreach ($group_data as $mi_data)
|
||||
error_log(print_r($group_data, true));
|
||||
|
||||
// 2nd layer is group data
|
||||
foreach ($group_data as $mi_data)
|
||||
{
|
||||
// check params
|
||||
if (!isset($mi_data['icon']))
|
||||
$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
|
||||
if (isset($mi_data['acl']))
|
||||
$mi->setACLKey($mi_data['acl']);
|
||||
|
||||
// check for orphans
|
||||
if (isset($orphans[$mi_data['id']]))
|
||||
{
|
||||
// check params
|
||||
if (!isset($mi_data['icon']))
|
||||
$mi_data['icon'] = null;
|
||||
|
||||
// instantiate
|
||||
$mi = $this->newItem($index, $mi_data['id'], $mi_data['label'], $mi_data['icon']);
|
||||
|
||||
// acl
|
||||
if (isset($mi_data['acl']))
|
||||
$mi->setACLKey($mi_data['acl']);
|
||||
|
||||
// check parent
|
||||
if (isset($mi_data['parent']) && $mi_data['parent'] != null)
|
||||
foreach ($orphans[$mi_data['id']] as $orphan)
|
||||
{
|
||||
$parent = $index->get($mi_data['parent']);
|
||||
if ($parent == null)
|
||||
continue;
|
||||
|
||||
$parent->addChild($mi);
|
||||
$mi->addChild($orphan);
|
||||
}
|
||||
else
|
||||
$menu->add($mi);
|
||||
}
|
||||
|
||||
// check parent
|
||||
if (isset($mi_data['parent']) && $mi_data['parent'] != null)
|
||||
{
|
||||
$parent = $index->get($mi_data['parent']);
|
||||
|
||||
// if parent has not been set, add to orphans
|
||||
if ($parent == null)
|
||||
{
|
||||
if (!isset($orphans[$mi_data['parent']]))
|
||||
$orphans[$mi_data['parent']] = [];
|
||||
|
||||
$orphans[$mi_data['parent']][] = $mi;
|
||||
continue;
|
||||
}
|
||||
|
||||
$parent->addChild($mi);
|
||||
}
|
||||
else
|
||||
$menu->add($mi);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
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