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
Comment #2
rphair CreditAttribution: rphair at COSD commentedComment #3
rphair CreditAttribution: rphair at COSD commentedlast 2 changes cosmetic only; for clarity.
Comment #4
vasi CreditAttribution: vasi at Evolving Web commentedI see this issue as well. Do you have any idea if this is new or not? If it's relatively recent, we can bisect.
Comment #5
vasi CreditAttribution: vasi at Evolving Web commentedVerified that it's broken even in 8.0.6.
Comment #6
vasi CreditAttribution: vasi at Evolving Web commentedWhat 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:
Here's a bit of code we used as a workaround, that basically does #1:
Thoughts?
Comment #7
rphair CreditAttribution: rphair at COSD commentedI'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?
Comment #8
vasi CreditAttribution: vasi at Evolving Web commentedWe should test on the 8.3.x or 8.4.x branch, I think.
Comment #9
rphair CreditAttribution: rphair at COSD commentedComment #10
dawehner#6Seems to be a totally reasonable fix, aka. provide the route parameters by default.
Comment #11
NewZeal CreditAttribution: NewZeal as a volunteer commentedI experienced a problem with Views menu links having an empty trail in the following:
Implementing the function at #6 did the trick to fill it correctly. Thanks Vasi.
Comment #12
nydragondad CreditAttribution: nydragondad as a volunteer commentedI 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)
Comment #13
rphair CreditAttribution: rphair at COSD commentedThis 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.
Comment #14
TLWatson+1 to getting a core fix, rather than having to 'patch' using #6 in a custom module.
Comment #15
bobbygryzyngerComment #20
catchThis is a duplicate of #2528166: Upcast named arguments/named parameters in views.