EntityReferenceFieldInheritanceFactory.php in Field Inheritance 2.0.x
File
src/EntityReferenceFieldInheritanceFactory.php
View source
<?php
namespace Drupal\field_inheritance;
use Drupal\Core\Field\EntityReferenceFieldItemList;
use Drupal\Core\TypedData\ComputedItemListTrait;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\TypedData\TypedDataInterface;
class EntityReferenceFieldInheritanceFactory extends EntityReferenceFieldItemList {
use ComputedItemListTrait;
public function __construct(BaseFieldDefinition $definition, $name = NULL, TypedDataInterface $parent = NULL) {
parent::__construct($definition, $name, $parent);
if ($this
->getSetting('plugin') === NULL) {
throw new \InvalidArgumentException("The definition's 'plugin' key has to specify the plugin to use to inherit data.");
}
if ($this
->getSetting('method') === NULL) {
throw new \InvalidArgumentException("The definition's 'method' key has to specify the method to use to inherit data. Valid options are inherit, prepend, replace, and append.");
}
if ($this
->getSetting('source entity type') === NULL) {
throw new \InvalidArgumentException("The definition's 'source entity type' key has to specify the entity type from which to inherit data.");
}
if ($this
->getSetting('source entity bundle') === NULL) {
throw new \InvalidArgumentException("The definition's 'source entity bundle' key has to specify the entity bundle from which to inherit data.");
}
if ($this
->getSetting('source field') === NULL) {
throw new \InvalidArgumentException("The definition's 'source field' key has to specify the field from which to inherit data.");
}
}
protected function computeValue() {
$entity = $this
->getEntity();
$manager = $this
->getManager();
$configuration = $this
->getSettings() + [
'entity' => $entity,
];
$plugin = $manager
->createInstance($this
->getSetting('plugin'), $configuration);
$values = $plugin
->computeValue();
if (!empty($values)) {
foreach ($values as $key => $value) {
$this->list[$key] = $this
->createItem($key, $value);
}
}
else {
$this
->applyDefaultValue();
}
}
protected function getManager() {
return \Drupal::service('plugin.manager.field_inheritance');
}
}