From 9eb4efb98614da9cb752f687bc860c0c029e3c07 Mon Sep 17 00:00:00 2001
From: webchick <drupal@webchick.net>
Date: Wed, 10 Dec 2014 07:40:28 -0800
Subject: [PATCH] Issue #2390615 by alexpott: Add method to determine config
 dependency key depending on entity type

---
 .../lib/Drupal/Core/Config/Entity/ConfigEntityType.php |  7 +++++++
 core/lib/Drupal/Core/Entity/ContentEntityType.php      |  7 +++++++
 core/lib/Drupal/Core/Entity/EntityType.php             | 10 ++++++++++
 core/lib/Drupal/Core/Entity/EntityTypeInterface.php    | 10 ++++++++++
 .../Plugin/Field/FieldType/EntityReferenceItem.php     |  4 +---
 5 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/core/lib/Drupal/Core/Config/Entity/ConfigEntityType.php b/core/lib/Drupal/Core/Config/Entity/ConfigEntityType.php
index 9948c74418ad..ef4616809166 100644
--- a/core/lib/Drupal/Core/Config/Entity/ConfigEntityType.php
+++ b/core/lib/Drupal/Core/Config/Entity/ConfigEntityType.php
@@ -125,4 +125,11 @@ public function getDataTable() {
     return FALSE;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function getConfigDependencyKey() {
+    return 'config';
+  }
+
 }
diff --git a/core/lib/Drupal/Core/Entity/ContentEntityType.php b/core/lib/Drupal/Core/Entity/ContentEntityType.php
index 213c097be288..f9de958447ab 100644
--- a/core/lib/Drupal/Core/Entity/ContentEntityType.php
+++ b/core/lib/Drupal/Core/Entity/ContentEntityType.php
@@ -29,4 +29,11 @@ public function getConfigPrefix() {
     return FALSE;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function getConfigDependencyKey() {
+    return 'content';
+  }
+
 }
diff --git a/core/lib/Drupal/Core/Entity/EntityType.php b/core/lib/Drupal/Core/Entity/EntityType.php
index 5ea863d94101..faf8856e5a1b 100644
--- a/core/lib/Drupal/Core/Entity/EntityType.php
+++ b/core/lib/Drupal/Core/Entity/EntityType.php
@@ -680,4 +680,14 @@ public function getListCacheTags() {
     return $this->list_cache_tags;
   }
 
+  /**
+   * {@inheritdoc}
+   */
+  public function getConfigDependencyKey() {
+    // Return 'content' for the default implementation as important distinction
+    // is that dependencies on other configuration entities are hard
+    // dependencies and have to exist before creating the dependent entity.
+    return 'content';
+  }
+
 }
diff --git a/core/lib/Drupal/Core/Entity/EntityTypeInterface.php b/core/lib/Drupal/Core/Entity/EntityTypeInterface.php
index f8237a5a37d6..26c913be533d 100644
--- a/core/lib/Drupal/Core/Entity/EntityTypeInterface.php
+++ b/core/lib/Drupal/Core/Entity/EntityTypeInterface.php
@@ -638,4 +638,14 @@ public function setUriCallback($callback);
    * @return string[]
    */
   public function getListCacheTags();
+
+  /**
+   * Gets the key that is used to store configuration dependencies.
+   *
+   * @return string
+   *   The key to be used in configuration dependencies when storing
+   *   dependencies on entities of this type.
+   */
+  public function getConfigDependencyKey();
+
 }
diff --git a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php
index a6c6bedf94fd..87868d6e18f3 100644
--- a/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php
+++ b/core/lib/Drupal/Core/Field/Plugin/Field/FieldType/EntityReferenceItem.php
@@ -247,17 +247,15 @@ public function hasNewEntity() {
    */
   public static function calculateDependencies(FieldDefinitionInterface $field_definition) {
     $dependencies = [];
-
     if (is_array($field_definition->default_value) && count($field_definition->default_value)) {
       $target_entity_type = \Drupal::entityManager()->getDefinition($field_definition->getFieldStorageDefinition()->getSetting('target_type'));
-      $key = $target_entity_type instanceof ConfigEntityType ? 'config' : 'content';
       foreach ($field_definition->default_value as $default_value) {
         if (is_array($default_value) && isset($default_value['target_uuid'])) {
           $entity = \Drupal::entityManager()->loadEntityByUuid($target_entity_type->id(), $default_value['target_uuid']);
           // If the entity does not exist do not create the dependency.
           // @see \Drupal\Core\Field\EntityReferenceFieldItemList::processDefaultValue()
           if ($entity) {
-            $dependencies[$key][] = $entity->getConfigDependencyName();
+            $dependencies[$target_entity_type->getConfigDependencyKey()][] = $entity->getConfigDependencyName();
           }
         }
       }
-- 
GitLab