- 🇺🇸United States dcam
The following workaround code was adapted from #64. It corrects the problem of flagging all routes of the same name by also comparing the route parameters.
/** * Prepares variables for block__system_menu_block templates. * * Work around an issue where front page menu links are not set in the * active trail. * * Default template: block.html.twig. * * @see https://www.drupal.org/project/drupal/issues/1578832 */ function my_theme_preprocess_block__system_menu_block(&$variables) { if (!\Drupal::service('path.matcher')->isFrontPage()) { return; } $homepage_path = \Drupal::config('system.site')->get('page.front'); $homepage_url = \Drupal::service('path.validator')->getUrlIfValid($homepage_path); if (!$homepage_url instanceof Url) { return; } foreach ($variables['content']['#items'] as $key => $item) { /** @var \Drupal\Core\Url $url */ $url = $item['url']; if (!($url instanceof Url) || !$url->isRouted()) { continue; } $is_front = $url->getRouteName() == '<front>'; $is_configured_route = $url->getRouteName() == $homepage_url->getRouteName() && $url->getRouteParameters() == $homepage_url->getRouteParameters(); if ($is_front || $is_configured_route) { $variables['content']['#items'][$key]['in_active_trail'] = TRUE; } } }