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;
*/
protected $cacheBackend;
+ /**
+ * Stores the latest revision IDs for entities.
+ *
+ * @var array
+ */
+ protected $latestRevisionIds = [];
+
/**
* Constructs a ContentEntityStorageBase object.
*
* 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;
return new static(
$entity_type,
$container->get('entity.manager'),
- $container->get('cache.entity')
+ $container->get('cache.entity'),
+ $container->get('entity.memory_cache')
);
}
/** @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
// 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;
}
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];
}
/**
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];
}
/**
*/
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";
- }
-
}