abstract class TaxonomyFormatterBase

Base class for the taxonomy_term formatters.

Hierarchy

Expanded class hierarchy of TaxonomyFormatterBase

3 files declare their use of TaxonomyFormatterBase
LinkFormatter.php in drupal/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/LinkFormatter.php
Contains \Drupal\taxonomy\Plugin\field\formatter\LinkFormatter.
PlainFormatter.php in drupal/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/PlainFormatter.php
Contains \Drupal\taxonomy\Plugin\field\formatter\PlainFormatter.
RSSCategoryFormatter.php in drupal/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/RSSCategoryFormatter.php
Contains \Drupal\taxonomy\Plugin\field\formatter\RSSCategoryFormatter.

File

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

Namespace

Drupal\taxonomy\Plugin\field\formatter
View source
abstract class TaxonomyFormatterBase extends FormatterBase {

  /**
   * 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.
   */
  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]);
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FormatterBase::$field protected property The field definition.
FormatterBase::$instance protected property The field instance definition.
FormatterBase::$label protected property The label display setting.
FormatterBase::$settings protected property The formatter settings. Overrides PluginSettingsBase::$settings
FormatterBase::$viewMode protected property The view mode.
FormatterBase::settingsForm public function Returns a form to configure settings for the formatter. Overrides FormatterInterface::settingsForm 12
FormatterBase::settingsSummary public function Returns a short summary for the current formatter settings. Overrides FormatterInterface::settingsSummary 12
FormatterBase::view public function Builds a renderable array for one field on one entity instance. Overrides FormatterInterface::view
FormatterBase::__construct public function Constructs a FormatterBase object. Overrides PluginBase::__construct
FormatterInterface::viewElements public function Builds a renderable array for a field value. 26
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin_id.
PluginBase::getPluginDefinition public function Returns the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Returns the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginSettingsBase::$defaultSettingsMerged protected property Whether default settings have been merged into the current $settings.
PluginSettingsBase::getDefaultSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getDefaultSettings(). Overrides PluginSettingsInterface::getDefaultSettings
PluginSettingsBase::getSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSetting(). Overrides PluginSettingsInterface::getSetting
PluginSettingsBase::getSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::getSettings(). Overrides PluginSettingsInterface::getSettings
PluginSettingsBase::mergeDefaults protected function Merges default settings values into $settings.
PluginSettingsBase::setSetting public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSetting(). Overrides PluginSettingsInterface::setSetting
PluginSettingsBase::setSettings public function Implements Drupal\field\Plugin\PluginSettingsInterface::setSettings(). Overrides PluginSettingsInterface::setSettings
TaxonomyFormatterBase::prepareView public function Implements \Drupal\field\Plugin\Type\Formatter\FormatterInterface::prepareView(). Overrides FormatterBase::prepareView