knplabs/KnpMenuBundle — Issue #85: Error when trying to create a custom renderer
Author: razeta
Created: 2011-11-19 00:38:17+00:00
State: open

Hello,

I am trying to create a cutom renderer as shown in the documentation.... I get the following error :

An exception has been thrown during the rendering of a template ("Catchable Fatal Error: Argument 1 passed to Application\FrontBundle\Menu\CustomRenderer::render() must implement interface Knp\Menu\ItemInterface, string given, called in C:\wamp\www\aletheiatrac\src\Application\FrontBundle\Menu\CustomRenderer.php on line 28 and defined in C:\wamp\www\aletheiatrac\src\Application\FrontBundle\Menu\CustomRenderer.php line 9") in "FrontBundle:Index:index.html.twig" at line 53.
500 Internal Server Error - Twig_Error_Runtime
1 linked Exception:

I have the following configuration:

VIEW----------------------------------------------
{{ knp_menu_render('frontBundleMainMenu', {}, 'customRender') }}

MENUBUILDER----------------------------------
class MenuBuilder
{
private $factory;

/**
* @param FactoryInterface $factory
*/
public function __construct(FactoryInterface $factory)
{
$this->factory = $factory;
}

public function createFrontBundleMenu(Request $request)
{
$menu = $this->factory->createItem('root');
$menu->setCurrentUri($request->getRequestUri());

$menu->addChild('Home', array('route' => 'main_menu', 'label' => 'Home', 'attributes' => array('icon' => '/bundles/front/images/buttons/home_64.png')));

return $menu;
}

MY SERVICE ---------------------------------

services:
frontBundle_main.menu_builder:
class: Application\FrontBundle\Menu\MenuBuilder
arguments: ["@knp_menu.factory"]

frontBundle_main.menu.main:
class: Knp\Menu\MenuItem # the service definition requires setting the class
factory_service: frontBundle_main.menu_builder
factory_method: createFrontBundleMenu
arguments: ["@request"]
scope: request # needed as we have the request as a dependency here
tags:
- { name: knp_menu.menu, alias: frontBundleMainMenu } # The alias is what is used to retrieve the menu

frontBundle_main.menu_renderer:
class: Application\FrontBundle\Menu\CustomRenderer # the service definition requires setting the class
arguments: [%kernel.charset%]
tags:
- { name: knp_menu.renderer, alias: cumRender } # The alias is what is used to retrieve the menu

CUSTOM RENDERER ------------------------------------------------
getDefaultOptions(), $options);

/**
* Return an empty string if any of the following are true:
* a) The menu has no children eligible to be displayed
* b) The depth is 0
* c) This menu item has been explicitly set to hide its children
*/
/*if (!$item->hasChildren() || 0 === $options['depth'] || !$item->getDisplayChildren()) {
return '';
}*/

return $this->render('FrontBundle:CustomRender:customRender.json.twig',
array('attr' => $item->getAttributes(),
'item' => $item,
'options' => $options,
'level' => $item->getLevel())
)->getContent();
}
}

I cant find the problem here.

If someone can help me please :D

Thanks.