Community

Always getting the root term from a taxonomy

Hi:

Using Drupal 7, I've created a vocabulary that looks like this:

  • Section A
    • Section A-Group 1
    • Section A-Group 2
      • Section A-Group 1-Subgroup (a)
      • Section A-Group 1-Subgroup (b)
      • Section A-Group 1-Subgroup (c)
    • Section A-Group 3
  • Section B
  • Section C

I modify the content type so I have a Term Reference field tied to this vocabulary that is required and restricted to one choice. All is well and good.

I now create three articles as follows

Article 1 with a choice of "Section A" in the term reference field
Article 2 with a choice of "Section 1-Group 2" in the term reference field
Article 3 with a choice of "Section 1-Group 2-Subgroup (c)" in the term reference field. All is still well and good.

Now I want to create a table view that will give me a table that lists the title of the article and the top most term in the vocabulary. It should look something like this.

TITLE VOCABULARY
Article 1 Section A
Article 2 Section A
Article 3 Section A

I've added the fields in the view, but the only "Taxonomy Term" fields I can see to add to the view is Taxonomy Term: Name

I kind of expected to see "Taxonomy Term: Parent" and "Taxonomy Term: Root".

Help please!

Kevin

Comments

Kind of surprised

Four days without a response?

Sorry if this is an obvious question but I haven't been able to figure it out.

Thanks in advance for any help.

Kevin

This answer is a bit late in

This answer is a bit late in coming, but I came across your post a few minutes ago while I was searching for how to accomplish the same-ish task. Here's a solution.

  1. Identify the tid for the article:
    $wrapper = entity_metadata_wrapper('node', $node);
    $site = $wrapper->field_site->value()->tid;

    In my case, I had a entity ref to a taxonomy term called "field_site".
  2. Gather all the parent terms:
    $parents = taxonomy_get_parents_all($site);
  3. Get the last parent (which is the root)
    $parent = end($parents)->name;

Of course, the $parent var contains the tid, vid, description and other properties too.