FieldStorageDefinitionListener.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <?php
  2. namespace Drupal\Core\Field;
  3. use Drupal\Core\Database\DatabaseExceptionWrapper;
  4. use Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface;
  5. use Drupal\Core\Entity\EntityFieldManagerInterface;
  6. use Drupal\Core\Entity\EntityTypeManagerInterface;
  7. use Drupal\Core\Entity\FieldableEntityStorageInterface;
  8. use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
  9. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  10. /**
  11. * Reacts to field storage definition CRUD on behalf of the Entity system.
  12. *
  13. * @see \Drupal\Core\Field\FieldStorageDefinitionEvents
  14. */
  15. class FieldStorageDefinitionListener implements FieldStorageDefinitionListenerInterface {
  16. /**
  17. * The entity type manager.
  18. *
  19. * @var \Drupal\Core\Entity\EntityTypeManagerInterface
  20. */
  21. protected $entityTypeManager;
  22. /**
  23. * The event dispatcher.
  24. *
  25. * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
  26. */
  27. protected $eventDispatcher;
  28. /**
  29. * The entity definition manager.
  30. *
  31. * @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface
  32. */
  33. protected $entityLastInstalledSchemaRepository;
  34. /**
  35. * The entity field manager.
  36. *
  37. * @var \Drupal\Core\Entity\EntityFieldManagerInterface
  38. */
  39. protected $entityFieldManager;
  40. /**
  41. * The deleted fields repository.
  42. *
  43. * @var \Drupal\Core\Field\DeletedFieldsRepositoryInterface
  44. */
  45. protected $deletedFieldsRepository;
  46. /**
  47. * Constructs a new FieldStorageDefinitionListener.
  48. *
  49. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
  50. * The entity type manager.
  51. * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
  52. * The event dispatcher.
  53. * @param \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository
  54. * The entity last installed schema repository.
  55. * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager
  56. * The entity field manager.
  57. * @param \Drupal\Core\Field\DeletedFieldsRepositoryInterface $deleted_fields_repository
  58. * The deleted fields repository.
  59. */
  60. public function __construct(EntityTypeManagerInterface $entity_type_manager, EventDispatcherInterface $event_dispatcher, EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository, EntityFieldManagerInterface $entity_field_manager, DeletedFieldsRepositoryInterface $deleted_fields_repository) {
  61. $this->entityTypeManager = $entity_type_manager;
  62. $this->eventDispatcher = $event_dispatcher;
  63. $this->entityLastInstalledSchemaRepository = $entity_last_installed_schema_repository;
  64. $this->entityFieldManager = $entity_field_manager;
  65. $this->deletedFieldsRepository = $deleted_fields_repository;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function onFieldStorageDefinitionCreate(FieldStorageDefinitionInterface $storage_definition) {
  71. $entity_type_id = $storage_definition->getTargetEntityTypeId();
  72. // @todo Forward this to all interested handlers, not only storage, once
  73. // iterating handlers is possible: https://www.drupal.org/node/2332857.
  74. $storage = clone $this->entityTypeManager->getStorage($entity_type_id);
  75. // Entity type definition updates can change the schema by adding or
  76. // removing entity tables (for example when switching an entity type from
  77. // non-revisionable to revisionable), so CRUD operations on a field storage
  78. // definition need to use the last installed entity type schema.
  79. if ($storage instanceof SqlContentEntityStorage
  80. && ($last_installed_entity_type = $this->entityLastInstalledSchemaRepository->getLastInstalledDefinition($entity_type_id))) {
  81. $storage->setEntityType($last_installed_entity_type);
  82. }
  83. if ($storage instanceof FieldStorageDefinitionListenerInterface) {
  84. $storage->onFieldStorageDefinitionCreate($storage_definition);
  85. }
  86. $this->eventDispatcher->dispatch(FieldStorageDefinitionEvents::CREATE, new FieldStorageDefinitionEvent($storage_definition));
  87. $this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinition($storage_definition);
  88. $this->entityFieldManager->clearCachedFieldDefinitions();
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function onFieldStorageDefinitionUpdate(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) {
  94. $entity_type_id = $storage_definition->getTargetEntityTypeId();
  95. // @todo Forward this to all interested handlers, not only storage, once
  96. // iterating handlers is possible: https://www.drupal.org/node/2332857.
  97. $storage = clone $this->entityTypeManager->getStorage($entity_type_id);
  98. // Entity type definition updates can change the schema by adding or
  99. // removing entity tables (for example when switching an entity type from
  100. // non-revisionable to revisionable), so CRUD operations on a field storage
  101. // definition need to use the last installed entity type schema.
  102. if ($storage instanceof SqlContentEntityStorage
  103. && ($last_installed_entity_type = $this->entityLastInstalledSchemaRepository->getLastInstalledDefinition($entity_type_id))) {
  104. $storage->setEntityType($last_installed_entity_type);
  105. }
  106. if ($storage instanceof FieldStorageDefinitionListenerInterface) {
  107. $storage->onFieldStorageDefinitionUpdate($storage_definition, $original);
  108. }
  109. $this->eventDispatcher->dispatch(FieldStorageDefinitionEvents::UPDATE, new FieldStorageDefinitionEvent($storage_definition, $original));
  110. $this->entityLastInstalledSchemaRepository->setLastInstalledFieldStorageDefinition($storage_definition);
  111. $this->entityFieldManager->clearCachedFieldDefinitions();
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function onFieldStorageDefinitionDelete(FieldStorageDefinitionInterface $storage_definition) {
  117. $entity_type_id = $storage_definition->getTargetEntityTypeId();
  118. // @todo Forward this to all interested handlers, not only storage, once
  119. // iterating handlers is possible: https://www.drupal.org/node/2332857.
  120. $storage = clone $this->entityTypeManager->getStorage($entity_type_id);
  121. // Entity type definition updates can change the schema by adding or
  122. // removing entity tables (for example when switching an entity type from
  123. // non-revisionable to revisionable), so CRUD operations on a field storage
  124. // definition need to use the last installed entity type schema.
  125. if ($storage instanceof SqlContentEntityStorage
  126. && ($last_installed_entity_type = $this->entityLastInstalledSchemaRepository->getLastInstalledDefinition($entity_type_id))) {
  127. $storage->setEntityType($last_installed_entity_type);
  128. }
  129. // Keep the field definition in the deleted fields repository so we can use
  130. // it later during field_purge_batch(), but only if the field has data.
  131. try {
  132. if ($storage_definition instanceof BaseFieldDefinition && $storage instanceof FieldableEntityStorageInterface && $storage->countFieldData($storage_definition, TRUE)) {
  133. $deleted_storage_definition = clone $storage_definition;
  134. $deleted_storage_definition->setDeleted(TRUE);
  135. $this->deletedFieldsRepository->addFieldDefinition($deleted_storage_definition);
  136. $this->deletedFieldsRepository->addFieldStorageDefinition($deleted_storage_definition);
  137. }
  138. }
  139. catch (DatabaseExceptionWrapper $e) {
  140. // This may happen when changing field storage schema, since we are not
  141. // able to use a table mapping matching the passed storage definition.
  142. // @todo Revisit this once we are able to instantiate the table mapping
  143. // properly. See https://www.drupal.org/node/2274017.
  144. }
  145. if ($storage instanceof FieldStorageDefinitionListenerInterface) {
  146. $storage->onFieldStorageDefinitionDelete($storage_definition);
  147. }
  148. $this->eventDispatcher->dispatch(FieldStorageDefinitionEvents::DELETE, new FieldStorageDefinitionEvent($storage_definition));
  149. $this->entityLastInstalledSchemaRepository->deleteLastInstalledFieldStorageDefinition($storage_definition);
  150. $this->entityFieldManager->clearCachedFieldDefinitions();
  151. }
  152. }