From 719d87598d528209e0ca2b3cbf2fec68c0a4305b Mon Sep 17 00:00:00 2001
From: Axel Rutz <axel.rutz@clever-systems.net>
Date: Fri, 4 Nov 2016 16:14:33 +0100
Subject: [PATCH] Issue #986024 by axel.rutz, fago: Inconsistent static entity
 cache in hook_entity_update

---
 includes/file.inc        | 10 ++++++++--
 modules/node/node.module |  5 +++--
 modules/user/user.module |  6 ++++--
 3 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/includes/file.inc b/includes/file.inc
index de9d17d..3d0c8b7 100644
--- a/includes/file.inc
+++ b/includes/file.inc
@@ -612,12 +612,20 @@ function file_save(stdClass $file) {
 
   if (empty($file->fid)) {
     drupal_write_record('file_managed', $file);
+
+    // Clear the static loading cache.
+    entity_get_controller('file')->resetCache(array($file->fid));
+
     // Inform modules about the newly added file.
     module_invoke_all('file_insert', $file);
     module_invoke_all('entity_insert', $file, 'file');
   }
   else {
     drupal_write_record('file_managed', $file, 'fid');
+
+    // Clear the static loading cache.
+    entity_get_controller('file')->resetCache(array($file->fid));
+
     // Inform modules that the file has been updated.
     module_invoke_all('file_update', $file);
     module_invoke_all('entity_update', $file, 'file');
@@ -625,8 +633,6 @@ function file_save(stdClass $file) {
 
   // Clear internal properties.
   unset($file->original);
-  // Clear the static loading cache.
-  entity_get_controller('file')->resetCache(array($file->fid));
 
   return $file;
 }
diff --git a/modules/node/node.module b/modules/node/node.module
index 1d88834..f62f8d2 100644
--- a/modules/node/node.module
+++ b/modules/node/node.module
@@ -1177,6 +1177,9 @@ function node_save($node) {
     $function = "field_attach_$op";
     $function('node', $node);
 
+    // Clear the static loading cache.
+    entity_get_controller('node')->resetCache(array($node->nid));
+
     module_invoke_all('node_' . $op, $node);
     module_invoke_all('entity_' . $op, $node, 'node');
 
@@ -1186,8 +1189,6 @@ function node_save($node) {
     // Clear internal properties.
     unset($node->is_new);
     unset($node->original);
-    // Clear the static loading cache.
-    entity_get_controller('node')->resetCache(array($node->nid));
 
     // Ignore slave server temporarily to give time for the
     // saved node to be propagated to the slave.
diff --git a/modules/user/user.module b/modules/user/user.module
index b818d79..422b99a 100644
--- a/modules/user/user.module
+++ b/modules/user/user.module
@@ -589,6 +589,10 @@ function user_save($account, $edit = array(), $category = 'account') {
 
       field_attach_insert('user', $account);
       $edit = (array) $account;
+
+      // Clear the static loading cache.
+      entity_get_controller('user')->resetCache(array($account->uid));
+
       user_module_invoke('insert', $edit, $account, $category);
       module_invoke_all('entity_insert', $account, 'user');
 
@@ -609,8 +613,6 @@ function user_save($account, $edit = array(), $category = 'account') {
     // Clear internal properties.
     unset($account->is_new);
     unset($account->original);
-    // Clear the static loading cache.
-    entity_get_controller('user')->resetCache(array($account->uid));
 
     return $account;
   }
-- 
2.7.4

