Skip to content
Snippets Groups Projects
Commit 3c6c9912 authored by Pablo López's avatar Pablo López
Browse files

Issue #3529602 by plopesc: FieldStorageDefinitionSubscriber can cause...

Issue #3529602 by plopesc: FieldStorageDefinitionSubscriber can cause unexpected errors for entities with no defined Field Inheritance
parent 2d931339
No related branches found
No related tags found
1 merge request!24Issue #3529602: FieldStorageDefinitionSubscriber can cause unexpected errors...
Pipeline #519764 passed
......@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Drupal\field_inheritance\EventSubscriber;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldStorageDefinitionEvent;
use Drupal\Core\Field\FieldStorageDefinitionEvents;
......@@ -27,10 +28,13 @@ final class FieldStorageDefinitionSubscriber implements EventSubscriberInterface
* The database connection.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
* @param \Drupal\Core\Entity\EntityFieldManagerInterface $entityFieldManager
* The entity field manager.
*/
public function __construct(
private readonly Connection $database,
private readonly EntityTypeManagerInterface $entityTypeManager,
private readonly EntityFieldManagerInterface $entityFieldManager,
) {}
/**
......@@ -38,11 +42,16 @@ final class FieldStorageDefinitionSubscriber implements EventSubscriberInterface
*/
public function onFieldStorageDefinitionCreate(FieldStorageDefinitionEvent $event, $event_name): void {
$entity_type_id = $event->getFieldStorageDefinition()->getTargetEntityTypeId();
$base_fields = $this->entityFieldManager->getBaseFieldDefinitions($entity_type_id);
if (!in_array('field_inheritance', $base_fields)) {
return;
}
$entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
$data_table = $entity_type->getDataTable() ?? $entity_type->getBaseTable();
if ($data_table) {
$this->database->update($data_table)
->fields(['field_inheritance' => serialize([])])
->isNull('field_inheritance')
->execute();
}
$revision_data_table = $entity_type->getRevisionDataTable() ?? $entity_type->getRevisionTable();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment