1

I load my main menu programmatically with the following code :

    $menuTree = \Drupal::menuTree();
    $menuTreeParameters = $menuTree->getCurrentRouteMenuTreeParameters($name);
    $menuTreeParameters
        ->setMinDepth($minLevel)
        ->setMaxDepth($maxLevel);

    $tree = $menuTree->load($name, $menuTreeParameters);
    $tree = $menuTree->transform($tree, $this->getDefaultManipulators());

    return render($menuTree->build($tree));

I would like to get all my menu items, included thoses which are disabled. When I inspect $tree variable, I have all my menu items.

I don't know why but after the call to the build method, I get only my enabled items.

0

Check this line in buildItems() which is called by $menuTree->build($tree), in \Drupal\Core\Menu\MenuLinkTree.php:

  // Generally we only deal with visible links, but just in case.
  if (!$link->isEnabled()) {
    continue;
  }

It's designed to exclude links that are not enabled. I played around with something like this to get them all showing:

foreach ($tree as $id => $treeItem) {
    // Get the link definition.
    $definition = $tree[$id]->link->getPluginDefinition();

    // Skip links that are already enabled.
    if ($definition['enabled'] == 1) {
        continue;
    }

    // Change definition and enable link.
    $definition['enabled'] = 1;

    // Recreate the link.
    // I'm not 100% sure about the correctness of this part. 
    // This is my first time playing with this stuff. Hopefully someone can fill in this blank.
    $pluginContainer = \Drupal::getContainer();
    $conf = [];
    $newLink = \Drupal\menu_link_content\Plugin\Menu\MenuLinkContent::create($pluginContainer, $conf, $id, $definition);
    $tree[$id]->link = $newLink;
}

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.