public function TaxonomyFormatterBase::prepareView

Implements \Drupal\field\Plugin\Type\Formatter\FormatterInterface::prepareView().

This preloads all taxonomy terms for multiple loaded objects at once and unsets values for invalid terms that do not exist.

Overrides FormatterBase::prepareView

File

drupal/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/TaxonomyFormatterBase.php, line 26
Contains \Drupal\taxonomy\Plugin\field\formatter\TaxonomyFormatterBase.

Class

TaxonomyFormatterBase
Base class for the taxonomy_term formatters.

Namespace

Drupal\taxonomy\Plugin\field\formatter

Code

public function prepareView(array $entities, $langcode, array &$items) {
  $tids = array();

  // Collect every possible term attached to any of the fieldable entities.
  foreach ($entities as $id => $entity) {
    foreach ($items[$id] as $delta => $item) {

      // Force the array key to prevent duplicates.
      if ($item['tid'] !== 0) {
        $tids[$item['tid']] = $item['tid'];
      }
    }
  }
  if ($tids) {
    $terms = taxonomy_term_load_multiple($tids);

    // Iterate through the fieldable entities again to attach the loaded term
    // data.
    foreach ($entities as $id => $entity) {
      $rekey = FALSE;
      foreach ($items[$id] as $delta => $item) {

        // Check whether the taxonomy term field instance value could be
        // loaded.
        if (isset($terms[$item['tid']])) {

          // Replace the instance value with the term data.
          $items[$id][$delta]['entity'] = $terms[$item['tid']];
        }
        elseif ($item['tid'] === 0 && isset($item['entity'])) {

          // Leave the item in place.
        }
        else {
          unset($items[$id][$delta]);
          $rekey = TRUE;
        }
      }
      if ($rekey) {

        // Rekey the items array.
        $items[$id] = array_values($items[$id]);
      }
    }
  }
}