function entity_reference_revisions_entity_delete in Entity Reference Revisions 8
Implements hook_entity_delete().
Performs garbage collection for composite entities that were not removed by EntityReferenceRevisionsItem.
File
- ./
entity_reference_revisions.module, line 310 - Provides a field that can reference other entities.
Code
function entity_reference_revisions_entity_delete(EntityInterface $entity) {
if (!$entity instanceof FieldableEntityInterface) {
return;
}
$entity_type_manager = \Drupal::entityTypeManager();
/** @var \Drupal\Core\Field\FieldTypePluginManager $field_type_manager */
$field_type_manager = \Drupal::service('plugin.manager.field.field_type');
foreach ($entity
->getFieldDefinitions() as $field_name => $field_definition) {
$field_class = $field_type_manager
->getPluginClass($field_definition
->getType());
if ($field_class == EntityReferenceRevisionsItem::class || is_subclass_of($field_class, EntityReferenceRevisionsItem::class)) {
$target_entity_type_id = $field_definition
->getSetting('target_type');
$target_entity_storage = $entity_type_manager
->getStorage($target_entity_type_id);
$target_entity_type = $target_entity_storage
->getEntityType();
$parent_type_field = $target_entity_type
->get('entity_revision_parent_type_field');
$parent_id_field = $target_entity_type
->get('entity_revision_parent_id_field');
$parent_name_field = $target_entity_type
->get('entity_revision_parent_field_name_field');
if ($parent_type_field && $parent_id_field && $parent_name_field) {
$entity_ids = $target_entity_storage
->getQuery()
->allRevisions()
->condition($parent_type_field, $entity
->getEntityTypeId())
->condition($parent_id_field, $entity
->id())
->condition($parent_name_field, $field_name)
->execute();
if (empty($entity_ids)) {
continue;
}
$entity_ids = array_unique($entity_ids);
foreach ($entity_ids as $revision_id => $entity_id) {
\Drupal::queue('entity_reference_revisions_orphan_purger')
->createItem([
'entity_id' => $entity_id,
'entity_type_id' => $target_entity_type_id,
]);
}
}
}
}
}