diff --git a/core/lib/Drupal/Core/Entity/EntityDefinitionUpdateManager.php b/core/lib/Drupal/Core/Entity/EntityDefinitionUpdateManager.php index 6870ae0..e04d601 100644 --- a/core/lib/Drupal/Core/Entity/EntityDefinitionUpdateManager.php +++ b/core/lib/Drupal/Core/Entity/EntityDefinitionUpdateManager.php @@ -123,7 +123,7 @@ public function applyUpdates() { if (!empty($change_list['entity_type']) && $change_list['entity_type'] == static::DEFINITION_UPDATED) { $entity_type = $this->entityManager->getDefinition($entity_type_id); $original = $this->entityManager->getLastInstalledDefinition($entity_type_id); - $this->entityManager->onEntityTypeUpdate($entity_type, $original); + $this->eventDispatcher->dispatch(EntityTypeEvent::UPDATE, new EntityTypeEvent($entity_type, $original)); } // Process field storage definition changes. @@ -142,7 +142,7 @@ public function applyUpdates() { break; case static::DEFINITION_DELETED: - $this->eventDispatcher->dispatch(FieldStorageDefinitionEvent::DELETE, new FieldStorageDefinitionEvent($storage_definitions[$field_name], $original_storage_definitions[$field_name])); + $this->eventDispatcher->dispatch(FieldStorageDefinitionEvent::DELETE, new FieldStorageDefinitionEvent($original_storage_definitions[$field_name])); break; } } diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index 9c9daab..a22d0fa 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -49,6 +49,7 @@ class EntityManager extends DefaultPluginManager implements EntityManagerInterfa use ContainerAwareTrait; use StringTranslationTrait; + use EntityTypeEventSubscriberTrait; use FieldStorageDefinitionEventSubscriberTrait; /** @@ -202,6 +203,13 @@ public function __construct(\Traversable $namespaces, ModuleHandlerInterface $mo /** * {@inheritdoc} */ + public static function getSubscribedEvents() { + return static::getEntityTypeEvents() + static::getFieldStorageDefinitionEvents(); + } + + /** + * {@inheritdoc} + */ public function clearCachedDefinitions() { parent::clearCachedDefinitions(); $this->clearCachedBundles(); diff --git a/core/lib/Drupal/Core/Entity/EntityTypeEvent.php b/core/lib/Drupal/Core/Entity/EntityTypeEvent.php new file mode 100644 index 0000000..7628763 --- /dev/null +++ b/core/lib/Drupal/Core/Entity/EntityTypeEvent.php @@ -0,0 +1,83 @@ +entityType = $entity_type; + $this->original = $original; + } + + /** + * The field storage definition. + * + * @return \Drupal\Core\Entity\EntityTypeInterface + */ + public function getEntityType() { + return $this->entityType; + } + + /** + * The original field storage definition. + * + * @return \Drupal\Core\Entity\EntityTypeInterface + */ + public function getOriginal() { + return $this->original; + } + +} diff --git a/core/lib/Drupal/Core/Entity/EntityTypeEventSubscriberTrait.php b/core/lib/Drupal/Core/Entity/EntityTypeEventSubscriberTrait.php new file mode 100644 index 0000000..2e9dcd3 --- /dev/null +++ b/core/lib/Drupal/Core/Entity/EntityTypeEventSubscriberTrait.php @@ -0,0 +1,76 @@ +onEntityTypeCreate($event->getEntityType()); + break; + + case EntityTypeEvent::UPDATE: + $this->onEntityTypeUpdate($event->getEntityType(), $event->getOriginal()); + break; + + case EntityTypeEvent::DELETE: + $this->onEntityTypeDelete($event->getEntityType()); + break; + } + } + + /** + * {@inheritdoc} + */ + abstract public function onEntityTypeCreate(EntityTypeInterface $entity_type); + + /** + * {@inheritdoc} + */ + abstract public function onEntityTypeUpdate(EntityTypeInterface $entity_type, EntityTypeInterface $original); + + /** + * {@inheritdoc} + */ + abstract public function onEntityTypeDelete(EntityTypeInterface $entity_type); + +} diff --git a/core/lib/Drupal/Core/Extension/ModuleHandler.php b/core/lib/Drupal/Core/Extension/ModuleHandler.php index ae2f890..bc16084 100644 --- a/core/lib/Drupal/Core/Extension/ModuleHandler.php +++ b/core/lib/Drupal/Core/Extension/ModuleHandler.php @@ -12,6 +12,7 @@ use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\String; use Drupal\Core\Cache\CacheBackendInterface; +use Drupal\Core\Entity\EntityTypeEvent; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -825,10 +826,11 @@ public function install(array $module_list, $enable_dependencies = TRUE) { // so that it can notify all interested handlers. For example, a // SQL-based storage handler can use this as an opportunity to create // the necessary database tables. - $entity_manager = \Drupal::entityManager(); - foreach ($entity_manager->getDefinitions() as $entity_type) { + /** @var \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher */ + $event_dispatcher = \Drupal::service('event_dispatcher'); + foreach (\Drupal::entityManager()->getDefinitions() as $entity_type) { if ($entity_type->getProvider() == $module) { - $entity_manager->onEntityTypeCreate($entity_type); + $event_dispatcher->dispatch(EntityTypeEvent::CREATE, new EntityTypeEvent($entity_type)); } } @@ -963,9 +965,10 @@ public function uninstall(array $module_list, $uninstall_dependents = TRUE) { // deleted, so that it can notify all interested handlers. For example, // a SQL-based storage handler can use this as an opportunity to drop // the corresponding database tables. - foreach ($entity_manager->getDefinitions() as $entity_type) { + $event_dispatcher = \Drupal::service('event_dispatcher'); + foreach (\Drupal::entityManager()->getDefinitions() as $entity_type) { if ($entity_type->getProvider() == $module) { - $entity_manager->onEntityTypeDelete($entity_type); + $event_dispatcher->dispatch(EntityTypeEvent::DELETE, new EntityTypeEvent($entity_type)); } } diff --git a/core/lib/Drupal/Core/Field/FieldStorageDefinitionEvent.php b/core/lib/Drupal/Core/Field/FieldStorageDefinitionEvent.php index 7cb8fe2..991396f 100644 --- a/core/lib/Drupal/Core/Field/FieldStorageDefinitionEvent.php +++ b/core/lib/Drupal/Core/Field/FieldStorageDefinitionEvent.php @@ -2,7 +2,7 @@ /** * @file - * Contains \Drupal\Core\Field\Events\FieldStorageDefinitionEvent. + * Contains \Drupal\Core\Field\FieldStorageDefinitionEvent. */ namespace Drupal\Core\Field; @@ -15,17 +15,23 @@ class FieldStorageDefinitionEvent extends GenericEvent { /** - * TODO + * Event name for field storage definition creation. + * + * @var string */ const CREATE = 'field_storage.definition.create'; /** - * TODO + * Event name for field storage definition update. + * + * @var string */ const UPDATE = 'field_storage.definition.update'; /** - * TODO + * Event name for field storage definition deletion. + * + * @var string */ const DELETE = 'field_storage.definition.delete'; diff --git a/core/lib/Drupal/Core/Field/FieldStorageDefinitionEventSubscriberTrait.php b/core/lib/Drupal/Core/Field/FieldStorageDefinitionEventSubscriberTrait.php index 90d0915..d6eef57 100644 --- a/core/lib/Drupal/Core/Field/FieldStorageDefinitionEventSubscriberTrait.php +++ b/core/lib/Drupal/Core/Field/FieldStorageDefinitionEventSubscriberTrait.php @@ -19,9 +19,14 @@ trait FieldStorageDefinitionEventSubscriberTrait { /** - * {@inheritdoc} + * Returns the subscribed events. + * + * @return array + * An array of subscribed event names. + * + * @see \Symfony\Component\EventDispatcher\EventSubscriberInterface::getSubscribedEvents() */ - public static function getSubscribedEvents() { + public static function getFieldStorageDefinitionEvents() { $event = array('onFieldStorageDefinitionEvent', 100); $events[FieldStorageDefinitionEvent::CREATE][] = $event; $events[FieldStorageDefinitionEvent::UPDATE][] = $event; @@ -48,7 +53,7 @@ public function onFieldStorageDefinitionEvent(FieldStorageDefinitionEvent $event break; case FieldStorageDefinitionEvent::DELETE: - $this->onFieldStorageDefinitionDelete($event->getOriginal()); + $this->onFieldStorageDefinitionDelete($event->getFieldStorageDefinition()); break; } }