Reputation: 399
i'm trying to make a category / menu dynamic to update the links and everything from admin panel. I tried implementing Tree from gedmo but couldn't get the children even after i spend hours looking over the documentation. Also i want to use knp menu bundle. Can someone help me out to implement it and explain to me what is with lft, rgt, root, level from tree and why should i use when should i use them ?
Here's my method :
public function createAdminMenu(array $options)
{
$menu_item = $this->em->getRepository('AppBundle:MenuItem');
$menu = $this->factoryInterface->createItem('root');
$rootNodes = $menu_item->getRootNodes();
//var_dump($rootNodes);
$node = $menu_item->findOneByName('User');
var_dump($menu_item->getChildren());
foreach($rootNodes as $node) {
if($node->getDisplay())
{
$menu->addChild($node->getName(), array('uri' => $node->getUri()));
$child_node = $node->getChildren($node);
//var_dump($child_node);
foreach($child_node as $child)
{
//$menu[$node->getName()] = $menu->addChild($child->getName());
}
}
}
return $menu;
}
Upvotes: 1
Views: 287
Reputation: 399
Solved it like this:
$repo = $this->em->getRepository('AppBundle:MenuItem');
$nodes = $repo->findByRootNodes($menuId);
foreach ($nodes as $node) {
if ($node->getDisplay()) {
$menu->addChild($node->getName(), ['uri' => $node->getRoute()])
->setAttribute('dropdown', $node->getDropDown());
if ($node->getDisplayChildren()) {
$children = $repo->children($node);
if($children)
{
foreach ($children as $child)
{
if ($child->getDisplay())
{
$menu[$node->getName()]->addChild($child->getName(), ['uri' => $child->getRoute()])
->setAttribute('divider_prepend', $child->getDividerPrepend())
->setAttribute('divider_append', $child->getDividerAppend());
}
}
}
}
}
}
return $menu;
Upvotes: 1