3

I've created a multi-level main menu in Drupal 8, all levels need to show by default (they are revealed on hover/tap).

Unfortunately when adding this menu as a block, Drupal states:

The menu will only be visible if the menu item for the current page is at or below the selected starting level. Select level 1 to always keep this menu visible.

Which is exactly what I don't want, all items need to be visible by default regardless of the current menu item.

I can't see any obvious way around this, is there something obvious I'm missing?

2 Answers 2

4

All links in that menu have to have the "expanded" option checked. So you have to manually edit all of them and enable this option.

1
  • 1
    It seems like a strange (although I suppose flexible?) way of doing things, but I can confirm this fixed it. Thanks!
    – Sean
    Feb 2, 2016 at 23:01
1

I solved that by a custom block:

namespace Drupal\MY_MODULE\Plugin\Block;

use Drupal\Core\Block\BlockBase;


class MainMenuBlock extends BlockBase {

  /**
   * {@inheritdoc}
   */
  public function build() {
    $menu_name = 'main';
    $menu_tree = \Drupal::menuTree();
    $parameters = $menu_tree->getCurrentRouteMenuTreeParameters($menu_name);

    $parameters->expandedParents = []; // HERE HAPPENS THE MAGICK


    $tree = $menu_tree->load($menu_name, $parameters);
    $manipulators = array(
      array('callable' => 'menu.default_tree_manipulators:checkAccess'),
      array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
    );
    $tree = $menu_tree->transform($tree, $manipulators);

    return $menu_tree->build($tree);
  }
}

Found this idea on http://mks.pp.ua/content/drupal-8/snippet-render-menu-drupal-8-programtically.

If you don't know how to get your own block programmatically follow the instructions on https://www.drupal.org/node/2101565.

If you did that, you can add your custom block to any region of your theme.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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