You are here

function cshs_field_formatter_view in Client-side Hierarchical Select 7

Implements hook_field_formatter_view().

File

./cshs.formatter.inc, line 108
All implementations of field formatters.

Code

function cshs_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];
  if ($display['type'] == 'cshs_term_reference_hierarchical_text') {
    foreach ($items as $delta => $item) {
      $linked = isset($settings['linked']) && $settings['linked'];
      $separator = drupal_strlen(trim($settings['separator'])) == 0 ? ' ' : $settings['separator'];

      // Load the term.
      $term = taxonomy_term_load($item['tid']);

      // Get the parents.
      $parents = _cshs_get_parents($term);

      // Build the output string.
      $terms = array();
      foreach ($parents as $parent) {
        $terms[] = $linked ? l($parent->name, "taxonomy/term/{$parent->tid}") : check_plain($parent->name);
      }
      $terms = implode($separator, $terms);
      $element[$delta]['#markup'] = $terms;
    }
  }
  if ($display['type'] == 'cshs_term_reference_group_by_root') {
    $linked = isset($settings['linked']) && $settings['linked'];
    $separator = drupal_strlen(trim($settings['separator'])) == 0 ? ' ' : $settings['separator'];

    // Build the roots.
    $roots = array();
    foreach ($items as $delta => $item) {

      // Load the term.
      $term = taxonomy_term_load($item['tid']);

      // Get the parents.
      $parents = _cshs_get_parents($term);

      // Get the root.
      $root = array_shift($parents);
      if (!isset($roots[$root->tid])) {
        $roots[$root->tid] = array(
          'title' => $root->name,
          'terms' => array(),
        );
      }

      // Build the output string.
      if (count($parents)) {

        // If some childs are left...
        $terms = array();
        foreach ($parents as $parent) {
          $terms[] = $linked ? l($parent->name, "taxonomy/term/{$parent->tid}") : check_plain($parent->name);
        }
        $terms = implode($separator, $terms);

        // Add terms to the root.
        $roots[$root->tid]['terms'][] = $terms;
      }
    }

    // Add each root to the render array.
    foreach ($roots as $root_tid => $root) {
      $element[$root_tid] = array(
        '#markup' => theme('cshs_term_group', $root),
      );
    }
  }
  if ($display['type'] == 'cshs_term_reference_flexible_hierarchy') {
    foreach ($items as $delta => $item) {
      $linked = isset($settings['linked']) && $settings['linked'];
      $term = taxonomy_term_load($item['tid']);

      // Token replace takes care of sanitizing - therefor the l() function set
      // the html option to avoid double-escaping.
      $formatted_text = token_replace($settings['format'], array(
        'term' => $term,
      ));
      $formatted_text = $linked ? l($formatted_text, "taxonomy/term/{$term->tid}", array(
        'html' => TRUE,
      )) : $formatted_text;
      $markup = $formatted_text;
      $element[$delta] = array(
        '#markup' => $markup,
      );
    }
  }
  return $element;
}