<?php
/**
 * @file
 * adds a formatter for taxonomy terms with options to specify element type, wrapper type, and separators
 *
 */
/**
 * Implements hook_field_formatter_info().
 */
function taxonomy_formatter_field_formatter_info() {
  return array(
    'taxonomy_term_reference_csv' => array(
      'label' => t('Delimited'),
      'field types' => array('taxonomy_term_reference'),
      'settings'  => array(
        'links_option' => FALSE,
        'append_vocabulary' => FALSE,
        'separator_option' => ', ',
        'element_option' => '- None -',
        'wrapper_option' => '- None -',
        'element_class' => '',
        'wrapper_class' => '',
      ),
    ),
  );
}
/**
 * Implements hook_field_formatter_settings_form().
 */
function taxonomy_formatter_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $element = array();
  $element['links_option'] = array(
    '#type'           => 'checkbox',
    '#title'          => t('Links'),
    '#description'    => t('When checked terms will be displayed as links'),
    '#default_value'  => $settings['links_option'],
  );
  $element['append_vocabulary'] = array(
    '#type'           => 'checkbox',
    '#title'          => t('Append Vocabulary'),
    '#description'    => t('When checked the name of the vocabulary that a term is from will be appended to the term name.'),
    '#default_value'  => $settings['append_vocabulary'],
  );
  $element['separator_option'] = array(
    '#type'           => 'textfield',
    '#title'          => t('Separator'),
    '#description'    => t('The separator to use, including leading and trailing spaces'),
    '#default_value'  => $settings['separator_option'],
  );
  $element['element_option'] = array(
    '#type'           => 'select',
    '#title'          => t('Element'),
    '#description'    => t('The HTML element to wrap each tag in'),
    '#default_value'  => $settings['element_option'],
    '#options'        => array(
      '- None -'  => '- None -',
      'span'      => 'span',
      'h1'        => 'h1',
      'h2'        => 'h2',
      'h3'        => 'h3',
      'h4'        => 'h4',
      'h5'        => 'h5',
      'strong'    => 'h6',
      'em'        => 'h7',
      'li'        => 'li',
    ),
  );
  $element['element_class'] = array(
    '#type'           => 'textfield',
    '#title'          => t('Element Class'),
    '#description'    => t('The class assigned to the element'),
    '#default_value'  => $settings['element_class'],
  );
  $element['wrapper_option'] = array(
    '#type'           => 'select',
    '#title'          => t('Wrapper'),
    '#description'    => t('The HTML element to wrap the entire collection in'),
    '#default_value'  => $settings['wrapper_option'],
    '#options'        => array(
      '- None -'  => '- None -',
      'div'       => 'div',
      'span'      => 'span',
      'h1'        => 'h1',
      'h2'        => 'h2',
      'h3'        => 'h3',
      'h4'        => 'h4',
      'h5'        => 'h5',
      'p'         => 'p',
      'ul'        => 'ul',
      'ol'        => 'ol',
      'strong'    => 'strong',
      'em'        => 'em',
      ),
  );
  $element['wrapper_class'] = array(
    '#type'           => 'textfield',
    '#title'          => t('Wrapper Class'),
    '#description'    => t('The class assigned to the wrapper'),
    '#default_value'  => $settings['wrapper_class'],
    );
  return $element;
}
/**
 * Implements hook_field_formatter_settings_summary().
 */
function taxonomy_formatter_field_formatter_settings_summary($field, $instance, $view_mode) {
  $display = $instance['display'][$view_mode];
  $settings = $display['settings'];
  $summary = t('The Terms will be displayed separated by "@separator"', array('@separator' => $settings['separator_option']));
  if ($settings['links_option']) {
    $summary .= t('<br>The terms will link to the term pages');
  }
  if ($settings['append_vocabulary']) {
    $summary .= t('<br>The vocabulary will be displayed');
  }
  if ($settings['element_option']!="- None -") {
    $summary .= t('<br>Elements will be wrapped in a "@element" tag',  array('@element' => $settings['element_option']));
    if (!empty($settings['element_class'])) {
      $summary .= t(' with the class of @elemclass', array('@elemclass' => $settings['element_class']));
    }
  }
  if ($settings['wrapper_option']!="- None -") {
    $summary .= t('<br>The entire list will be wrapped in a "@wrapper" tag', array('@wrapper' => $settings['wrapper_option']));
    if (!empty($settings['wrapper_class'])) {
      $summary .= t(' with the class of @wrapclass', array('@wrapclass' => $settings['wrapper_class']));
    }
  }
  return $summary;
}
/**
 * Implements hook_field_formatter_view().
 */
function taxonomy_formatter_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $settings = $display['settings'];
  $element = array();
  $separator = check_plain($settings['separator_option']);
  if ($settings['element_option']!='- None -') {
    $elementwrap[0] = '<' . $settings['element_option'] . ' class="' . check_plain($settings['element_class']) . '">';
    $elementwrap[1] = '</' . $settings['element_option'] . '>';
  }
  else {
    $elementwrap[0] = '';
    $elementwrap[1] = '';
  }
  if ($settings['wrapper_option']!='- None -') {
    $wrapper[0] = '<' . $settings['wrapper_option'] . ' class="' . check_plain($settings['wrapper_class']) . '">';
    $wrapper[1] = '</' . $settings['wrapper_option'] . '>';
  }
  else {
    $wrapper[0] = '';
    $wrapper[1] = '';
  }
  $formatted = array();
  // We convert the items array into an array of terms (keyed by delta).
  $tids = array();
  foreach($items as $tidarray){
    $tids[] = $tidarray['tid'];
  }
  $terms = entity_load('taxonomy_term', $tids);
  foreach ($items as $delta => $item) {
    if(isset($terms[$item['tid']])) {
      $link_text = $terms[$item['tid']]->name;
      if ($settings['append_vocabulary']){
        $vocabularies = &drupal_static(__FUNCTION__, array());
        if(!isset($vocabularies[$terms[$item['tid']]->vid])){
          $vocabularies[$terms[$item['tid']]->vid] = taxonomy_vocabulary_load($terms[$item['tid']]->vid);
        }
        $link_text .= ' ('.$vocabularies[$terms[$item['tid']]->vid]->name.')';
      }
      if ($settings['links_option']) {
        $uri = entity_uri('taxonomy_term', $terms[$item['tid']]);
        $formatted[] = $elementwrap[0] . l($link_text, $uri['path'], $uri['options']) . $elementwrap[1];
      }
      else {
        $formatted[] = $elementwrap[0] . check_plain($link_text) . $elementwrap[1];
      }
    }
  }
  if(count($formatted)){
    $element[0]['#markup'] = $wrapper[0] . implode($separator, $formatted) . $wrapper[1];
  }
  return $element;
}