I'm trying to write a simple twig extension to render a menu inside twig template files.
I created a custom module for this purpose. It's folder structure looks like this:
-modules
--MenuRender
---src
----MenuRenderExtension.php
--menurender.info.yml
--menurender.services.yml
The *.info
file is a standard file, the *.services
files looks like this:
services:
menurender.twig_extension:
class: Drupal\MenuRender\MenuRenderExtension
tags:
- { name: twig.extension }
And the MenuRenderExtension
class is as follows:
namespace Drupal\MenuRender;
class MenuRenderExtension extends \Twig_Extension {
public function getName() {
return 'MODULE.twig_extension';
}
/**
* @return array
*/
public function getFunctions() {
return [
new \Twig_SimpleFunction('renderMenu', [$this, 'renderMenu'])
];
}
/**
* Provides function to programmatically rendering a menu
*
* @param String $menu_name
* The machine configuration id of the menu to render
*/
public function renderMenu($menu_name) {
// 50+ lines so i cut this part as it's mostly clean PHP
}
}
I am constantly getting this error msg in apache's error log
Service 'menurender.twig_extension' for consumer 'twig' does not implement Twig_ExtensionInterface
Any one have a clue ? p.s. new to drupal8
src
calledTwigExtension
, too. See: cgit.drupalcode.org/twig_blocks/tree/src/Twig/…