Commit ac95b851 authored by jhedstrom's avatar jhedstrom Committed by GitHub

Merge pull request #201 from kevin-dutra/199-entity-delete-target-type

#199 - Incorrect messages deleted after entity delete.
parents 927b7a08 842aff12
......@@ -38,13 +38,12 @@ function message_entity_delete(EntityInterface $entity) {
// Keyed by message ID, pointing to array of the relevant field names.
$check_mids = [];
$fields_ids = \Drupal::entityQuery('field_storage_config')->execute();
/** @var FieldStorageConfig[] $fields */
$fields = entity_load_multiple('field_storage_config', array_keys($fields_ids));
/** @var \Drupal\Core\Field\FieldStorageDefinitionInterface[] $fields */
$fields = \Drupal::entityTypeManager()->getStorage('field_storage_config')->loadMultiple();
// Search for fields in which messages referenced the deleted entity.
foreach ($fields as $field) {
if ($field->get('entity_type') != 'message') {
if ($field->getTargetEntityTypeId() != 'message') {
// This field isn't used in any message.
continue;
}
......@@ -55,6 +54,11 @@ function message_entity_delete(EntityInterface $entity) {
continue;
}
// Only delete references if the referenced entity is the correct type.
if ($field->getSetting('target_type') != $entity->getEntityTypeId()) {
continue;
}
$query = \Drupal::entityQuery('message');
$results = $query
->condition($field->getName(), $entity->id())
......
......@@ -77,7 +77,7 @@ class MessageEntityDelete extends MessageTestBase {
$this->nodeType = $this->drupalCreateContentType();
for ($i = 0; $i <= 5; $i++) {
for ($i = 0; $i <= 6; $i++) {
entity_create('node', [
'type' => $this->nodeType->id(),
'title' => 'Node ' . $i,
......@@ -239,6 +239,19 @@ class MessageEntityDelete extends MessageTestBase {
$account->delete();
$cron->run();
$this->assertFalse(Message::load($message->id()), 'Message deleted after deleting single referenced user.');
// Test that only messages with a reference to the correct TYPE of entity
// get deleted.
$message_one = Message::create(['template' => 'dummy_message']);
$message_one->set('field_node_reference', 5);
$message_one->save();
$message_two = Message::create(['template' => 'dummy_message']);
$message_two->set('field_term_reference', 5);
$message_two->save();
Node::load(5)->delete();
$cron->run();
$this->assertFalse(Message::load($message_one->id()), 'Message with node deleted after deleting referenced node.');
$this->assertTrue(Message::load($message_two->id()), 'Message with term remains after deleting a node with the same ID.');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment