Is it possible to get taxonomy term from url and node id at the same time from current node? I need to get taxonomy term from current node to pull some date from referenced nodes that have parent taxonomy of current node term. So far ok. But moreover I need to be able to get to current node's fields. Using another conextual filter to get NID was not helpful.
-
you should be able to use the 'provide default value' feature to provide one or both of them– JimajammaCommented Apr 25, 2013 at 0:02
-
@Jimajamma I am using 'provide default value' for both of contextual filters - NID as well as 'content has taxonomy term with depth' but it seems like NID argument puts focus solely on current node thus disabling the ability to pull data from other nodes available using 'content has taxonomy term with depth'– loparrCommented Apr 25, 2013 at 0:11
-
that's the issue... if you are sitting on node/###, Views can grab the nid quite obviously. It can also grab any taxonomy information from that node as well. However, if you are sitting anywhere else, there is no nid, eg, if you are on taxonomy/term/### where there is a tid... and you've lost me on how to pull data from other nodes as you can only be sitting on one at a time ;0– JimajammaCommented Apr 25, 2013 at 4:41
1 Answer
I don't think you can do this with multiple contectual filters.
Sounds like you need relationships.
Use the contextual filter to get the current nid and show the fields for that node.
Then add a relationship between the node and its terms (you can limit this by vocab if need be) by adding the relationship "Content: Taxonomy terms on node".
Then add a relationship from the term to the parent term by add the "Taxonomy term: Parent term" relationship. This relationship should use the previous relationship.
The add a relationship from the parent term to the content that uses that term by adding the " Taxonomy term: Content with term" relationship. This relationship should use the Parent relationship.
Then you can do extra things like adding filters that use these relationships.
All these relationships can have the "Require this relationship" checkbox checked.
With some relationships you can end up with duplicate results. One way to try to avoid this is go in the "Query options" in the "Advanced" part of the views configuration and check the "Distinct" checkbox.
Relationships can be confusing and they are hard to explain, but if you play around with them, once you understand them they are very useful.
Extra question To answer your extra question of whether you can display the value of one field if it is there and if it is not then show another field in its place:
Yes, you can do that. Depending on your requirements there are a couple of ways it can be done:
- Exclude the secondary field from display and for the primary field check "Rewrite the output of this field" and use the tokens to print the primary and secondary fields out next to each other. This solution will only work if only one or the other of your fields will have a value. If there is a chance that both fields will have a value you will end up with both values printing next to each other.
- Exclude the secondary value from display. Then in a custom module implement hook_view_pre_render() and you can check the $view->result to see if the primary field has a value. If it doesn't you can replace the empty value with the value of the secondary field.
- Override the template for views_view_fields.tpl.php so that if the primary field is empty you print the secondary one. And don't print the secondary one. This could also be handled in the preprocessor function template_preprocess_views_view_fields() in your theme's template.php file.
Unfortunately I don't have time to give code examples right now.
-
hi, your solution works. To add little bit more on top: do you think it would be possible if current node tries to display fields (that I am pulling from referenced nodes with parent terms) from itself first? I would like to display fields from current node and if not present then display fields from referenced nodes with parent term. Thank you– loparrCommented Apr 25, 2013 at 23:32
-