You are here

taxonomy_formatter.module in Taxonomy Formatter 7

Same filename and directory in other branches
  1. 8 taxonomy_formatter.module

adds a formatter for taxonomy terms with options to specify element type, wrapper type, and separators

File

taxonomy_formatter.module
View source
<?php

/**
 * @file
 * adds a formatter for taxonomy terms with options to specify element type, wrapper type, and separators
 *
 */

/**
 * Implements hook_field_formatter_info().
 */
function taxtocomma_field_formatter_info() {
  return array(
    'taxonomy_term_reference_csv' => array(
      'label' => t('Comma Separated'),
      'field types' => array(
        'taxonomy_term_reference',
      ),
      'settings' => array(
        'links_option' => FALSE,
        'separator_option' => ', ',
        'element_option' => '- None -',
        'wrapper_option' => '- None -',
        'element_class' => '',
        'wrapper_class' => '',
      ),
    ),
  );
}

/**
 * Implements hook_field_formatter_settings_form().
 */
function taxtocomma_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['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',
    ),
  );
  $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',
      '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 taxtocomma_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 "') . $settings['separator_option'] . '"';
  if ($settings['links_option']) {
    $summary .= t('<br>The terms will link to the term pages');
  }
  if ($settings['element_option'] != "- None -") {
    $summary .= "<br>Elements will be wrapped in a " . $settings['element_option'] . " tag";
    if (!empty($settings['element_class'])) {
      $summary .= " with the class of " . $settings['element_class'];
    }
  }
  if ($settings['wrapper_option'] != "- None -") {
    $summary .= "<br>The entire list will be wrapped in a " . $settings['wrapper_option'] . " tag";
    if (!empty($settings['wrapper_class'])) {
      $summary .= " with the class of " . $settings['wrapper_class'];
    }
  }
  return $summary;
}

/**
 * Implements hook_field_formatter_view().
 */
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;
}