From 4d166e5e337d67273ba0c7e8761ee7d80ec7e33d Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
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 b3579b8bf49b..6a2385f9ae7c 100644
--- a/core/lib/Drupal/Core/Entity/EntityManager.php
+++ b/core/lib/Drupal/Core/Entity/EntityManager.php
@@ -309,8 +309,12 @@ public function getFieldDefinitions($entity_type_id, $bundle = NULL) {
         $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 38b5d4de0a43..e1f698a0a55c 100644
--- a/core/lib/Drupal/Core/Field/FieldDefinition.php
+++ b/core/lib/Drupal/Core/Field/FieldDefinition.php
@@ -285,6 +285,27 @@ public function getDefaultValue(EntityInterface $entity) {
     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 7d5790f0c8b0..aba828fc2806 100644
--- a/core/lib/Drupal/Core/Field/FieldDefinitionInterface.php
+++ b/core/lib/Drupal/Core/Field/FieldDefinitionInterface.php
@@ -255,6 +255,22 @@ public function isMultiple();
    */
   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 1085f7901e92..69c7551dedbb 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 @@ public function getDisplayOptions($display_context) {
     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 f83e45ac60e0..620e48c5053e 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 @@ public function getDisplayOptions($display_context) {
     return array('type' => 'hidden');
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function getTargetEntityTypeId() {
+    return $this->entity_type;
+  }
+
   /**
    * {@inheritdoc}
    */
-- 
GitLab