Hi all,
I've been getting really happy with using Views when theming on Drupal.
I've spend all day trying to figure this out though:
I'd like my view (block) to show all 12 possible taxonomy terms in my vocabulary names 'months'.
the terms I have selected (say: jan, feb, march..) for my node should have a link in them to the term list of that particular month.
The output should be like this:

jan
feb
march
june
july
aug
sept
oct
nov
dec

I've been trying to figure out what to write inside my views-view-field-blockname.tpl.php but I simply can't get my head around it, so hoping you can help me out :)

Have a nice day!

Comments

bjmagar1906’s picture

Hello magnus_n ,

I think you do not need to touch any template file to achieve it. You can create the list of taxonomy term using views UI itself. By setting "Show" to "Taxonomy terms" and setting "of type" to your "months" vocabulary while creating views for first time will generates the list of term links.

magnus_n’s picture

Hi,
to my knowledge that delivers a list of links to the taxonomy terms.
I would like it to only make links to the taxonomy terms that the node has attached. :)

The taxonomy is set by a field with with check marks so it's all stored in a variable I'm guessing, and needing som kind of if or foreach statement I can't figure out :/

bjmagar1906’s picture

umm.. still no need to do any foreach :) .

- Add a relationships "Taxonomy term: Content with term" and make sure you check "Require this relationship" option
- Again in Advance setting, click on Query Setting link and check distinct option and save the views.

It should now populate only terms attached to nodes.

magnus_n’s picture

That's cool - and thanks!
It's not totally what I want. (I got to this myself when trying)
I want the all the terms outputted in a list, but the ones where the term is
connected to the node, should be wrapped in a class or have a link associated to it.

magnus_n’s picture

http://i.imgur.com/8gISiM2.png

I tried illustrating my idea here to clarify.
My problem so far is that I can't get all tags in the vocabulary AND get the terms for the node only.
with the solution posted so far, I manage to highlight BOTH node-1's and node-2's terms in the block..

I've once again spent a whole day on this and crossing my fingers there's a nice easy solution to this :)

magnus_n’s picture

It looks like I need something like an EntityQuery where I can compare each term against the node's terms,
and then either print the term with or without a link wrapped around.
Can anyone help me out here?

magnus_n’s picture

Okay, I couldn't wait.. starting to get gray hairs but I managed to make some code that works for me here :)
It's drupal 7 and the code is used in a node.tpl.php if anybody needs to use it:

<?php
  $items = field_get_items('node', $node,'field_name'); //get field values
  
  if($items){
    $nodeterms; $i = -1;
    
    foreach($items as $val){
      $i++;
      $nodeterms[$i] = (int)$val['tid']; //id's are now in an array, ready to be compared with the vocabulary..
    }
    
    $query = new EntityFieldQuery();//prepare query for terms in vocabulary 7 = "months"
  
    $query->entityCondition('entity_type', 'taxonomy_term');
    $query->propertyCondition('vid', 7); //again 7 is the vic - change it to your needs
    $query->propertyOrderBy('tid', 'ASC');
    $result = $query->execute();
  
    print "<ul id=\"saeson\">";
    foreach($result['taxonomy_term'] as $key => $value){
      print "<li>";
      $i = 1;
      $iterations = count($nodeterms); //check for value per node term
      foreach($nodeterms as $nodetid){
        $taxonomy = taxonomy_term_load($key);
        $taxonomy_term_url = l($taxonomy->name[0], 'taxonomy/term/'. $nodetid, array('attributes' => array('class' => 'active', 'title' => 'se other posts from ' . $taxonomy->name))); //month names only first letter, so you can strip the "[0]"
        
        if($key==$nodetid){
          print $taxonomy_term_url;
          break;
          
        } else if ($i >= $iterations){
          print $taxonomy->name[0]; 
          break;
        }
        
        $i++;
      }
      print "</li>";
    }
    print "</ul>";
  }              
?>