View source
<?php
namespace Drupal\field_inheritance\Plugin\FieldInheritance;
use Drupal\Component\Plugin\PluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\field_inheritance\FieldInheritancePluginInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Entity\EntityTypeManager;
use Drupal\Core\KeyValueStore\KeyValueFactory;
abstract class FieldInheritancePluginBase extends PluginBase implements FieldInheritancePluginInterface, ContainerFactoryPluginInterface {
protected $fieldInheritanceId;
protected $entity;
protected $method;
protected $sourceEntityType;
protected $sourceField;
protected $destinationField;
protected $languageManager;
protected $langCode;
protected $entityTypeManager;
protected $keyValue;
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;
}
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'));
}
public function getMethod() {
return $this->method;
}
public function getSourceEntityType() {
return $this->sourceEntityType;
}
public function getSourceEntityBundle() {
return $this->sourceEntityBundle;
}
public function getSourceField() {
return $this->sourceField;
}
public function getDestinationEntityType() {
return $this->destinationEntityType;
}
public function getDestinationEntityBundle() {
return $this->destinationEntityBundle;
}
public function getDestinationField() {
return $this->destinationField;
}
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;
}
protected function inheritData() {
$source_entity = $this
->getSourceEntity();
if ($source_entity === FALSE) {
return [];
}
return $source_entity->{$this
->getSourceField()}
->getValue() ?? '';
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
}