2

In a view showing for example products (nodes) filtered by a taxonomy term (product type), I can easily set a contextual filter to select the product type term to be displayed.

Is there a way to get the value for a contextual filter in such a view from another node?

Usage example: Using the example above, I have a content type that presents a product type (taxonomy term) in depth with text, images etc. I want to specify a term in a reference field in the product type presentation and have a block that displays all product for the selected term.

Is that type of filter possible? I am guessing the same approach can be used in D7 as well as D8 if possible without using third party modules.

If my question is unclear I will be happy to elaborate my case further.

1 Answer 1

2

Views are complicated, so pardon me if i don't have this entirely right in the first go.

So as I understand things, you have one node that references a taxonomy term representing a product type, and you need to get a view on this node page to show other nodes (possibly of a different content type) that reference this same taxonomy term (possibly along with others). If that is correct, you should be able to do this with two relationships and a contextual filter. Both are found under the advanced settings portion of the view edit pane.

The first relationship you want to add is 'Entity Reference: Referenced Entity - A bridge to the Taxonomy term entity that is referenced via field_your_taxonomy_field'. This gets us from your 'result' nodes to the term you want to filter on.

The second relationship to add is 'Entity Reference: Referencing entity - A bridge to the Content entity that is referencing Taxonomy term via field_your_taxonomy_field_on_the_entity_we_are_viewing_the_view_from'. You set it up to use the first relationship as its relationship.

Last, you set up a contextual filter on 'Content: Nid' using the second relationship.

Edit
Since you are inside a block, you will also want to tell the view how to get the context (node ID) from the URL. Under the 'WHEN THE FILTER VALUE IS NOT AVAILABLE' you will want to select 'Provide default value' and in the resulting dropdown, select 'Raw value from URL'. Path component 1 should be correct. Save and update the view.
End Edit

To test this, pick the NID of a node where you might be viewing the view and put the NID in the 'Preview with contextual filters' box and click update preview. Did you see what you wanted to see? If so, great!

4
  • Although this is a good written answer, I am still having problems using the instructions. Perhaps this is working badly for me because I am using the view as a block and not as a page.
    – Henkealg
    Commented Apr 26, 2016 at 18:22
  • 1
    Not a problem. The next step is to ensure that the context is available inside the block. This link gives instructions, but I will edit my answer to reflect them: drupal.stackexchange.com/questions/27818/… Commented Apr 26, 2016 at 18:38
  • I can't see the 'Entity Reference: Referencing entity' on my Drupal website... do I need to install a module for this? Commented Mar 20, 2020 at 16:07
  • I answer myself: in Drupal 8.8.3 the title of this relationship is 'Content using field_whatever_is_your_entity_reference_field', under the Taxonomy Term category Commented Mar 20, 2020 at 16:10

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.