abstract class EntityReferenceFormatterBase

Parent plugin for entity reference formatters.

Hierarchy

Expanded class hierarchy of EntityReferenceFormatterBase

4 files declare their use of EntityReferenceFormatterBase
EntityReferenceEntityFormatter.php in drupal/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceEntityFormatter.php
Contains \Drupal\entity_reference\Plugin\field\formatter\EntityReferenceEntityFormatter.
EntityReferenceIdFormatter.php in drupal/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceIdFormatter.php
Contains \Drupal\entity_reference\Plugin\field\formatter\EntityReferenceIdFormatter.
EntityReferenceLabelFormatter.php in drupal/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/formatter/EntityReferenceLabelFormatter.php
Contains \Drupal\entity_reference\Plugin\field\formatter\EntityReferenceLabelFormatter.
EntityReferenceTaxonomyTermRssFormatter.php in drupal/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/formatter/EntityReferenceTaxonomyTermRssFormatter.php
Contains \Drupal\taxonomy\Plugin\field\formatter\EntityReferenceTaxonomyTermRssFormatter.

File

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

Namespace

Drupal\entity_reference\Plugin\field\formatter
View source
abstract class EntityReferenceFormatterBase extends FormatterBase {

  /**
   * Overrides \Drupal\field\Plugin\Type\Formatter\FormatterBase::prepareView().
   *
   * Mark the accessible IDs a user can see. We do not unset unaccessible
   * values, as other may want to act on those values, even if they can
   * not be accessed.
   */
  public function prepareView(array $entities, $langcode, array &$items) {
    $target_ids = array();
    $revision_ids = array();

    // Collect every possible entity attached to any of the entities.
    foreach ($entities as $id => $entity) {
      foreach ($items[$id] as $delta => $item) {
        if (!empty($item['revision_id'])) {
          $revision_ids[] = $item['revision_id'];
        }
        elseif (!empty($item['target_id'])) {
          $target_ids[] = $item['target_id'];
        }
      }
    }
    $target_type = $this->field['settings']['target_type'];
    $target_entities = array();
    if ($target_ids) {
      $target_entities = entity_load_multiple($target_type, $target_ids);
    }
    if ($revision_ids) {

      // We need to load the revisions one by-one.
      foreach ($revision_ids as $revision_id) {
        $entity = entity_revision_load($target_type, $revision_id);

        // Use the revision ID in the key.
        $identifier = $entity
          ->id() . ':' . $revision_id;
        $target_entities[$identifier] = $entity;
      }
    }

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

        // If we have a revision ID, the key uses it as well.
        $identifier = !empty($item['revision_id']) ? $item['target_id'] . ':' . $item['revision_id'] : $item['target_id'];
        if ($item['target_id'] !== 0) {
          if (!isset($target_entities[$identifier])) {

            // The entity no longer exists, so remove the key.
            $rekey = TRUE;
            unset($items[$id][$delta]);
            continue;
          }
          $entity = $target_entities[$identifier];
          $items[$id][$delta]['entity'] = $entity;
          if (!$entity
            ->access('view')) {
            continue;
          }
        }
        else {

          // This is an "auto_create" item, just leave the entity in place.
        }

        // Mark item as accessible.
        $items[$id][$delta]['access'] = TRUE;
      }
      if ($rekey) {

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

  /**
   * Overrides \Drupal\field\Plugin\Type\Formatter\FormatterBase::viewElements().
   *
   * @see \Drupal\entity_reference\Plugin\field\formatter\EntityReferenceFormatterBase::viewElements().
   */
  public function viewElements(EntityInterface $entity, $langcode, array $items) {

    // Remove un-accessible items.
    foreach ($items as $delta => $item) {
      if (empty($item['access'])) {
        unset($items[$delta]);
      }
    }
    return array();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityReferenceFormatterBase::prepareView public function Overrides \Drupal\field\Plugin\Type\Formatter\FormatterBase::prepareView(). Overrides FormatterBase::prepareView
EntityReferenceFormatterBase::viewElements public function Overrides \Drupal\field\Plugin\Type\Formatter\FormatterBase::viewElements(). Overrides FormatterInterface::viewElements 4
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
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