1

I have created a view block for a content type of card. The card content type also has a taxonomy vocabulary where users can add new terms to that taxonomy vocabulary. I have a contextual filter on this view block which filters by taxonomy ID. And this works in the views preview pane, e.g. I enter 1 and cards with the taxonomy term ID 1 are rendered in the preview.

Screenshot of Views interface

Screenshot of contextual filter

However, when I use Layout Builder and select the view block, I do not get an option to input what taxonomy term ID I want to use for the block. What am I doing wrong?

Screenshot of Layout Builder block selection

6
  • If you configured the contextual filter in view block once, then why this contextual filter needs to configured in block , I believe it should work w.r.t contextual filter configured in the view block, what issue you are facing here Nov 21, 2023 at 14:33
  • 1
    I'm fairly sure my understanding of how to use contextual filters are flawed, but this is the use-case. I have a content type called card, and it includes a taxonomy term field. I would like to create a view block that shows only cards that have specific taxonomy terms. I thought using contextual filters, I could choose which term is being used, then place that block on a page with layout builder.
    – huijing
    Nov 22, 2023 at 7:29
  • There are two different types of filter in views : filter criteria & contextual filter, you can follow this documentation drupal.org/docs/8/core/modules/views/… to understand more about contextual filter Nov 22, 2023 at 7:50
  • +1 I agree @huijing - there should be a field in the configuration in layout builder to add the contextual filter value(s). Simple as that. Mar 5 at 10:23
  • @huijing take comfort in that others have this same issue and are also trying to find solutions with possibly some success, as do I. drupal.org/project/drupal/issues/3255977 One definitely should be able to pass parameters,i.e. contexual filters, to a block which is a Views display. Otherwise their usefulness (and of layout builder itself is very limited). Specifically look at this solution in that above issue: drupal.org/project/drupal/issues/3255977#comment-15018513 Mar 5 at 10:30

1 Answer 1

0

Pass value in contextual filter programmatically : using hook_views_pre_view(): (can add either .theme/.module file)

use Drupal\views\ViewExecutable;

function Mythemename/Mymodulename_views_pre_view(ViewExecutable $view, $display_id, array &$args) {

  if ($view->id() == 'view_machine_name' && $display_id == 'view_display_machine_name') {
      $node = \Drupal::routeMatch()->getParameter('node');
      if ($node instanceof \Drupal\node\NodeInterface) {
        // get taxonmoy field value from current node page
        $tid = $node->get('field_taxonomy')->target_id;
        if(!empty($tid)){
          $args[0] = $tid;
        }
      }  
  }
}
8
  • ok, this seems closer to what i want to achieve. but i'm a little confused about which ID is the filter criteria. let's say my page is node/16, but i want the cards of taxonomy term ID 2 to be displayed, how would the dynamic value from URL work here?
    – huijing
    Nov 22, 2023 at 10:43
  • I have added steps 1-3 in ans how can use fixed value in contextual filter Nov 22, 2023 at 11:11
  • does this mean that i will have to define new blocks every time i have a new taxonomy term? i think this ties back to my original question of whether it would be possible to pass a value from layout builder to the contextual filter in the block. seems like that is not possible?
    – huijing
    Nov 22, 2023 at 13:15
  • Let say /node/1 , in it's taxonomy field has value 2, then you want to taxonomy id = 2 in contextual filter, similarly if editor update taxonomy id = 3 in /node/1 , for this particular scenario you want to contextual filter and pass taxonomy value i.e in the node, confirm Is this what you want? Nov 22, 2023 at 13:38
  • yes. so to rephrase, i would like to reuse the same views block on different pages, but with the option to select what the taxonomy ID should be on different pages.
    – huijing
    Nov 22, 2023 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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