You are here

function taxtocomma_field_formatter_view in Taxonomy Formatter 7

Implements hook_field_formatter_view().

File

./taxonomy_formatter.module, line 122
adds a formatter for taxonomy terms with options to specify element type, wrapper type, and separators

Code

function taxtocomma_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $settings = $display['settings'];
  $element = array();
  $separator = $settings['separator_option'];
  if ($settings['element_option'] != '- None -') {
    $elementwrap[0] = '<' . $settings['element_option'] . ' class="' . $settings['element_class'] . '">';
    $elementwrap[1] = '</' . $settings['element_option'] . '>';
  }
  else {
    $elementwrap[0] = '';
    $elementwrap[1] = '';
  }
  if ($settings['wrapper_option'] != '- None -') {
    $wrapper[0] = '<' . $settings['wrapper_option'] . ' class="' . $settings['wrapper_class'] . '">';
    $wrapper[1] = '</' . $settings['wrapper_option'] . '>';
  }
  else {
    $wrapper[0] = '';
    $wrapper[1] = '';
  }
  $formatted = '';
  foreach ($items as $delta => $item) {
    $termid = $item['tid'];
    $term[] = $termid;
    $actterm = entity_load('taxonomy_term', $term);
    $uri = entity_uri('taxonomy_term', $actterm[$termid]);
    if ($settings['links_option']) {
      $formatted .= $elementwrap[0] . '<a href=\'/' . $uri['path'] . '\'>' . check_plain($actterm[$termid]->name) . '</a>' . $elementwrap[1] . $separator;
    }
    else {
      $formatted .= $elementwrap[0] . check_plain($actterm[$termid]->name) . $elementwrap[1] . $separator;
    }
  }
  $length = strlen($separator);
  $formatted = substr($formatted, 0, -$length);
  $formatted = $wrapper[0] . $formatted . $wrapper[1];
  $element[0]['#markup'] = $formatted;
  return $element;
}