From fee42cb6b3578d3d1bed7150ae25b350610ab67c Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Thu, 3 Jul 2014 10:27:57 +0100
Subject: [PATCH] Issue #2247095 by yched, xjm, alexpott: Optimize loading of
 deleted fields.

---
 core/modules/field/src/FieldConfigStorage.php | 26 ++++++++++++-------
 .../field/src/FieldInstanceConfigStorage.php  | 26 ++++++++++++-------
 2 files changed, 32 insertions(+), 20 deletions(-)

diff --git a/core/modules/field/src/FieldConfigStorage.php b/core/modules/field/src/FieldConfigStorage.php
index ff776cab2dec..841942487f2d 100644
--- a/core/modules/field/src/FieldConfigStorage.php
+++ b/core/modules/field/src/FieldConfigStorage.php
@@ -97,19 +97,25 @@ public function loadByProperties(array $conditions = array()) {
     $include_deleted = isset($conditions['include_deleted']) ? $conditions['include_deleted'] : FALSE;
     unset($conditions['include_deleted']);
 
-    // Get fields stored in configuration.
-    if (isset($conditions['entity_type']) && isset($conditions['field_name'])) {
-      // Optimize for the most frequent case where we do have a specific ID.
-      $id = $conditions['entity_type'] . $conditions['field_name'];
-      $fields = $this->loadMultiple(array($id));
-    }
-    else {
-      // No specific ID, we need to examine all existing fields.
-      $fields = $this->loadMultiple();
+    $fields = array();
+
+    // Get fields stored in configuration. If we are explicitly looking for
+    // deleted fields only, this can be skipped, because they will be retrieved
+    // from state below.
+    if (empty($conditions['deleted'])) {
+      if (isset($conditions['entity_type']) && isset($conditions['field_name'])) {
+        // Optimize for the most frequent case where we do have a specific ID.
+        $id = $conditions['entity_type'] . $conditions['field_name'];
+        $fields = $this->loadMultiple(array($id));
+      }
+      else {
+        // No specific ID, we need to examine all existing fields.
+        $fields = $this->loadMultiple();
+      }
     }
 
     // Merge deleted fields (stored in state) if needed.
-    if ($include_deleted) {
+    if ($include_deleted || !empty($conditions['deleted'])) {
       $deleted_fields = $this->state->get('field.field.deleted') ?: array();
       foreach ($deleted_fields as $id => $config) {
         $fields[$id] = $this->create($config);
diff --git a/core/modules/field/src/FieldInstanceConfigStorage.php b/core/modules/field/src/FieldInstanceConfigStorage.php
index e9b15dca5a6e..20dc083089c2 100644
--- a/core/modules/field/src/FieldInstanceConfigStorage.php
+++ b/core/modules/field/src/FieldInstanceConfigStorage.php
@@ -104,19 +104,25 @@ public function loadByProperties(array $conditions = array()) {
     $include_deleted = isset($conditions['include_deleted']) ? $conditions['include_deleted'] : FALSE;
     unset($conditions['include_deleted']);
 
-    // Get instances stored in configuration.
-    if (isset($conditions['entity_type']) && isset($conditions['bundle']) && isset($conditions['field_name'])) {
-      // Optimize for the most frequent case where we do have a specific ID.
-      $id = $conditions['entity_type'] . '.' . $conditions['bundle'] . '.' . $conditions['field_name'];
-      $instances = $this->loadMultiple(array($id));
-    }
-    else {
-      // No specific ID, we need to examine all existing instances.
-      $instances = $this->loadMultiple();
+    $instances = array();
+
+    // Get instances stored in configuration. If we are explicitly looking for
+    // deleted instances only, this can be skipped, because they will be
+    // retrieved from state below.
+    if (empty($conditions['deleted'])) {
+      if (isset($conditions['entity_type']) && isset($conditions['bundle']) && isset($conditions['field_name'])) {
+        // Optimize for the most frequent case where we do have a specific ID.
+        $id = $conditions['entity_type'] . '.' . $conditions['bundle'] . '.' . $conditions['field_name'];
+        $instances = $this->loadMultiple(array($id));
+      }
+      else {
+        // No specific ID, we need to examine all existing instances.
+        $instances = $this->loadMultiple();
+      }
     }
 
     // Merge deleted instances (stored in state) if needed.
-    if ($include_deleted) {
+    if ($include_deleted || !empty($conditions['deleted'])) {
       $deleted_instances = $this->state->get('field.instance.deleted') ?: array();
       $deleted_fields = $this->state->get('field.field.deleted') ?: array();
       foreach ($deleted_instances as $id => $config) {
-- 
GitLab