1

Say, I have taxonomy vocabulary 'fruit' with terms like 'apple', 'pear' and 'grape'. Through content type 'Healthy', 'Healthy' content is made using a entity reference field to choose a term from the 'fruit' vocabulary.

Now, on a page with path /food/good/apple, a views block is placed that should show content related to taxonomy term 'apple'. I've tried to make a contextual filter in the views block like this:

When the filter value is NOT available > Provide default value > 'raw value from URL' And for 'Path Component', I checked 3 (for the third position in the url, 'apple'). This however doesn't return any content. When I remove the contextual filter, the block shows all Fruit content items.

Can this even be accomplished without using contrib modules? Any help on how to get the right setup would be welcome!

1 Answer 1

1

Your contextual filter will be expecting the term ID rather than the term name. You'll need remove that contextual filter and then add a relationship to the taxonomy which will then allow you to add a contextual filter for the taxonomy term name rather than the term ID

5
  • Thanks for your answer, I tested this without result: I removed the old filter and added the required relation to the taxonomy term entity reference field for the Fruit vocabulary. Then added a contextual filter on that field for the taxonomy term name, again with the settings: When the filter value is NOT available > Provide default value > 'raw value from URL' And for 'Path Component', I checked 3 (for the third position in the url, 'apple'). Did I do something wrong?
    – C.A. Vuyk
    Oct 9, 2019 at 12:03
  • To debug, I turned on 'show the query' setting, what I found remarkable is that the query uses the 'food' part from the path instead of 'apple'. SELECT node_field_data.created AS node_field_data_created, node_field_data.nid AS nid FROM {node_field_data} node_field_data LEFT JOIN {node__field_fruit} node__field_fruit ON node_field_data.nid = node__field_fruit.entity_id AND node__field_fruit.deleted = '0' WHERE ((node__field_fruit.field_fruit_target_id = 'food')) AND ((node_field_data.status = '1') AND (node_field_data.type IN ('healthy')))
    – C.A. Vuyk
    Oct 9, 2019 at 12:10
  • Is that MYSQL snippet after you've done the changes I suggested? As it doesn't look like it because it has no JOIN to the taxonomy_term tables and is still using the field "target_id" in the where clause which will be the term ID. What's ive said should work fine: 1)add a relationship to your taxonomy term reference field 2) add a contextual filter for "Taxonomy term: Name". Is this for a views page? What have you set the url to be for your page?
    – Leigh
    Oct 9, 2019 at 12:24
  • Sorry, the snippet was old. After doing the settings this is the query: LEFT JOIN {node__field_fruit} node__field_fruit ON node_field_data.nid = node__field_fruit.entity_id AND node__field_fruit.deleted = '0' INNER JOIN {taxonomy_term_field_data} taxonomy_term_field_data_node__field_fruit ON node__field_fruit.field_themes_target_id = taxonomy_term_field_data_node__field_fruit.tid WHERE ((taxonomy_term_field_data_node__field_fruit.vid = 'food')) AND ((node_field_data.status = '1') AND (node_field_data.type IN ('healthy'))) It is for a views block, URL /food/good/apple
    – C.A. Vuyk
    Oct 9, 2019 at 12:39
  • When searching in the setup of contextual filters on 'Taxonomy', I couldn't find 'name' so I think last setup was wrong. Searching on 'name' helped. I think I've set it up like you said, but it doesn't show any output. Because it is a block, I still have to get the name from the URL, and the query shows searching in the wrong part ('food' instead of 'apple').
    – C.A. Vuyk
    Oct 9, 2019 at 13:01

Your Answer

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

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