You are here

abstract class FieldFormatterEntityEmbedDisplayBase in Entity Embed 8

Base class for field formatter display plugins.

Hierarchy

Expanded class hierarchy of FieldFormatterEntityEmbedDisplayBase

1 file declares its use of FieldFormatterEntityEmbedDisplayBase
EntityReferenceFieldFormatter.php in src/Plugin/entity_embed/EntityEmbedDisplay/EntityReferenceFieldFormatter.php

File

src/EntityEmbedDisplay/FieldFormatterEntityEmbedDisplayBase.php, line 20

Namespace

Drupal\entity_embed\EntityEmbedDisplay
View source
abstract class FieldFormatterEntityEmbedDisplayBase extends EntityEmbedDisplayBase {
  use PluginDependencyTrait;

  /**
   * The field formatter plugin manager.
   *
   * @var \Drupal\Core\Field\FormatterPluginManager
   */
  protected $formatterPluginManager;

  /**
   * The typed data manager.
   *
   * @var \Drupal\Core\TypedData\TypedDataManager
   */
  protected $typedDataManager;

  /**
   * The field definition.
   *
   * @var \Drupal\Core\Field\BaseFieldDefinition
   */
  protected $fieldDefinition;

  /**
   * The field formatter.
   *
   * @var \Drupal\Core\Field\FormatterInterface
   */
  protected $fieldFormatter;

  /**
   * Constructs a FieldFormatterEntityEmbedDisplayBase object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   * @param \Drupal\Core\Field\FormatterPluginManager $formatter_plugin_manager
   *   The field formatter plugin manager.
   * @param \Drupal\Core\TypedData\TypedDataManager $typed_data_manager
   *   The typed data manager.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, FormatterPluginManager $formatter_plugin_manager, TypedDataManager $typed_data_manager, LanguageManagerInterface $language_manager) {
    $this->formatterPluginManager = $formatter_plugin_manager;
    $this
      ->setConfiguration($configuration);
    $this->typedDataManager = $typed_data_manager;
    parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager, $language_manager);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('entity_type.manager'), $container
      ->get('plugin.manager.field.formatter'), $container
      ->get('typed_data_manager'), $container
      ->get('language_manager'));
  }

  /**
   * Get the FieldDefinition object required to render this field's formatter.
   *
   * @return \Drupal\Core\Field\BaseFieldDefinition
   *   The field definition.
   *
   * @see \Drupal\entity_embed\FieldFormatterEntityEmbedDisplayBase::build()
   */
  public function getFieldDefinition() {
    if (!isset($this->fieldDefinition)) {
      $field_type = $this
        ->getPluginDefinition()['field_type'];
      $this->fieldDefinition = BaseFieldDefinition::create($field_type);

      // Ensure the field name is unique for each Entity Embed Display plugin
      // instance.
      static $index = 0;
      $this->fieldDefinition
        ->setName('_entity_embed_' . $index++);
    }
    return $this->fieldDefinition;
  }

  /**
   * Get the field value required to pass into the field formatter.
   *
   * @return mixed
   *   The field value.
   */
  public abstract function getFieldValue();

  /**
   * {@inheritdoc}
   */
  public function access(AccountInterface $account = NULL) {
    return parent::access($account)
      ->andIf($this
      ->isApplicableFieldFormatter());
  }

  /**
   * Checks if the field formatter is applicable.
   *
   * @return \Drupal\Core\Access\AccessResult
   *   Returns the access result.
   */
  protected function isApplicableFieldFormatter() {
    $definition = $this->formatterPluginManager
      ->getDefinition($this
      ->getFieldFormatterId());
    return AccessResult::allowedIf($definition['class']::isApplicable($this
      ->getFieldDefinition()));
  }

  /**
   * Returns the field formatter id.
   *
   * @return string|null
   *   Returns field formatter id or null.
   */
  public function getFieldFormatterId() {
    return $this
      ->getDerivativeId();
  }

  /**
   * {@inheritdoc}
   */
  public function build() {

    // Create a temporary entity to which our fake field value can be
    // added.
    $fakeEntity = EntityEmbedFakeEntity::create([
      'type' => '_entity_embed',
    ]);
    $definition = $this
      ->getFieldDefinition();

    /* @var \Drupal\Core\Field\FieldItemListInterface $items $items */

    // Create a field item list object, 1 is the value, array('target_id' => 1)
    // would work too, or multiple values. 1 is passed down from the list to the
    // field item, which knows that an integer is the ID.
    $items = $this->typedDataManager
      ->create($definition, $this
      ->getFieldValue($definition), $definition
      ->getName(), $fakeEntity
      ->getTypedData());

    // Prepare, expects an array of items, keyed by parent entity ID.
    $formatter = $this
      ->getFieldFormatter();
    $formatter
      ->prepareView([
      $fakeEntity
        ->id() => $items,
    ]);
    $build = $formatter
      ->viewElements($items, $this
      ->getLangcode());

    // For some reason $build[0]['#printed'] is TRUE, which means it will fail
    // to render later. So for now we manually fix that.
    // @todo Investigate why this is needed.
    show($build[0]);
    return $build[0];
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return $this->formatterPluginManager
      ->getDefaultSettings($this
      ->getFieldFormatterId());
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    return $this
      ->getFieldFormatter()
      ->settingsForm($form, $form_state);
  }

  /**
   * Constructs a field formatter.
   *
   * @return \Drupal\Core\Field\FormatterInterface
   *   The formatter object.
   */
  public function getFieldFormatter() {
    if (!isset($this->fieldFormatter)) {
      $display = [
        'type' => $this
          ->getFieldFormatterId(),
        'settings' => $this
          ->getConfiguration(),
        'label' => 'hidden',
      ];

      // Create the formatter plugin. Will use the default formatter for that
      // field type if none is passed.
      $this->fieldFormatter = $this->formatterPluginManager
        ->getInstance([
        'field_definition' => $this
          ->getFieldDefinition(),
        'view_mode' => '_entity_embed',
        'configuration' => $display,
      ]);
    }
    return $this->fieldFormatter;
  }

  /**
   * Creates a new faux-field definition.
   *
   * @param string $type
   *   The type of the field.
   *
   * @return \Drupal\Core\Field\BaseFieldDefinition
   *   A new field definition.
   */
  protected function createFieldDefinition($type) {
    $definition = BaseFieldDefinition::create($type);
    static $index = 0;
    $definition
      ->setName('_entity_embed_' . $index++);
    return $definition;
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    $this
      ->addDependencies(parent::calculateDependencies());
    $definition = $this->formatterPluginManager
      ->getDefinition($this
      ->getFieldFormatterId());
    $this
      ->addDependency('module', $definition['provider']);

    // @todo Investigate why this does not work currently.
    // $this->calculatePluginDependencies($this->getFieldFormatter());
    return $this->dependencies;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
DependencyTrait::$dependencies protected property The object's dependencies.
DependencyTrait::addDependencies protected function Adds multiple dependencies.
DependencyTrait::addDependency protected function Adds a dependency.
EntityEmbedDisplayBase::$attributes public property The attributes on the embedded entity.
EntityEmbedDisplayBase::$context public property The context for the plugin.
EntityEmbedDisplayBase::$entityTypeManager protected property The entity type manager service.
EntityEmbedDisplayBase::$languageManager protected property The language manager.
EntityEmbedDisplayBase::getAttributeValue public function Gets the value for an attribute.
EntityEmbedDisplayBase::getAttributeValues public function Gets the values for all attributes.
EntityEmbedDisplayBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
EntityEmbedDisplayBase::getConfigurationValue public function Gets a configuration value.
EntityEmbedDisplayBase::getContextValue public function Gets the value for a defined context.
EntityEmbedDisplayBase::getContextValues public function Gets the values for all defined contexts.
EntityEmbedDisplayBase::getEntityFromContext public function Gets the entity from the current context.
EntityEmbedDisplayBase::getEntityTypeFromContext public function Gets the entity type from the current context.
EntityEmbedDisplayBase::getLangcode public function Gets the current language code.
EntityEmbedDisplayBase::hasAttribute public function Checks if an attribute is set.
EntityEmbedDisplayBase::hasContextValue public function Returns whether or not value is set for a defined context.
EntityEmbedDisplayBase::isValidEntityType protected function Validates that this display plugin applies to the current entity type.
EntityEmbedDisplayBase::setAttributes public function Sets the values for all attributes.
EntityEmbedDisplayBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
EntityEmbedDisplayBase::setContextValue public function Sets the value for a defined context.
EntityEmbedDisplayBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm 1
EntityEmbedDisplayBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
FieldFormatterEntityEmbedDisplayBase::$fieldDefinition protected property The field definition.
FieldFormatterEntityEmbedDisplayBase::$fieldFormatter protected property The field formatter.
FieldFormatterEntityEmbedDisplayBase::$formatterPluginManager protected property The field formatter plugin manager.
FieldFormatterEntityEmbedDisplayBase::$typedDataManager protected property The typed data manager.
FieldFormatterEntityEmbedDisplayBase::access public function Indicates whether this Entity Embed display can be used. Overrides EntityEmbedDisplayBase::access 1
FieldFormatterEntityEmbedDisplayBase::build public function Builds the renderable array for this Entity Embed display plugin. Overrides EntityEmbedDisplayBase::build 1
FieldFormatterEntityEmbedDisplayBase::buildConfigurationForm public function Form constructor. Overrides EntityEmbedDisplayBase::buildConfigurationForm 2
FieldFormatterEntityEmbedDisplayBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides EntityEmbedDisplayBase::calculateDependencies 1
FieldFormatterEntityEmbedDisplayBase::create public static function Creates an instance of the plugin. Overrides EntityEmbedDisplayBase::create 1
FieldFormatterEntityEmbedDisplayBase::createFieldDefinition protected function Creates a new faux-field definition.
FieldFormatterEntityEmbedDisplayBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides EntityEmbedDisplayBase::defaultConfiguration 1
FieldFormatterEntityEmbedDisplayBase::getFieldDefinition public function Get the FieldDefinition object required to render this field's formatter. 1
FieldFormatterEntityEmbedDisplayBase::getFieldFormatter public function Constructs a field formatter. 1
FieldFormatterEntityEmbedDisplayBase::getFieldFormatterId public function Returns the field formatter id. 1
FieldFormatterEntityEmbedDisplayBase::getFieldValue abstract public function Get the field value required to pass into the field formatter. 1
FieldFormatterEntityEmbedDisplayBase::isApplicableFieldFormatter protected function Checks if the field formatter is applicable. 1
FieldFormatterEntityEmbedDisplayBase::__construct public function Constructs a FieldFormatterEntityEmbedDisplayBase object. Overrides EntityEmbedDisplayBase::__construct 1
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginDependencyTrait::calculatePluginDependencies protected function Calculates and adds dependencies of a specific plugin instance. 1
PluginDependencyTrait::getPluginDependencies protected function Calculates and returns dependencies of a specific plugin instance.
PluginDependencyTrait::moduleHandler protected function Wraps the module handler. 1
PluginDependencyTrait::themeHandler protected function Wraps the theme handler. 1
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.