1 taxonomy.module taxonomy_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display)

Implements hook_field_formatter_view().

File

core/modules/taxonomy/taxonomy.module, line 1527
Enables the organization of content into categories.

Code

function taxonomy_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  $element = array();
  $settings = $display['settings'];

  // Terms whose tid is 'autocreate' do not exist
  // yet and $item['taxonomy_term'] is not set. Theme such terms as
  // just their name.

  switch ($display['type']) {
    case 'taxonomy_term_reference_link':
      foreach ($items as $delta => $item) {
        if ($item['tid'] == 'autocreate') {
          $element[$delta] = array(
            '#markup' => check_plain($item['name']),
          );
        }
        else {
          $term = $item['taxonomy_term'];
          $uri = entity_uri('taxonomy_term', $term);
          $element[$delta] = array(
            '#type' => 'link',
            '#title' => $term->name,
            '#href' => $uri['path'],
            '#options' => $uri['options'],
          );
        }
      }
      break;

    case 'taxonomy_term_reference_plain':
      foreach ($items as $delta => $item) {
        $name = ($item['tid'] != 'autocreate' ? $item['taxonomy_term']->name : $item['name']);
        $element[$delta] = array(
          '#markup' => check_plain($name),
        );
      }
      break;

    case 'taxonomy_term_reference_rss_category':
      foreach ($items as $delta => $item) {
        $entity->rss_elements[] = array(
          'key' => 'category',
          'value' => $item['tid'] != 'autocreate' ? $item['taxonomy_term']->name : $item['name'],
          'attributes' => array(
            'domain' => $item['tid'] != 'autocreate' ? url('taxonomy/term/' . $item['tid'], array('absolute' => TRUE)) : '',
          ),
        );
      }
      break;

    case 'taxonomy_term_reference_rendered':
      // To prevent infinite recursion caused by reference cycles, we store
      // diplayed terms in a recursion queue.
      $recursion_queue = &backdrop_static(__FUNCTION__, array());

      // If no 'referencing entity' is set, we are starting a new 'reference
      // thread' and need to reset the queue.
      if (!isset($entity->referencing_entity)) {
        $recursion_queue = array();
      }

      // The recursion queue only needs to track terms.
      if (in_array($entity_type, array('taxonomy_term'))) {
        $recursion_queue[$entity->id()] = $entity->id();
      }

      // Check the recursion queue to determine which terms should be fully
      // displayed, and which terms will only be displayed as a title.
      $terms_display = array();
      foreach ($items as $delta => $item) {
        if (!isset($recursion_queue[$item['tid']])) {
          $terms_display[$item['tid']] = $item['taxonomy_term'];
        }
      }

      // Load and build the fully displayed terms.
      if ($terms_display) {
        foreach ($terms_display as $tid => $term) {
          $terms_display[$tid]->referencing_entity = $entity;
          $terms_display[$tid]->referencing_field = $field['field_name'];
        }
        $terms_built = taxonomy_term_view_multiple($terms_display, $settings['view_mode']);
      }

      // Assemble the render array.
      foreach ($items as $delta => $item) {
        if (isset($terms_display[$item['tid']])) {
          $element[$delta] = $terms_built['taxonomy_terms'][$item['tid']];
        }
        else {
          $term = $item['taxonomy_term'];
          $label = entity_label('taxonomy_term', $term);
          $uri = entity_uri('taxonomy_term', $term);
          $element[$delta] = array(
            '#type' => 'link',
            '#title' => $label,
            '#href' => $uri['path'],
            '#options' => $uri['options'],
          );
        }
      }

      break;
  }

  return $element;
}