Skip to content
Snippets Groups Projects

Add purge queue static cache to avoid multiple processing of same entities

1 file
+ 41
5
Compare changes
  • Side-by-side
  • Inline
+ 41
5
@@ -15,6 +15,13 @@ use Drupal\entity_reference_revisions\Plugin\Field\FieldType\EntityReferenceRevi
*/
final class EntityHooks {
/**
* The processed entities cache.
*
* @var array
*/
protected static array $queuedEntities = [];
/**
* Constructor of the entity hooks service.
*/
@@ -24,6 +31,29 @@ final class EntityHooks {
protected QueueFactory $queueFactory,
) {}
/**
* Check that a specific entity has been already added to the purger queue.
*
* @param string $key
* Key of the entity to check if added to queue.
*
* @return bool
* TRUE if the entity has already been added to the queue, FALSE otherwise.
*/
public static function isQueued($key): bool {
return isset(EntityHooks::$queuedEntities[$key]);
}
/**
* Indicate that an entity has been added to the purger queue.
*
* @param string $key
* The key of the entity added to the purger queue.
*/
public static function addToQueue($key) {
EntityHooks::$queuedEntities[$key] = TRUE;
}
/**
* Implements hook_entity_delete() and hook_entity_revision_delete().
*
@@ -36,7 +66,9 @@ final class EntityHooks {
if (!$entity instanceof FieldableEntityInterface) {
return;
}
if (self::isQueued($entity->getEntityTypeId() . '_' . $entity->id())) {
return;
}
foreach ($entity->getFieldDefinitions() as $field_name => $field_definition) {
$field_class = $this->fieldTypeManager->getPluginClass($field_definition->getType());
if ($field_class == EntityReferenceRevisionsItem::class || is_subclass_of($field_class, EntityReferenceRevisionsItem::class)) {
@@ -63,10 +95,14 @@ final class EntityHooks {
}
$entity_ids = array_unique($entity_ids);
foreach ($entity_ids as $revision_id => $entity_id) {
$this->queueFactory->get('entity_reference_revisions_orphan_purger')->createItem([
'entity_id' => $entity_id,
'entity_type_id' => $target_entity_type_id,
]);
$entity_queue_key = $target_entity_type_id . '_' . $entity_id;
if (!self::isQueued($entity_queue_key)) {
$this->queueFactory->get('entity_reference_revisions_orphan_purger')->createItem([
'entity_id' => $entity_id,
'entity_type_id' => $target_entity_type_id,
]);
self::addToQueue($entity_queue_key);
}
}
}
}
Loading