You are here

abstract class FieldInheritancePluginBase in Field Inheritance 8

Same name and namespace in other branches
  1. 2.0.x src/Plugin/FieldInheritance/FieldInheritancePluginBase.php \Drupal\field_inheritance\Plugin\FieldInheritance\FieldInheritancePluginBase

Abstract class FieldInheritancePluginBase.

Hierarchy

Expanded class hierarchy of FieldInheritancePluginBase

File

src/Plugin/FieldInheritance/FieldInheritancePluginBase.php, line 16

Namespace

Drupal\field_inheritance\Plugin\FieldInheritance
View source
abstract class FieldInheritancePluginBase extends PluginBase implements FieldInheritancePluginInterface, ContainerFactoryPluginInterface {

  /**
   * The field inheritance id.
   *
   * @var int
   */
  protected $fieldInheritanceId;

  /**
   * The entity.
   *
   * @var \Drupal\Core\Entity\EntityInterface
   */
  protected $entity;

  /**
   * The method used to inherit.
   *
   * @var string
   */
  protected $method;

  /**
   * The source entity type used to inherit.
   *
   * @var string
   */
  protected $sourceEntityType;

  /**
   * The source field used to inherit.
   *
   * @var string
   */
  protected $sourceField;

  /**
   * The entity field used to inherit.
   *
   * @var string
   */
  protected $destinationField;

  /**
   * The language manager service.
   *
   * @var \Drupal\Core\Language\LanguageManagerInterface
   */
  protected $languageManager;

  /**
   * The current language code.
   *
   * @var string
   */
  protected $langCode;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManager
   */
  protected $entityTypeManager;

  /**
   * The key value store.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueFactory
   */
  protected $keyValue;

  /**
   * Constructs a FieldInheritancePluginBase object.
   *
   * @param array $configuration
   *   The plugin configuration.
   * @param string $plugin_id
   *   The plugin ID.
   * @param mixed $plugin_definition
   *   The plugin definition.
   * @param Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager service.
   * @param Drupal\Core\Entity\EntityTypeManager $entity_type_manager
   *   The entity type manager.
   * @param Drupal\Core\KeyValueStore\KeyValueFactory $key_value
   *   The key value store.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, LanguageManagerInterface $language_manager, EntityTypeManager $entity_type_manager, KeyValueFactory $key_value) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->fieldInheritanceId = $configuration['id'];
    $this->entity = $configuration['entity'];
    $this->method = $configuration['method'];
    $this->sourceEntityType = $configuration['source entity type'];
    $this->sourceField = $configuration['source field'];
    if (!empty($configuration['destination field'])) {
      $this->destinationField = $configuration['destination field'];
    }
    $this->languageManager = $language_manager;
    $this->langCode = $this->languageManager
      ->getCurrentLanguage()
      ->getId();
    $this->entityTypeManager = $entity_type_manager;
    $this->keyValue = $key_value;
  }

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

  /**
   * Get the configuration method.
   */
  public function getMethod() {
    return $this->method;
  }

  /**
   * Get the configuration source entity type.
   */
  public function getSourceEntityType() {
    return $this->sourceEntityType;
  }

  /**
   * Get the configuration source entity bundle.
   */
  public function getSourceEntityBundle() {
    return $this->sourceEntityBundle;
  }

  /**
   * Get the configuration source field.
   */
  public function getSourceField() {
    return $this->sourceField;
  }

  /**
   * Get the configuration destination entity type.
   */
  public function getDestinationEntityType() {
    return $this->destinationEntityType;
  }

  /**
   * Get the configuration destination entity bundle.
   */
  public function getDestinationEntityBundle() {
    return $this->destinationEntityBundle;
  }

  /**
   * Get the configuration destination field.
   */
  public function getDestinationField() {
    return $this->destinationField;
  }

  /**
   * {@inheritdoc}
   */
  public function computeValue() {
    $this
      ->validateArguments();
    $method = $this
      ->getMethod();
    $value = '';
    switch ($method) {
      case 'inherit':
        $value = $this
          ->inheritData();
        break;
      case 'prepend':
        $value = $this
          ->prependData();
        break;
      case 'append':
        $value = $this
          ->appendData();
        break;
      case 'fallback':
        $value = $this
          ->fallbackData();
        break;
    }
    $context = [
      'source_field' => $this
        ->getSourceField(),
      'source_entity' => $this
        ->getSourceEntity(),
      'destination_field' => $this
        ->getDestinationField(),
      'destination_entity' => $this
        ->getDestinationEntity(),
      'method' => $this
        ->getMethod(),
    ];
    \Drupal::moduleHandler()
      ->alter('field_inheritance_compute_value', $value, $context);
    return $value;
  }

  /**
   * Retrieve inherited data.
   *
   * @return string
   *   The inherited data.
   */
  protected function inheritData() {
    $source_entity = $this
      ->getSourceEntity();
    if ($source_entity === FALSE) {
      return [];
    }
    return $source_entity->{$this
      ->getSourceField()}
      ->getValue() ?? '';
  }

  /**
   * Retrieve prepended data.
   *
   * @return string
   *   The prepended data.
   */
  protected function prependData() {
    $source_entity = $this
      ->getSourceEntity();
    $destination_entity = $this
      ->getDestinationEntity();
    $values = [];
    if ($source_entity === FALSE) {
      return $values;
    }
    if (!empty($destination_entity->{$this
      ->getDestinationField()}
      ->getValue())) {
      $values = array_merge($values, $destination_entity->{$this
        ->getDestinationField()}
        ->getValue());
    }
    if (!empty($source_entity->{$this
      ->getSourceField()}
      ->getValue())) {
      $values = array_merge($values, $source_entity->{$this
        ->getSourceField()}
        ->getValue());
    }
    return $values;
  }

  /**
   * Retrieve appended data.
   *
   * @return string
   *   The appended data.
   */
  protected function appendData() {
    $source_entity = $this
      ->getSourceEntity();
    $destination_entity = $this
      ->getDestinationEntity();
    $values = [];
    if ($source_entity === FALSE) {
      return $values;
    }
    if (!empty($source_entity->{$this
      ->getSourceField()}
      ->getValue())) {
      $values = array_merge($values, $source_entity->{$this
        ->getSourceField()}
        ->getValue());
    }
    if (!empty($destination_entity->{$this
      ->getDestinationField()}
      ->getValue())) {
      $values = array_merge($values, $destination_entity->{$this
        ->getDestinationField()}
        ->getValue());
    }
    return $values;
  }

  /**
   * Retrieve fallback data.
   *
   * @return string
   *   The fallback data.
   */
  protected function fallbackData() {
    $source_entity = $this
      ->getSourceEntity();
    $destination_entity = $this
      ->getDestinationEntity();
    $values = [];
    if ($source_entity === FALSE) {
      return $values;
    }
    if (!empty($destination_entity->{$this
      ->getDestinationField()}
      ->getValue())) {
      $values = $destination_entity->{$this
        ->getDestinationField()}
        ->getValue();
    }
    elseif (!empty($source_entity->{$this
      ->getSourceField()}
      ->getValue())) {
      $values = $source_entity->{$this
        ->getSourceField()}
        ->getValue();
    }
    return $values;
  }

  /**
   * Validate the configuration arguments of the plugin.
   */
  protected function validateArguments() {
    if (empty($this
      ->getMethod())) {
      throw new \InvalidArgumentException("The definition's 'method' key must be set to inherit data.");
    }
    if (empty($this
      ->getSourceField())) {
      throw new \InvalidArgumentException("The definition's 'source field' key must be set to inherit data.");
    }
    $method = $this
      ->getMethod();
    $destination_field_methods = [
      'prepend',
      'append',
      'fallback',
    ];
    if (array_search($method, $destination_field_methods)) {
      if (empty($this
        ->getDestinationField())) {
        throw new \InvalidArgumentException("The definition's 'destination field' key must be set to prepend, append, or fallback to series data.");
      }
    }
    return TRUE;
  }

  /**
   * Get the translated source entity.
   *
   * @return Drupal\Core\Entity\EntityInterface|bool
   *   The translated source entity, or FALSE.
   */
  protected function getSourceEntity() {
    $entity = $this->entity;
    if (empty($entity)) {
      return FALSE;
    }
    $state_key = $entity
      ->getEntityTypeId() . ':' . $entity
      ->uuid();
    $state = $this->keyValue
      ->get('field_inheritance');
    $state_values = $state
      ->get($state_key);
    if (!empty($state_values[$this->fieldInheritanceId]['entity'])) {
      if ($source = $this->entityTypeManager
        ->getStorage($this->sourceEntityType)
        ->load($state_values[$this->fieldInheritanceId]['entity'])) {
        $context['data'] = $source;
        $context += [
          'operation' => 'entity_view',
          'langcode' => $this->langCode,
        ];
        $candidates = $this->languageManager
          ->getFallbackCandidates($context);
        foreach ($candidates as $candidate) {
          if ($source
            ->hasTranslation($candidate)) {
            return $source
              ->getTranslation($candidate);
          }
        }
      }
    }
    return FALSE;
  }

  /**
   * Get the translated destination entity.
   *
   * @return Drupal\Core\Entity\EntityInterface
   *   The translated destination entity.
   */
  protected function getDestinationEntity() {
    $context['data'] = $this->entity;
    $context += [
      'operation' => 'entity_view',
      'langcode' => $this->langCode,
    ];
    $candidates = $this->languageManager
      ->getFallbackCandidates($context);
    foreach ($candidates as $candidate) {
      if ($this->entity
        ->hasTranslation($candidate)) {
        return $this->entity
          ->getTranslation($candidate);
      }
    }
    return $this->entity;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FieldInheritancePluginBase::$destinationField protected property The entity field used to inherit.
FieldInheritancePluginBase::$entity protected property The entity.
FieldInheritancePluginBase::$entityTypeManager protected property The entity type manager.
FieldInheritancePluginBase::$fieldInheritanceId protected property The field inheritance id.
FieldInheritancePluginBase::$keyValue protected property The key value store.
FieldInheritancePluginBase::$langCode protected property The current language code.
FieldInheritancePluginBase::$languageManager protected property The language manager service.
FieldInheritancePluginBase::$method protected property The method used to inherit.
FieldInheritancePluginBase::$sourceEntityType protected property The source entity type used to inherit.
FieldInheritancePluginBase::$sourceField protected property The source field used to inherit.
FieldInheritancePluginBase::appendData protected function Retrieve appended data.
FieldInheritancePluginBase::computeValue public function Compute the value of the field. Overrides FieldInheritancePluginInterface::computeValue
FieldInheritancePluginBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
FieldInheritancePluginBase::fallbackData protected function Retrieve fallback data.
FieldInheritancePluginBase::getDestinationEntity protected function Get the translated destination entity.
FieldInheritancePluginBase::getDestinationEntityBundle public function Get the configuration destination entity bundle.
FieldInheritancePluginBase::getDestinationEntityType public function Get the configuration destination entity type.
FieldInheritancePluginBase::getDestinationField public function Get the configuration destination field.
FieldInheritancePluginBase::getMethod public function Get the configuration method.
FieldInheritancePluginBase::getSourceEntity protected function Get the translated source entity.
FieldInheritancePluginBase::getSourceEntityBundle public function Get the configuration source entity bundle.
FieldInheritancePluginBase::getSourceEntityType public function Get the configuration source entity type.
FieldInheritancePluginBase::getSourceField public function Get the configuration source field.
FieldInheritancePluginBase::inheritData protected function Retrieve inherited data.
FieldInheritancePluginBase::prependData protected function Retrieve prepended data.
FieldInheritancePluginBase::validateArguments protected function Validate the configuration arguments of the plugin.
FieldInheritancePluginBase::__construct public function Constructs a FieldInheritancePluginBase object. Overrides PluginBase::__construct
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.