TransWikia.com

Programmatic menu link for a view with contextual filter and no parameters in the path

Drupal Answers Asked on December 31, 2021

Related question (my question is when contextual filters are NOT in the path): Generate menu links for a view with contextual filter and parameters in the path

In mymodule.links.menu.yml I have defined a link:

MYMODULE.fruits-apple:
  title: 'Apple'
  route_name: view.fruit_list.page_apple
  parent: MYMODULE.fruits
  weight: 5
  menu_name: fruits-navigation

The view page view.fruit_list.page_apple has the path /fruits/apple.

When on a page other than /fruits/apple, the link appears in my menu and allows access to the view. When on the parent page (/fruits), it appears expanded under the parent link in the menu. But, on /fruits/apple, the link is not shown.

I checked the route with /devel/routes and found this:

Route name: view.fruit_list.page_apple
Path: /fruits/apple/{arg_0}

I think the problem is that I am not setting {arg_0} in mymodule.links.menu.yml. I think this is the problem because I have links to views with routes that do not have {arg_0} in them and they display properly.

{arg_0} is a contextual filter for the User ID:

When the value is NOT in the URL: Provide default value (User ID from logged in user)

I am using the view path as /fruits/apple, so I never explicitly set the user ID for the contextual filter; instead, I rely on Views to automatically pull in the value.

In this case, how can I set the route parameter {arg_0} for my view so that the menu link appears properly on the view path?

I tried adding route_parameters: { arg_0: '' } and route_parameters: { arg_0: 'NULL' }, but that didn’t help.

MORE DEBUGGING

When I create a link in the menu UI to the path /fruits/apple and then go the page /fruits/apple, I see BOTH my menu UI link and my module-defined link. But if I disable or delete my menu UI link, then my module-defined link is not shown.

In the same way, I can create a menu link using the options in the Views UI, which also causes both my Views UI menu link + module-defined menu link to be displayed on /fruits/apple. When I remove the Views UI link, the module-defined link is also no longer displayed.

If I delete the contextual filter on my view, then the module-defined link is also shown.

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP