title: Trying to highlight current menu item in Drupal 8 using hook_preprocess_menu
body: <p>I have the below function to create active trail functionality. So if I were to have /blog as a "parent" and a post of /blog/mypost, when on mypost the blog link would show as highlighted. I don't want to have to make menu items for all the blog posts. The problem is when caching is turned on (not using settings.local.php and debug turned off) the getRequestUri isn't changing on some pages. It seems to be cached depending on the page. It works fine with page caching turned off but I'd like to get this working with caching. Is there a better way to check for the current path and apply the active class? </p>
  
  <pre><code>function mytheme_preprocess_menu(&amp;$variables, $hook) {
    if($variables['theme_hook_original'] == 'menu__main'){
      $node = \Drupal::routeMatch()-&gt;getParameter('node');
      if($node){
        $current_path = \Drupal::request()-&gt;getRequestUri();
        $items = $variables['items'];
        foreach ($items as $key =&gt; $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']-&gt;toString())) {
            // Add active link.
            $variables['items'][$key]['attributes']['class'] .= ' menu-item--active-trail';
          }
        }
      }
    }
  }
  </code></pre>
  
  <p>I've also tried putting this into a module to try and see if I can get the current path to then do the twig logic in the menu--main.twig.html template but I have the same problem.</p>
  
  <pre><code>function highlight_menu_sections_template_preprocess_default_variables_alter(&amp;$variables) {
    $variables['current_path'] = $_SERVER['REQUEST_URI'];
  }
  </code></pre>