I want to create a block that contains somes links of a menu, without using the interface. So I created a custom module. Inside, there are somes files.

a mymodule/test.links.menu.yml :

  title: list
  route_name: test.listown

  title: Add new
  route_name: test.addnew
  weight: 0
  parent: test.menu.listown

and a mymodule/src/Plugin/Block/test_block_main.php

class test_block_main extends BlockBase {
  public function build(){

    $mp = \Drupal::menuTree()->getCurrentRouteMenuTreeParameters("test.menu.listown");
    $test = \Drupal::menuTree()->load("test.menu.listown", $mp);
    $render = \Drupal::menuTree()->build($test);

    return array('#markup' => \Drupal::service('renderer')->render($render));


I cleared caches (drush cache-rebuild). When I go to my homepage, the "test_block_main" is displayed, but it is empty. Why is my custom block empty? Is the "test.menu.listown" parameter passed to the methods not correct?


You also need to provide the actual menu config entity as default configuration in config/install. Note that test.menu.listown is not a valid menu name, they can't contain dots only alphanumeric characters and dashes.

I'd recommend to prefix all default configuration with your module name, so that they're unique. Something like mymodule-something.

And, try to follow coding standards and conventions. For example, always use camelCase for class names, like TestBlockMain in TestBlockMain.php.

And last, avoid calling render() yourself whenever possible. You can just return a render array from a block.

  • I thank you for the valuable tips ! Can you give me (a very sample) examples of the *.yml file in config/install for a menu entity and an another example for render a menu from a build function's block avoid calling "render()" ? I google search these examples but I didn't succeed... in drupal.org/node/2087879 by example, I don't understand how the *.yml "know" that this is a menu and I find anywhere an example where a menu is "programmed" form a block plugin .php (module/src/Plugin/Block/.php) file. I also sought on Github or in the example contrib module.. – matthieu lopez Jun 22 '15 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.