We have not found it possible to use the MenuActiveTrail service to get the active trail since it breaks for certain view pages, including 2 of the built-in views. Not all of this is necessary to demonstrate the problem, but I wanted to prove this problem wasn't with the default Views configuration from any previous version of Drupal:
-- install Drupal 8 (last tested on 8.2.1) with Standard profile
-- drush en devel kint
-- add to bartik.theme & rebuild theme registry:

function bartik_preprocess_page(&$variables) {
  $route_match = \Drupal::routeMatch();
  $route_raw_parameters = $route_match->getRawParameters()->all();
  $getActiveTrailIds = \Drupal::service('menu.active_trail')->getActiveTrailIds("main");
  kint(
    "MenuActiveTrail::getActiveTrailIds(main)", $getActiveTrailIds,
    "raw parameters for MenuLinkManager::loadLinksByRoute()", $route_raw_parameters
  );
}

-- add a basic page Foo, provide a menu link on main menu:

  • getActiveTrailIds shows one menu link (the current page menu link ID), before empty string that terminates the returned array: as expected.

-- create a view Bar (e.g. a Page of all Basic page summaries); set its Page settings to be a Normal menu entry on the Main menu.

  • getActiveTrailIds also shows one menu link (the current page view ID): as expected.

-- enable the built-in Glossary view, which is pre-configured to be on the Main menu.

  • getActiveTrailIds shows NO menu link IDs.

-- enable the built-in Archive view; set its Page settings to be a Normal menu entry on the Main menu.

  • getActiveTrailIds shows NO menu link IDs.

Taxonomy pages, though they are also built-in views, also work normally, as does the Frontpage view. One difference between the working & not working views is that the user-defined, correctly working view (Bar) shows these raw parameters:

array(0)

and the non-working view (Archive, Glossary) show these raw parameters:

array(1)
    'arg_0' => NULL

If this behaviour is by design, or if a fix is pushed back to a future version, please we need the best practice to fix the above cases in modules (e.g. this issue for Menu Breadcrumb) that expect menu links in the active trail to always be returned from functions like MenuActiveTrail::getActiveTrailIds (and ::getActiveLink) and MenuLinkManager::loadLinksByRoute.

For instance we might filter out a NULL argument from the raw parameters before passing them to loadLinksByRoute(), but need to know first if that would break anything else.

Comments

rphair created an issue. See original summary.

rphair’s picture

Issue summary: View changes
rphair’s picture

Issue summary: View changes

last 2 changes cosmetic only; for clarity.

vasi’s picture

I see this issue as well. Do you have any idea if this is new or not? If it's relatively recent, we can bisect.

vasi’s picture

Verified that it's broken even in 8.0.6.

vasi’s picture

What seems to be happening here:

So these won't compare the same, and we end up with no active trail. Our options seem to be:

  1. Views could just generate the menu links including the route_parameters. This would be a small fix, and would just fix this one case.
  2. Drupal could enforce that menu links are always generated including route_parameters. This would fix the general case, but would require that every time we save a menu link, we load its route. Is that too heavy-weight?
  3. Perhaps we could somehow manage this at lookup-time, instead of link generation time. But I don't see any way to allow menu links to be looked up both with or without defaulted route_parameters.

Here's a bit of code we used as a workaround, that basically does #1:

/**
 * Implements hook_menu_links_discovered_alter().
 *
 * This gives views menu links the right route_parameters setting, so that
 * they get the proper active trails.
 */
function scratchpad_menu_links_discovered_alter(&$links) {
  // Find menu links provided by views. For each link, find its route.
  $views_link_routes = [];
  foreach ($links as $key => $link) {
    if (!isset($link['provider']) || $link['provider'] != 'views') {
      continue;
    }
    $views_link_routes[$key] = $link['route_name'];
  }
  if (empty($views_link_routes)) {
    return;
  }

  // Load all the routes.
  /** @var \Drupal\Core\Routing\RouteProviderInterface $route_provider */
  $route_provider = \Drupal::service('router.route_provider');
  $routes = $route_provider->getRoutesByNames($views_link_routes);

  // Iterate over all the views links.
  foreach (array_keys($views_link_routes) as $key) {
    if ($route = $routes[$views_link_routes[$key]]) {
      // If we have a route, find the route variables.
      $link =& $links[$key];
      $variables = $route->compile()->getVariables();
      foreach ($variables as $v) {
        // If any variable is missing, add the default value.
        if (!isset($link['route_parameters'][$v])) {
          $link['route_parameters'][$v] = $route->getDefault($v);
        }
      }
    }
  }
}

Thoughts?

rphair’s picture

I've just tested this again in Drupal 8.3.1 and the same problem exists as far as I can tell: testing with the Archive and Glossary default views for a new installation, both disabled by default, shows they still don't have an active trail when created from the default configuration.

Will someone with core experience please tell me whether I should leave the Version at 8.2.1, to show that the bug has been around for a while (even longer according to @vasi), or update the Version to 8.3.1 to show it's still active & potentially affecting any new installation?

vasi’s picture

We should test on the 8.3.x or 8.4.x branch, I think.

rphair’s picture

Version: 8.2.1 » 8.3.1
dawehner’s picture

#6Seems to be a totally reasonable fix, aka. provide the route parameters by default.

NewZeal’s picture

I experienced a problem with Views menu links having an empty trail in the following:

$parameters = $this->menuTree->getCurrentRouteMenuTreeParameters($menu_name);
$trail = $parameters->activeTrail; 

Implementing the function at #6 did the trick to fill it correctly. Thanks Vasi.

nydragondad’s picture

I am sorry that I am related new as Drupal site owner, for fixing #6, would you please offer a little bit more detailed explanation, such as which module and file it should be modified, really appreciate.

Actually, the error message I saw after upgrade to Drupal 8.5.3 is like this:

The website encountered an unexpected error. Please try again later.
TypeError: Argument 9 passed to Drupal\system\PathBasedBreadcrumbBuilder::__construct() must implement interface Drupal\Core\Path\PathMatcherInterface, instance of Drupal\Core\Menu\MenuActiveTrail given, called in /home/innovake/public_html/core/lib/Drupal/Component/DependencyInjection/Container.php on line 298 in Drupal\system\PathBasedBreadcrumbBuilder->__construct() (line 119 of core/modules/system/src/PathBasedBreadcrumbBuilder.php).
Drupal\system\PathBasedBreadcrumbBuilder->__construct(Object, Object, Object, Object, Object, Object, Object, Object, Object, Object) (Line: 298)
Drupal\Component\DependencyInjection\Container->createService(Array, 'current_page_crumb.breadcrumb') (Line: 171)
Drupal\Component\DependencyInjection\Container->get('current_page_crumb.breadcrumb', 1) (Line: 480)
Drupal\Component\DependencyInjection\Container->resolveServicesAndParameters(Array) (Line: 319)
Drupal\Component\DependencyInjection\Container->createService(Array, 'breadcrumb') (Line: 171)
Drupal\Component\DependencyInjection\Container->get('breadcrumb') (Line: 63)
Drupal\system\Plugin\Block\SystemBreadcrumbBlock::create(Object, Array, 'system_breadcrumb_block', Array) (Line: 21)
Drupal\Core\Plugin\Factory\ContainerFactory->createInstance('system_breadcrumb_block', Array) (Line: 76)
Drupal\Component\Plugin\PluginManagerBase->createInstance('system_breadcrumb_block', Array) (Line: 62)
Drupal\Core\Plugin\DefaultSingleLazyPluginCollection->initializePlugin('system_breadcrumb_block') (Line: 57)
Drupal\block\BlockPluginCollection->initializePlugin('system_breadcrumb_block') (Line: 80)
Drupal\Component\Plugin\LazyPluginCollection->get('system_breadcrumb_block') (Line: 45)
Drupal\block\BlockPluginCollection->get('system_breadcrumb_block') (Line: 83)
Drupal\Core\Plugin\DefaultSingleLazyPluginCollection->setConfiguration(Array) (Line: 99)
Drupal\Core\Plugin\DefaultSingleLazyPluginCollection->addInstanceId('system_breadcrumb_block', Array) (Line: 55)
Drupal\Core\Plugin\DefaultSingleLazyPluginCollection->__construct(Object, 'system_breadcrumb_block', Array) (Line: 34)
Drupal\block\BlockPluginCollection->__construct(Object, 'system_breadcrumb_block', Array, 'breadcrumbs') (Line: 149)
Drupal\block\Entity\Block->getPluginCollection() (Line: 138)
Drupal\block\Entity\Block->getPlugin() (Line: 113)
Drupal\block\BlockAccessControlHandler->checkAccess(Object, 'view', Object) (Line: 105)
Drupal\Core\Entity\EntityAccessControlHandler->access(Object, 'view', Object, 1) (Line: 362)
Drupal\Core\Entity\Entity->access('view', NULL, 1) (Line: 56)
Drupal\block\BlockRepository->getVisibleBlocksPerRegion(Array) (Line: 137)
Drupal\block\Plugin\DisplayVariant\BlockPageVariant->build() (Line: 259)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 117)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 156)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 99)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 78)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 664)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

rphair’s picture

Version: 8.3.1 » 8.6.1

This issue is still alive and well in 8.6.1 - though I can't help with the solution, I originally posted this on behalf of the menu_breadcrumb module and now have to conclude that the absent active trail for these built-in views is somehow by design, or has become so now that the anomaly has become institutionalised. Please post in the related issue #2820206: No active trail for views with a NULL parameter if this issue ever gets any progress or attention.

TLWatson’s picture

+1 to getting a core fix, rather than having to 'patch' using #6 in a custom module.

bobbygryzynger’s picture

Version: 8.6.1 » 8.6.x-dev

Core issues are now filed against the dev versions where changes will be made. Document the specific release you are using in your issue comment. More information about choosing a version.

Version: 8.6.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Branches prior to 8.8.x are not supported, and Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
catch’s picture

Status: Active » Closed (duplicate)
Related issues: +#2528166: Upcast named arguments/named parameters in views