How can you contact us

2By phone at +40 749 014 360

If you still have problems, please let us know, by sending an email to info@westweb-solutions.com Thank you!

OFFICE HOURS

Mon-Fri 9:00AM - 6:00AM
Sat - 9:00AM - 1:00PM
Sundays by appointment only!

Incorrect parent tree shows when the same URL is used in menu links many times

/ / Drupal 8

The problem: You have a page on the site that is linked in two places on the same menu, but when you visit the page, it only shows one of two possible menu trees.

The solution: You can patch the MenuActiveTrail.php file to find the current parent of the active item. 

  /**
   * {@inheritdoc}
   */
  public function getActiveLink($menu_name = NULL) {
    // Note: this is a very simple implementation. If you need more control
    // over the return value, such as matching a prioritized list of menu names,
    // you should substitute your own implementation for the 'menu.active_trail'
    // service in the container.
    // The menu links coming from the storage are already sorted by depth,
    // weight and ID.
    $found = NULL;

    $route_name = $this->routeMatch->getRouteName();
    // On a default (not custom) 403 page the route name is NULL. On a custom
    // 403 page we will get the route name for that page, so we can consider
    // it a feature that a relevant menu tree may be displayed.
    if ($route_name) {
      $route_parameters = $this->routeMatch->getRawParameters()->all();

      // Load links matching this route.
      $links = $this->menuLinkManager->loadLinksByRoute($route_name, $route_parameters, $menu_name);
      // Select the first matching link.
      if ($links) {
        if (count($links) > 1) {
          foreach ($links as $link) {
            $current_url = $link->getUrlObject()->toString();
            $url_without_last_part = dirname($current_url);

            if (\Drupal::service('plugin.manager.menu.link')->hasDefinition($link->getParent())) {
              $parent = \Drupal::service('plugin.manager.menu.link')->createInstance($link->getParent());
              $parent_url = $parent->getUrlObject()->toString();

              if ($url_without_last_part === $parent_url) {
                return $link;
              }
            }
          }
        }
        $found = array_pop($links);
      }
    }
    return $found;
  }

}

Sign in your account to have access to different features

Forgot your details?

TOP