Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / lib / Drupal / Core / Entity / ContentEntityStorageBase.php
index a004ebad8142fabee4c9e29145bc573f5e6a60a9..d38244bd65dd7536bbcc467dc9aa7eaff0d19bd3 100644 (file)
@@ -4,8 +4,10 @@ namespace Drupal\Core\Entity;
 
 use Drupal\Core\Cache\Cache;
 use Drupal\Core\Cache\CacheBackendInterface;
+use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
 use Drupal\Core\Field\FieldDefinitionInterface;
 use Drupal\Core\Field\FieldStorageDefinitionInterface;
+use Drupal\Core\Language\LanguageInterface;
 use Drupal\Core\TypedData\TranslationStatusInterface;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 
@@ -35,6 +37,13 @@ abstract class ContentEntityStorageBase extends EntityStorageBase implements Con
    */
   protected $cacheBackend;
 
+  /**
+   * Stores the latest revision IDs for entities.
+   *
+   * @var array
+   */
+  protected $latestRevisionIds = [];
+
   /**
    * Constructs a ContentEntityStorageBase object.
    *
@@ -44,9 +53,11 @@ abstract class ContentEntityStorageBase extends EntityStorageBase implements Con
    *   The entity manager.
    * @param \Drupal\Core\Cache\CacheBackendInterface $cache
    *   The cache backend to be used.
+   * @param \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface|null $memory_cache
+   *   The memory cache backend.
    */
-  public function __construct(EntityTypeInterface $entity_type, EntityManagerInterface $entity_manager, CacheBackendInterface $cache) {
-    parent::__construct($entity_type);
+  public function __construct(EntityTypeInterface $entity_type, EntityManagerInterface $entity_manager, CacheBackendInterface $cache, MemoryCacheInterface $memory_cache = NULL) {
+    parent::__construct($entity_type, $memory_cache);
     $this->bundleKey = $this->entityType->getKey('bundle');
     $this->entityManager = $entity_manager;
     $this->cacheBackend = $cache;
@@ -59,7 +70,8 @@ abstract class ContentEntityStorageBase extends EntityStorageBase implements Con
     return new static(
       $entity_type,
       $container->get('entity.manager'),
-      $container->get('cache.entity')
+      $container->get('cache.entity'),
+      $container->get('entity.memory_cache')
     );
   }
 
@@ -237,6 +249,8 @@ abstract class ContentEntityStorageBase extends EntityStorageBase implements Con
     /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
     $new_revision = clone $entity;
 
+    $original_keep_untranslatable_fields = $keep_untranslatable_fields;
+
     // For translatable entities, create a merged revision of the active
     // translation and the other translations in the default revision. This
     // permits the creation of pending revisions that can always be saved as the
@@ -307,6 +321,11 @@ abstract class ContentEntityStorageBase extends EntityStorageBase implements Con
     // to the correct translation.
     $new_revision->setRevisionTranslationAffected(TRUE);
 
+    // Notify modules about the new revision.
+    $arguments = [$new_revision, $entity, $original_keep_untranslatable_fields];
+    $this->moduleHandler()->invokeAll($this->entityTypeId . '_revision_create', $arguments);
+    $this->moduleHandler()->invokeAll('entity_revision_create', $arguments);
+
     return $new_revision;
   }
 
@@ -339,13 +358,17 @@ abstract class ContentEntityStorageBase extends EntityStorageBase implements Con
       return NULL;
     }
 
-    $result = $this->getQuery()
-      ->latestRevision()
-      ->condition($this->entityType->getKey('id'), $entity_id)
-      ->accessCheck(FALSE)
-      ->execute();
+    if (!isset($this->latestRevisionIds[$entity_id][LanguageInterface::LANGCODE_DEFAULT])) {
+      $result = $this->getQuery()
+        ->latestRevision()
+        ->condition($this->entityType->getKey('id'), $entity_id)
+        ->accessCheck(FALSE)
+        ->execute();
 
-    return key($result);
+      $this->latestRevisionIds[$entity_id][LanguageInterface::LANGCODE_DEFAULT] = key($result);
+    }
+
+    return $this->latestRevisionIds[$entity_id][LanguageInterface::LANGCODE_DEFAULT];
   }
 
   /**
@@ -360,16 +383,19 @@ abstract class ContentEntityStorageBase extends EntityStorageBase implements Con
       return $this->getLatestRevisionId($entity_id);
     }
 
-    $result = $this->getQuery()
-      ->allRevisions()
-      ->condition($this->entityType->getKey('id'), $entity_id)
-      ->condition($this->entityType->getKey('revision_translation_affected'), 1, '=', $langcode)
-      ->range(0, 1)
-      ->sort($this->entityType->getKey('revision'), 'DESC')
-      ->accessCheck(FALSE)
-      ->execute();
-
-    return key($result);
+    if (!isset($this->latestRevisionIds[$entity_id][$langcode])) {
+      $result = $this->getQuery()
+        ->allRevisions()
+        ->condition($this->entityType->getKey('id'), $entity_id)
+        ->condition($this->entityType->getKey('revision_translation_affected'), 1, '=', $langcode)
+        ->range(0, 1)
+        ->sort($this->entityType->getKey('revision'), 'DESC')
+        ->accessCheck(FALSE)
+        ->execute();
+
+      $this->latestRevisionIds[$entity_id][$langcode] = key($result);
+    }
+    return $this->latestRevisionIds[$entity_id][$langcode];
   }
 
   /**
@@ -990,34 +1016,23 @@ abstract class ContentEntityStorageBase extends EntityStorageBase implements Con
    */
   public function resetCache(array $ids = NULL) {
     if ($ids) {
-      $cids = [];
-      foreach ($ids as $id) {
-        unset($this->entities[$id]);
-        $cids[] = $this->buildCacheId($id);
-      }
+      parent::resetCache($ids);
       if ($this->entityType->isPersistentlyCacheable()) {
+        $cids = [];
+        foreach ($ids as $id) {
+          unset($this->latestRevisionIds[$id]);
+          $cids[] = $this->buildCacheId($id);
+        }
         $this->cacheBackend->deleteMultiple($cids);
       }
     }
     else {
-      $this->entities = [];
+      parent::resetCache();
       if ($this->entityType->isPersistentlyCacheable()) {
         Cache::invalidateTags([$this->entityTypeId . '_values']);
       }
+      $this->latestRevisionIds = [];
     }
   }
 
-  /**
-   * Builds the cache ID for the passed in entity ID.
-   *
-   * @param int $id
-   *   Entity ID for which the cache ID should be built.
-   *
-   * @return string
-   *   Cache ID that can be passed to the cache backend.
-   */
-  protected function buildCacheId($id) {
-    return "values:{$this->entityTypeId}:$id";
-  }
-
 }