diff --git a/Resources/config/services.yaml b/Resources/config/services.yaml index d037fa1..8630c9c 100644 --- a/Resources/config/services.yaml +++ b/Resources/config/services.yaml @@ -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 } diff --git a/Service/Generator.php b/Service/Generator.php index 72a1271..6a2fc16 100644 --- a/Service/Generator.php +++ b/Service/Generator.php @@ -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); } } diff --git a/Twig/MenuExtension.php b/Twig/MenuExtension.php new file mode 100644 index 0000000..9ca7989 --- /dev/null +++ b/Twig/MenuExtension.php @@ -0,0 +1,28 @@ +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); + } +}