You are here

function term_level_field_formatter_view in Term Level Field 7

Implements hook_field_formatter_view().

File

./term_level.module, line 122
Field type for referencing terms with a level to an entity.

Code

function term_level_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  drupal_add_css(drupal_get_path('module', 'term_level') . '/term_level.css');
  $levels = term_level_options_list($field);
  $term_levels = array();
  $level_groups = array();
  foreach ($items as $delta => $item) {
    $term_levels[$item['tid']] = $item['level'];
    $level_groups[$item['level']][] = $item['tid'];
  }
  $terms = taxonomy_term_load_multiple(array_keys($term_levels));
  $element = array();
  if ($display['type'] == 'term_level_default') {
    foreach ($term_levels as $tid => $level_key) {
      if (isset($terms[$tid])) {
        $term = $terms[$tid];
        $element[$tid] = array(
          '#markup' => check_plain($term->name) . ': ' . check_plain($levels[$level_key]),
        );
      }
    }
  }
  elseif ($display['type'] == 'term_level_group') {
    krsort($level_groups);
    foreach ($level_groups as $level_key => $tids) {
      $term_names = array();
      foreach ($tids as $tid) {
        if (isset($terms[$tid])) {
          $term = $terms[$tid];
          $term_names[] = check_plain($term->name);
        }
      }
      $element[$level_key] = array(
        '#theme' => 'term_level_group',
        '#levels' => $levels,
        '#level' => $level_key,
        '#term_names' => $term_names,
      );
    }
  }
  return $element;
}