2

I'm using a view to override my taxonomy term pages to display content fields. One of those fields is another taxonomy term (applicable divisons). Displaying the titles of terms applied to the content is no problem (multiple field settings).

The problem comes in here. I need to display icons that represent each of those terms that is an image field in the term itself (division icon). I added a relationship to the view (Content:Taxonomy Terms on node). I can then add the field, but it limits it to one of the terms/icons and it doesn't have a "multiple field settings" area.

I've searched for this quite a while and it's probably an obvious answer, but it's one I can't find or figure out.

Answer: This isn't pretty, but it's getting me the data I want. It needs to be cleaned up. Thanks for the help!

<?php
foreach($data->field_field_applicable_divisions as $mydata) {
    if (isset($mydata['raw']['tid'])) { 
        $term = taxonomy_term_load($mydata['raw']['tid']);
        echo "<div class='term_icon'>";
        echo "<img src='" . image_style_url( 'icon', $term->field_division_icon['und'][0]['uri']) . "' title='" . $term->field_division_icon['und'][0]['field_file_image_alt_text']['und'][0]['value'] . "' alt='" . $term->field_division_icon['und'][0]['field_file_image_alt_text']['und'][0]['value'] . "'class='term-image' />";
        echo "</div> ";
    }
}
?>

Also, to get a legend in the header or footer:

<?php
foreach(taxonomy_get_tree(7) as $icon) {
    $term = taxonomy_term_load($icon->tid);
    echo "<div class='term_icon'>";
    echo "<img src='" . image_style_url( 'icon', $term->field_division_icon['und'][0]['uri']) . "' title='" . $term->field_division_icon['und'][0]['field_file_image_alt_text']['und'][0]['value'] . "' alt='" . $term->field_division_icon['und'][0]['field_file_image_alt_text']['und'][0]['value'] . "'class='term-image' /> - " . $term->field_division_icon['und'][0]['field_file_image_alt_text']['und'][0]['value'];
    echo "</div> ";
}
?>

1 Answer 1

0

You are going to have to adjust this to your own use case, but the approach I would use is a little different.

First, delete the relationship.

Add (or configure) the term reference field (field_divisions?) in your query. Set it to no label, and exclude from display. Rewrite the output to [field_tags-tid] . This will give you a list of term id's.

Install and enable the Views PHP module.

EDIT:

The $row variable does not play well with multiple values, so we will switch to the $data variable.

In your view, add a Global:PHP field, and in the Output code box enter something like:

<?php
  // Select your field object.
  $obj = $data->_field_data;
  // Then get your field array
  $divisions = $obj->field_divisions[LANGUAGE_NONE]
  foreach($divisions as $division) {
    if ( is_numeric( $division['tid'] ) ) {
      $term = taxonomy_term_load($division['tid']);
      $icons= $icons. "<div class='term-icons'>";

      if (isset($term->division_icon['und'][0]['uri'] )) {
        $icons= $icons. "<div class='term_icon'><img src='" . image_style_url( 'icon_style', $term->division_icon['und'][0]['uri']) . "' class='term-image' /></div> ";
      }  
      $icons = $icons . "</div>";
    }
  } 
  print $icons;
?>

Make sure the PHP field follows the term reference field so the $row values are available to the PHP field. Then style your icons to display as you wish.

2
  • I've deleted the relationship. I've changed the output in field_applicable_divisions to the token and modified your code to for the field name. In the PHP field, field_applicable_divisions shows up in available variables. I'm getting an "invalid argument supplied to the foreach." Any specific settings in the field_applicable_divisions that would mess this up? Multiple field settings perhaps? Thanks for the help. Mar 21, 2013 at 19:25
  • I found the problem. Slightly more involved code, but the above should get you going.
    – Triskelion
    Mar 21, 2013 at 20:27

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.