You are here

function nat_field_formatter_view in Node Auto Term [NAT] 7.2

Same name and namespace in other branches
  1. 7 nat.module \nat_field_formatter_view()

Implements hook_field_formatter_view().

File

./nat.module, line 262
NAT - node auto term - is a helper module that automatically creates a term using the same title as a node.

Code

function nat_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  foreach ($items as $delta => $item) {
    if ($item['tid'] == 'autocreate') {
      $element[$delta] = array(
        '#markup' => check_plain($item['name']),
      );
    }
    else {
      $term = $item['taxonomy_term'];
      $nids = array_keys(nat_get_nids(array(
        $term->tid,
      ), FALSE));
      if (count($nids)) {
        $element[$delta] = array(
          '#type' => 'link',
          '#title' => $term->name,
          '#href' => 'node/' . $nids[0],
        );
      }
      else {
        $element[$delta] = array(
          '#type' => 'markup',
          '#markup' => $term->name,
        );
      }
    }
  }
  return $element;
}