You are here

function taxonomy_image_field_formatter_view in Taxonomy Image 7

Implements hook_field_formatter_view().

File

./taxonomy_image.module, line 136
Implements a field formatter that can display image on referenced taxonomy terms.

Code

function taxonomy_image_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  switch ($display['type']) {
    case 'taxonomy_image_term_reference_image':
      foreach ($items as $delta => $item) {

        // Display the term name when the term has just been created.
        if ($item['tid'] == 'autocreate') {
          $element[$delta] = array(
            '#markup' => check_plain($item['name']),
          );
        }
        else {
          $term = $item['taxonomy_term'];
          $alt = $title = $term->name;
          $uri = entity_uri('taxonomy_term', $term);
          $item = field_get_items('taxonomy_term', $term, $display['settings']['field_name']);

          // Display the image if it exists.
          if (!empty($item)) {
            $image = reset($item);

            // Get HTML for the image.
            $variables = array(
              'path' => $image['uri'],
              'alt' => empty($image['alt']) ? $alt : $image['alt'],
              'title' => empty($image['title']) ? $title : $image['title'],
            );
            if (empty($display['settings']['image_style'])) {
              $markup = theme('image', $variables);
            }
            else {
              $variables['style_name'] = $display['settings']['image_style'];
              $markup = theme('image_style', $variables);
            }
            if (empty($display['settings']['linked'])) {
              $element[$delta] = array(
                '#markup' => $markup,
              );
            }
            else {
              $element[$delta] = array(
                '#type' => 'link',
                '#title' => $markup,
                '#href' => $uri['path'],
                '#options' => array(
                  'html' => TRUE,
                  'attributes' => array(
                    'title' => !empty($image['title']) ? $image['title'] : $title,
                  ),
                ) + $uri['options'],
              );
            }
          }
          elseif (!empty($display['settings']['text_alt'])) {
            if (empty($display['settings']['linked'])) {
              $element[$delta] = array(
                '#markup' => check_plain($term->name),
              );
            }
            else {
              $element[$delta] = array(
                '#type' => 'link',
                '#title' => $term->name,
                '#href' => $uri['path'],
                '#options' => $uri['options'],
              );
            }
          }
        }
      }
      break;
  }
  return $element;
}