Skip to content
Snippets Groups Projects
Commit ddec4ba3 authored by Owen Bush's avatar Owen Bush
Browse files

Fixing inheritance for entity reference fields.

parent 50ab5416
No related branches found
No related tags found
No related merge requests found
......@@ -40,6 +40,7 @@ function recurring_events_entity_base_field_info_alter(&$fields, EntityTypeInter
'method' => $field->type(),
'plugin' => $field->plugin(),
];
if ($field->entityField()) {
$settings['entity field'] = $field->entityField();
}
......@@ -51,12 +52,18 @@ function recurring_events_entity_base_field_info_alter(&$fields, EntityTypeInter
$type = $field_definitions[$field->sourceField()]->getType();
}
$settings = array_merge($settings, $field_definitions[$field->sourceField()]->getSettings());
$class = '\Drupal\recurring_events\FieldInheritanceFactory';
if ($type === 'entity_reference') {
$class = '\Drupal\recurring_events\EntityReferenceFieldInheritanceFactory';
}
$fields[$field->id()] = BaseFieldDefinition::create($type)
->setLabel($field->label())
->setName($field->id())
->setDescription(t('The inherited field: @field', ['@field' => $field->label()]))
->setComputed(TRUE)
->setClass('\Drupal\recurring_events\FieldInheritanceFactory')
->setClass($class)
->setSettings($settings)
->setTargetEntityTypeId($entity_type->id())
->setDisplayConfigurable('view', TRUE);
......
<?php
namespace Drupal\recurring_events;
use Drupal\Core\Field\EntityReferenceFieldItemList;
use Drupal\Core\TypedData\ComputedItemListTrait;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\TypedData\TypedDataInterface;
/**
* The EntityReferenceFieldInheritanceFactory class.
*
* Note: This class exists separately to the FieldInheritanceFactor because
* when inheriting data from an entity_reference field we end up with type hints
* failing during calls to EntityReferenceFormatterBase::getEntitiesToView().
*/
class EntityReferenceFieldInheritanceFactory extends EntityReferenceFieldItemList {
use ComputedItemListTrait;
/**
* Constructs a TypedData object given its definition and context.
*
* @param \Drupal\Core\Field\BaseFieldDefinition $definition
* The data definition.
* @param string $name
* (optional) The name of the created property, or NULL if it is the root
* of a typed data tree. Defaults to NULL.
* @param \Drupal\Core\TypedData\TypedDataInterface $parent
* (optional) The parent object of the data property, or NULL if it is the
* root of a typed data tree. Defaults to NULL.
*
* @see \Drupal\Core\TypedData\TypedDataManager::create()
*/
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 field') === NULL) {
throw new \InvalidArgumentException("The definition's 'source field' key has to specify the field from which to inherit data.");
}
}
/**
* Compute the field property from state.
*/
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);
}
}
}
/**
* Returns the FieldInheritancePluginManager plugin manager.
*
* @return \Drupal\recurring_events\FieldInheritancePluginManager
* The FieldInheritancePluginManager plugin manager.
*/
protected function getManager() {
return \Drupal::service('plugin.manager.field_inheritance');
}
}
......@@ -5,15 +5,15 @@ namespace Drupal\recurring_events\Plugin\FieldInheritance;
use Drupal\recurring_events\FieldInheritancePluginInterface;
/**
* Media Inheritance plugin.
* Entity Reference Inheritance plugin.
*
* @FieldInheritance(
* id = "media_inheritance",
* name = @Translation("Media Field Inheritance"),
* id = "entity_reference_inheritance",
* name = @Translation("Entity Reference Field Inheritance"),
* types = {
* "field_ui:entity_reference:media"
* "entity_reference"
* }
* )
*/
class MediaFieldInheritancePlugin extends FieldInheritancePluginBase implements FieldInheritancePluginInterface {
class EntityReferenceFieldInheritancePlugin extends FieldInheritancePluginBase implements FieldInheritancePluginInterface {
}
<?php
namespace Drupal\recurring_events\Plugin\FieldInheritance;
use Drupal\recurring_events\FieldInheritancePluginInterface;
/**
* Taxonomy Inheritance plugin.
*
* @FieldInheritance(
* id = "taxonomy_inheritance",
* name = @Translation("Taxonomy Field Inheritance"),
* types = {
* "field_ui:entity_reference:taxonomy_term"
* }
* )
*/
class TaxonomyFieldInheritancePlugin extends FieldInheritancePluginBase implements FieldInheritancePluginInterface {
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment