Add twig extension for menu_get

This commit is contained in:
Kendrick Chan 2020-04-05 10:36:49 +08:00
parent 6bead2fc57
commit c6b2d114ba
3 changed files with 92 additions and 35 deletions

View file

@ -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 }

View file

@ -42,49 +42,72 @@ 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
// NOTE: extension already merged and sorted the groups for us
foreach ($data as $group => $group_data)
{ {
// 2nd layer are the groups and the parents // store orphans here in case their parents have not yet been defined
foreach ($instance_data as $group => $group_data) $orphans = [];
// initialize group data
if (!isset($pdata[$group]))
{ {
// initialize group data $pdata[$group] = [
if (!isset($pdata[$group])) 'menu' => new Collection(),
{ 'index' => new Collection(),
$pdata[$group] = [ ];
'menu' => new Collection(), }
'index' => new Collection(),
];
}
$index = $pdata[$group]['index']; $index = $pdata[$group]['index'];
$menu = $pdata[$group]['menu']; $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 foreach ($orphans[$mi_data['id']] as $orphan)
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)
{ {
$parent = $index->get($mi_data['parent']); $mi->addChild($orphan);
if ($parent == null)
continue;
$parent->addChild($mi);
} }
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
View 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);
}
}