From 4d166e5e337d67273ba0c7e8761ee7d80ec7e33d Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole Date: Thu, 13 Feb 2014 12:01:34 +0000 Subject: [PATCH] Issue #2134967 by plopesc, swentel, chx: FieldDefinitionInterface should include a getTargetEntityTypeId(). --- core/lib/Drupal/Core/Entity/EntityManager.php | 6 +++++- .../lib/Drupal/Core/Field/FieldDefinition.php | 21 +++++++++++++++++++ .../Core/Field/FieldDefinitionInterface.php | 16 ++++++++++++++ .../field/lib/Drupal/field/Entity/Field.php | 7 +++++++ .../lib/Drupal/field/Entity/FieldInstance.php | 7 +++++++ 5 files changed, 56 insertions(+), 1 deletion(-) diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index b3579b8bf49..6a2385f9ae7 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -309,8 +309,12 @@ class EntityManager extends PluginManagerBase implements EntityManagerInterface $entity_type = $this->getDefinition($entity_type_id); $class = $entity_type->getClass(); + $base_definitions = $class::baseFieldDefinitions($entity_type_id); + foreach ($base_definitions as &$base_definition) { + $base_definition->setTargetEntityTypeId($entity_type_id); + } $this->entityFieldInfo[$entity_type_id] = array( - 'definitions' => $class::baseFieldDefinitions($entity_type_id), + 'definitions' => $base_definitions, // Contains definitions of optional (per-bundle) fields. 'optional' => array(), // An array keyed by bundle name containing the optional fields added diff --git a/core/lib/Drupal/Core/Field/FieldDefinition.php b/core/lib/Drupal/Core/Field/FieldDefinition.php index 38b5d4de0a4..e1f698a0a55 100644 --- a/core/lib/Drupal/Core/Field/FieldDefinition.php +++ b/core/lib/Drupal/Core/Field/FieldDefinition.php @@ -285,6 +285,27 @@ class FieldDefinition extends ListDefinition implements FieldDefinitionInterface return $this->getSetting('default_value'); } + /** + * {@inheritdoc} + */ + public function getTargetEntityTypeId() { + return isset($this->definition['entity_type']) ? $this->definition['entity_type'] : NULL; + } + + /** + * Sets the ID of the type of the entity this field is attached to. + * + * @param string $entity_type_id + * The name of the target entity type to set. + * + * @return static + * The object itself for chaining. + */ + public function setTargetEntityTypeId($entity_type_id) { + $this->definition['entity_type'] = $entity_type_id; + return $this; + } + /** * {@inheritdoc} */ diff --git a/core/lib/Drupal/Core/Field/FieldDefinitionInterface.php b/core/lib/Drupal/Core/Field/FieldDefinitionInterface.php index 7d5790f0c8b..aba828fc280 100644 --- a/core/lib/Drupal/Core/Field/FieldDefinitionInterface.php +++ b/core/lib/Drupal/Core/Field/FieldDefinitionInterface.php @@ -255,6 +255,22 @@ interface FieldDefinitionInterface extends ListDefinitionInterface { */ public function getDefaultValue(EntityInterface $entity); + /** + * Returns the ID of the type of the entity this field is attached to. + * + * This method should not be confused with EntityInterface::entityType() + * (configurable fields are config entities, and thus implement both + * interfaces): + * - FieldDefinitionInterface::getTargetEntityTypeId() answers "as a field, + * which entity type are you attached to?". + * - EntityInterface::getEntityTypeId() answers "as a (config) entity, what + * is your own entity type". + * + * @return string + * The name of the entity type. + */ + public function getTargetEntityTypeId(); + /** * Returns the field schema. * diff --git a/core/modules/field/lib/Drupal/field/Entity/Field.php b/core/modules/field/lib/Drupal/field/Entity/Field.php index 1085f7901e9..69c7551dedb 100644 --- a/core/modules/field/lib/Drupal/field/Entity/Field.php +++ b/core/modules/field/lib/Drupal/field/Entity/Field.php @@ -641,6 +641,13 @@ class Field extends ConfigEntityBase implements FieldInterface { return array('type' => 'hidden'); } + /** + * {@inheritdoc} + */ + public function getTargetEntityTypeId() { + return $this->entity_type; + } + /** * {@inheritdoc} */ diff --git a/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php b/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php index f83e45ac60e..620e48c5053 100644 --- a/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php +++ b/core/modules/field/lib/Drupal/field/Entity/FieldInstance.php @@ -598,6 +598,13 @@ class FieldInstance extends ConfigEntityBase implements FieldInstanceInterface { return array('type' => 'hidden'); } + /** + * {@inheritdoc} + */ + public function getTargetEntityTypeId() { + return $this->entity_type; + } + /** * {@inheritdoc} */