2

I have created a view with a contextual filter set to a taxonomy term. I am attempting to use the view block in a panels (page manager) page and pass the taxonomy term from the URL into the block. I could do this in Drupal 7 but I can't seem to figure it out in D8.

The page URL will be something like /reports/term1. I would like to pass term1 into the block.

For the panel page I have a URL like /reports/! (I also tried % as the place holder). Though I have seen some recommendations to use a context to pass the term, I have not been able to do so.

I am working with Drupal 8.7.1, Panels 8.x-4.4, Page Manager 8.4-4.0.

Any suggestions on how to accomplish this?

0

I have not used Panels with D8, but...

With D7 Panels I remember there was a way to set/pass a contextual filter value, but one thing I noticed was if I did not set it in Panels and instead set it directly in the View's settings, it also worked.

So all you have to do is edit your View block, under Advanced, for contextual filters add Has taxonomy term ID

enter image description here

In the settings for that, select Provide default value and for type use Taxonomy term ID from URL

enter image description here


Alternatively, you could do

enter image description here

| improve this answer | |
  • This does work with D7. However I can not get this to work in D8. – Finn Apr 13 at 6:06
  • You also tried with the alternative way, raw value from url and path component? – No Sssweat Apr 15 at 16:59
  • Yes, that will work in the view preview. However it won't work on the page manager/panel page. The issue seems to be there is no way to set a dynamic path in D8 page manager/panels pages. – Finn Apr 18 at 18:45
0

This answer doesn't have anything to do with panels, but it will work for generally setting $args (contextual filter values) for views.

function hook_views_pre_view(\Drupal\views\ViewExecutable $view, $display_id, array &$args) {

  // You'll likely be able to get your term ID from the current route and/or the current path

  $route = \Drupal::routeMatch(); // get the current route
  $params = $route->getParameters(); // get the names of any parameters on the route
  $your_param_val = $route->getParameter('your_param'); // gets the value of a param, given that it exists in $params

  $current_path = \Drupal::service('path.current')->getPath(); //gets the current path
  $path_components = explode('/', $current_path); // gives you an array of the path components

  switch ($view->id()) {
    case 'your_view_id': // the machine name of your view
      if ($display_id === 'block_1') { // the machine name for your display

        $args[0] = $tid; // will set the value of the first contextual filter to the value of $tid;
        $args[1] = $another_fun_value; // will set the value of the second contextual filter, etc
        break;
      }
  }
}

Then the settings for the Contextual Filter (show all results, hide view, view) will be used as the fallback if the above code doesn't set the value of the filter for whatever reason.

| improve this answer | |
  • Thank you for this answer. Do I need to put this code into a custom module to use it? – Finn Apr 14 at 13:14
  • Yes it needs to go in a module. You'd replace "hook" with the machine name of the module – Mrweiner Apr 14 at 15:36
  • Thank you, Mrweiner. I will try this and let you know how it works out. – Finn Apr 14 at 20:04
  • So far, I am not able to get this to work. Maybe the problem is that I am trying to use it with a page created in page manager. It appears that a path like "/reports/!" simply won't work with page manger (I have also tried %, $arg as a place holder). When I attempt to access the page I always get a page not found error. When I do not use a placeholder, the page will load but the arguments do not appear to be passed. – Finn Apr 15 at 2:58
  • Sounds like your issue is the page manager side of things and not the views side of things. If you're able to get that page to load at all, this snippet should help get the view to load up. For D7 at least, %arg was required and !arg was optional, and they had to be set up under Settings > Arguments for that page in page manager. I think there was some way to pass variables into views from page manager but never actually got that working. Sorry I can't offer more help on the Page Manager side of things, haven't worked with it for a long time – Mrweiner Apr 15 at 16:54
0

Finally figured out how to get this to work. The missing element was the correct way to add a placeholder to the path in D8 page manager and panels. The path needs to be something like /something/{args}. ! or % does not appear to work.

Then I added a page manager context (under the Variants section) of the data type "string". When adding this context I set the context value to "string."

page manager context addition form

In the view, I added a taxonomy term relationship and selected (within the relationship) the intended vocabulary. Then I added a contextual filter of the type Taxonomy term: name. Note, you must enter "name" in the contextual filter search box to find the correct filter.

enter image description here

Then I selected "Raw value from the URL", and in this case, path component 2. I also added a validator of "Taxonomy term name" to the view contextual filter.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.