3

I'm trying to display certain blocks in taxonomy term pages and the nodes associated based on the taxonomy term itself.

Let's say I have 3 taxonomy terms: Bologna, Milan and Rome.

When I'm in Bologna taxonomy-term page I would like to display a block that show the map of Bologna, when I'm in Milan taxonomy-term page the Milan map and so on.

Context module seems to work with node associated to a particular taxonomy term (for example, I'm able to display Milan map block in every node associated with Milan taxonomy term), but doesn't work for the taxonomy page itself.

I think the trick is in Panels. I've enabled the taxonomy-term page override but I do not know how to display blocks based on arguments.

I've used views and panels many times before but never faced this particular problem: It's a bit different that filtering with arguments particular nodes or content types.

2 Answers 2

0

Kropot,

Have you tried views contextual filters?

  1. Create a block view.
  2. Add a Contextual filters with Content: Has taxonomy term ID.
  3. defalt value: Taxonomy Term ID from URL
  4. Add the fields to display.
  5. Add the filters.

Deepak

6
  • Thank you for the answer @deepak-ml This works when you have to display a particular content based on taxonomy term ID. But how to do that when you want to display particular blocks based on taxonomy term? Block_A in taxonomy_term_A page, Block_B in taxonomy_term_B page, and so on (using Panels).
    – Kropot
    Commented Mar 19, 2013 at 13:22
  • how are the different blocks Block_A Block_B etc are related to the terms ?
    – Deepak ML
    Commented Mar 19, 2013 at 13:26
  • This is exactly the problem :) How to relate blocks with the terms? There is a similar question here I wasn't able to find a solution using Context module The block_term module seems to be in alpha state and it doesn't mention integrations with views.
    – Kropot
    Commented Mar 19, 2013 at 13:37
  • 1
    With a content type eg. relative_block with fields image and taxonomy reference ( same vocabulary as actual content type ) and actual content type where you reference the term. The views block will display the image field associated with that term.
    – Deepak ML
    Commented Mar 19, 2013 at 13:37
  • Yep, I was thinking at the same workaround: in my case, creating a content type called "Map" using the same vocabulary, create the content Map_A, Map_B and Map_C, then a views content pane wich displays only the body field, and then configure as usual Contextual filters and arguments in panels. Maybe we can write down a workaround :)
    – Kropot
    Commented Mar 19, 2013 at 13:41
0

As per the previous answer what you may consider using Views with context filter which will filter the content based on the taxonomy term page you're in.

But if you just want to get the page context to pass as an argument to the map (as per your example in the comment) then you may want to implement hook_preprocess_block() and get the taxonomy term id there

function <MODULE_NAME>_preprocess_block(array &$variables): void {
      //check if is a taxonomy page 
      if (\Drupal::routeMatch()->getRouteName == 'entity.taxonomy_term.canonical') 
      {
        //get Taxonomy Term id and pass it to the block
        $variables['term_id'] = \Drupal::routeMatch()->getRawParameter('taxonomy_term');
      }
}

This example would get the term id accessible to your block twig template and you could then pass it as an argument to your map constructor.

Obviously if you want to just show different blocks for different terms you can just set the path in the block settings (/admin/structure/block/ -> go to your block settings (/admin/structure/block/manage/<block_id>) and specify the term id(s) in the visibility settings on the pages tag.

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.