尝试使用hook_preprocess_menu在Drupal 8中突出显示当前菜单项


Trying to highlight current menu item in Drupal 8 using hook_preprocess_menu

我有下面的函数来创建活动跟踪功能。因此,如果我将/blog作为"父"和/blog/mypost的帖子,那么在mypost上,博客链接将显示为高亮显示。我不想为所有的博客文章制作菜单项。问题是当缓存打开时(不使用settings.local.php并且调试关闭),某些页面上的getRequestUri没有改变。它似乎被缓存取决于页面。它可以很好地关闭页面缓存,但我想让这个工作与缓存。是否有更好的方法来检查当前路径并应用活动类?

function mytheme_preprocess_menu(&$variables, $hook) {
  if($variables['theme_hook_original'] == 'menu__main'){
    $node = 'Drupal::routeMatch()->getParameter('node');
    if($node){
      $current_path = 'Drupal::request()->getRequestUri();
      $items = $variables['items'];
      foreach ($items as $key => $item) {
        // If current path starts with a part of another path i.e. a parent, set active to li.
        if (0 === strpos($current_path, $item['url']->toString())) {
          // Add active link.
          $variables['items'][$key]['attributes']['class'] .= ' menu-item--active-trail';
        }
      }
    }
  }
}

我也试过把它放进一个模块,试着看看我是否能得到当前路径,然后在菜单中做树枝逻辑——main.树枝。html模板,但我有同样的问题。

function highlight_menu_sections_template_preprocess_default_variables_alter(&$variables) {
  $variables['current_path'] = $_SERVER['REQUEST_URI'];
}

经过很长时间的尝试,我找到了一个很好的模块,它正好解决了这个问题。安装和运行,不配置,它只是工作:

https://www.drupal.org/project/menu_trail_by_path

D7和D8稳定版本

我尝试将活动路径声明为自定义菜单块的一部分,即使这样,我声明的路径也会被缓存。假设它与此更改日志中的"没有办法设置活动链接-如果您需要更多控制,请覆盖服务"语句有关,尽管为什么MenuTreeParameters->setActiveTrail()存在是任何人的猜测。

出于好奇(对于我来说,当我稍后搜索这个!),这里是我的块的build()函数:

public function build() {
  $menu_tree = 'Drupal::menuTree();
  $parameters = new MenuTreeParameters();
  $parameters->setRoot('menu_link_content:700c69e6-785b-4db7-be49-73188b47b5a3')->setMinDepth(1)->setMaxDepth(1)->onlyEnabledLinks();
  // An array of routes and menu_link_content ids to set as active
  $define_active_mlid = array(
    'view.press_releases.page_1' => 385
  );
  $route_name = 'Drupal::request()->get(RouteObjectInterface::ROUTE_NAME);
  if (array_key_exists($route_name, $define_active_mlid)) {
    $menu_link = 'Drupal::entityTypeManager()->getStorage('menu_link_content')->loadByProperties(array('id' => $define_active_mlid[$route_name]));
    $link = array_shift($menu_link);
    $parameters->setActiveTrail(array('menu_link_content:' . $link->uuid()));
  }
  $footer_tree = $menu_tree->load('footer', $parameters);
  $manipulators = array(
    array('callable' => 'menu.default_tree_manipulators:checkAccess'),
    array('callable' => 'menu.default_tree_manipulators:generateIndexAndSort'),
  );
  $tree = $menu_tree->transform($footer_tree, $manipulators);
  $menu = $menu_tree->build($tree);
  return array(
    'menu' => $menu,
  );
}

[添加一个新的答案,因为这是一个完全不同于我之前的方法]

如果一个基于css的解决方案是可接受的,这似乎工作得很好:

.page-node-type-press-release {
  a[data-drupal-link-system-path="press-room/press-releases"] {
    // active CSS styles here
  }
}