1

This is a bit confusing for me.

I am using Drupal 7 and Views 3.

I have a list of particular content type nodes tagged with a term (from a vocabulary "topic"). Vocabulary "topic" is 2 levels deep and nodes are tagged just with leaf terms. I have those same nodes tagged with another vocabulary ("level" vocabulary).

I'd like to get in a view all parent terms (exactly the term name) from the vocabulary "topic" whose children are tagged into a node which is tagged with a particular level (let's say beginners).

Let me show you an example:

Vocabulary "Level":

  • Beginners
  • Intermediate
  • Advance

Vocabulary "Topic":

  • Greek philosophy
    • Introduction to greek philosophy
    • First stage: Greek
    • Second stage: Greek
  • Latin philosophy
    • Introduction to latin philosophy
    • First stage: Latin

My content type nodes can be tagged with both vocabularies but in the case of "topic" they are only allowed to be tagged, as I said, with leaf nodes (i.e. Introduction to greek philosophy, first stage:greek, second stage: greek, Introduction to latin philosophy or first stage:latin).

In this context if one particular node is tagged with "introduction to greek philosophy" and "beginners", I would get "Greek philosophy" on my view. If that same node is not tagged with "beginners" I wouldn't get it on my view.

I know it's a bit complex but I would really appreciate your help and orientation.

Thanks in advance.

0

OK, so this solution has many steps, but the good news is that this is possible.

Here's what you need to do in your view:

  • In your view that lists your nodes, add a relationship to the topic term reference field (called field_topic) in my case. In the 'Advanced' section, add the 'Content: Topic' field, and rename it if you want in the configuration screen. As you can see, I did!

enter image description here

  • Next, add one more relationship (the parent term of this Topic term). I'm filtering to only see term fields, and choose the 'Taxonomy term: Parent term' option. Make sure, when you configure this second relationship, to choose that you want the Parent term of the already selected Topic term field.

enter image description here

enter image description here

  • If you want - at this point you can select the "Taxonomy term: Name" field twice in your fields list (once with the 'Topic term' as the relationship and once with the 'Parent term' as the relationship to see that your two relationship are bringing up the correct terms).

  • Finally, you're ready to set up a contextual filter! What you want is for this view (which I assume is a block view) to pull up only those nodes that share the same topic parent with the current node's topic parent. Sooooo, in your Contextual filters section (under 'Advanced'), choose the "Taxonomy term: Term ID" option. Under the 'Relationship' choose, 'Parent topic' (or whatever you named your parent term relationship). After all, we'll be filtering exactly by that - the term id of the parent of the topic term.

  • Choose to 'Provide a default' value for this filter when it's not available. The truth is, we have to get the parent term id from the current node ourselves (I don't know how else to do it). Choose that we'll provide it with 'PHP Code' and type in the following:

    $node = menu_get_object();  // Get current node
    $topic_term_id = $node->field_topic['und'][0]['tid']; // Just gets first topic term
    $parents = taxonomy_get_parents($topic_term_id);
    reset($parents); // Make sure pointer points to first element
    return(key($parents)); // Return first key
    

Here's what my contextual filter looks like: enter image description here

Obviously, I'm not really doing many checks in my code to test for the existence of terms, etc, but this should just about get you there. Let us know if it works. I just tested it and it works for me!!!

  • Boriana, thank you very very much for your great help. I was playing around with relationships as well but I wasn't able to reach the parent term from the term itself. Using the first relationship as a base for the second one gave me the clue. I will play a bit more with all this new ideas and hope everything make sense for me. Could you recommend some good tutorials about this? By the way, I am using panels, and my view is a content pane. But I just needed the first 3 steps. :-) – José L. May 8 '13 at 22:38
  • Boriana I get my results duplicated, as different nodes might have same parent, and I'm getting one line (with the parent topic) per node. Can I prevent this? – José L. May 8 '13 at 22:57
  • Hey Jose, can you be more specific? I'm actually not sure what is being duplicated... If different nodes have the same parent, don't you want them both to be listed in the view as related nodes? Is your view a 'Nodes list' view? – Boriana Ditcheva May 8 '13 at 23:48
  • I'm using this approach to build an index and insert in into a menu and into a page (using panels). Not sure if it's the best way to go... Anyway, the final view (the index) will have a list of "parent" topics which "owns" content nodes (content nodes are my content type for writing about the topic itself) tagged with a particular level. I'm not using contextual filters in this case as the view is just created for one particular level (beginners in our example). I hope everything makes sense. :-) – José L. May 9 '13 at 7:14
  • Hmmm, actually, I'm not sure I follow. I used this sentence of yours as a guide: "If that same node is not tagged with "beginners" I wouldn't get it on my view.", and so built my view as a view of nodes, not terms. I thought you want to list nodes with the same parent topic when looking at a particular node. If that's not the case, I'm not sure how to help more exactly. Maybe someone else can chime in. Sorry! – Boriana Ditcheva May 9 '13 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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