diff --git a/core/core.services.yml b/core/core.services.yml index 0c55e94..4f556ca 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -560,7 +560,7 @@ services: arguments: ['@entity_type.manager', '@entity_field.manager', '@keyvalue', '@cache.discovery'] entity.module_install.subscriber: class: \Drupal\Core\EventSubscriber\ModuleInstallerEntitySubscriber - arguments: ['@entity_type.manager', '@entity_field.manager', '@entity.definition_update_manager'] + arguments: ['@kernel'] tags: - { name: event_subscriber } entity.form_builder: diff --git a/core/lib/Drupal/Core/EventSubscriber/ModuleInstallerEntitySubscriber.php b/core/lib/Drupal/Core/EventSubscriber/ModuleInstallerEntitySubscriber.php index d546660..00b7b64 100644 --- a/core/lib/Drupal/Core/EventSubscriber/ModuleInstallerEntitySubscriber.php +++ b/core/lib/Drupal/Core/EventSubscriber/ModuleInstallerEntitySubscriber.php @@ -1,12 +1,8 @@ kernel = $kernel; + } /** - * The entity update manager. + * Gets the entity definition update manager. * - * @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface + * The manager is retrieved dynamically, because the container is rebuilt + * after module installation. + * + * @return \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface */ - protected $entityUpdateManager; + protected function getEntityDefinitionUpdateManager() { + return $this->kernel->getContainer()->get('entity.definition_update_manager'); + } /** - * Creates a new ModuleInstallerEntitySubscriber instance. + * Gets the entity definition update manager. + * + * The manager is retrieved dynamically, because the container is rebuilt + * after module installation. + * + * @return \Drupal\Core\Entity\EntityFieldManagerInterface + */ + protected function getEntityFieldManager() { + return $this->kernel->getContainer()->get('entity_field.manager'); + } + + /** + * Gets the entity type manager. + * + * The manager is retrieved dynamically, because the container is rebuilt + * after module installation. * - * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager - * The entity type manager. - * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager - * The entity field manager. - * @param \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface $entity_update_manager - * The entity update manager. + * @return \Drupal\Core\Entity\EntityTypeManagerInterface */ - public function __construct(\Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager, \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager, \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface $entity_update_manager) { - $this->entityTypeManager = $entity_type_manager; - $this->entityFieldManager = $entity_field_manager; - $this->entityUpdateManager = $entity_update_manager; + protected function getEntityTypeManager() { + return $this->kernel->getContainer()->get('entity_type.manager'); } /** @@ -67,21 +79,21 @@ public function onModuleInstall(ModuleEvent $event) { // field storage definitions are new. For example, a SQL-based storage // handler can use this as an opportunity to create the necessary // database tables. - foreach (\Drupal::entityTypeManager()->getDefinitions() as $entity_type) { + foreach ($this->getEntityTypeManager()->getDefinitions() as $entity_type) { if ($entity_type->getProvider() == $module) { - \Drupal::entityDefinitionUpdateManager()->installEntityType($entity_type); + $this->getEntityDefinitionUpdateManager()->installEntityType($entity_type); } elseif ($entity_type->isSubclassOf(FieldableEntityInterface::CLASS)) { // The module being installed may be adding new fields to existing // entity types. Field definitions for any entity type defined by // the module are handled in the if branch. - foreach ($this->entityFieldManager->getFieldStorageDefinitions($entity_type->id()) as $storage_definition) { + foreach ($this->getEntityFieldManager()->getFieldStorageDefinitions($entity_type->id()) as $storage_definition) { if ($storage_definition->getProvider() == $module) { // If the module being installed is also defining a storage key // for the entity type, the entity schema may not exist yet. It // will be created later in that case. try { - $this->entityUpdateManager->installFieldStorageDefinition($storage_definition->getName(), $entity_type->id(), $module, $storage_definition); + $this->getEntityDefinitionUpdateManager()->installFieldStorageDefinition($storage_definition->getName(), $entity_type->id(), $module, $storage_definition); } catch (EntityStorageException $e) { watchdog_exception('system', $e, 'An error occurred while notifying the creation of the @name field storage definition: "!message" in %function (line %line of %file).', ['@name' => $storage_definition->getName()]); @@ -103,9 +115,9 @@ public function onModuleUninstall(ModuleEvent $event) { // Notify interested components that this module's entity types are being // deleted. For example, a SQL-based storage handler can use this as an // opportunity to drop the corresponding database tables. - foreach (\Drupal::entityTypeManager()->getDefinitions() as $entity_type) { + foreach ($this->getEntityTypeManager()->getDefinitions() as $entity_type) { if ($entity_type->getProvider() == $module) { - \Drupal::entityDefinitionUpdateManager()->uninstallEntityType($entity_type); + $this->getEntityDefinitionUpdateManager()->uninstallEntityType($entity_type); } elseif ($entity_type->isSubclassOf(FieldableEntityInterface::CLASS)) { // The module being uninstalled may be removing fields from existing @@ -113,12 +125,12 @@ public function onModuleUninstall(ModuleEvent $event) { // the module are handled in the if branch. $entity_type_id = $entity_type->id(); /** @var \Drupal\Core\Entity\FieldableEntityStorageInterface $storage */ - $storage = $this->entityTypeManager->getStorage($entity_type_id); - foreach ($this->entityFieldManager->getFieldStorageDefinitions($entity_type_id) as $storage_definition) { + $storage = $this->getEntityTypeManager()->getStorage($entity_type_id); + foreach ($this->getEntityFieldManager()->getFieldStorageDefinitions($entity_type_id) as $storage_definition) { // @todo We need to trigger field purging here. // See https://www.drupal.org/node/2282119. if ($storage_definition->getProvider() == $module && !$storage->countFieldData($storage_definition, TRUE)) { - $this->entityUpdateManager->uninstallFieldStorageDefinition($storage_definition); + $this->getEntityDefinitionUpdateManager()->uninstallFieldStorageDefinition($storage_definition); } } } diff --git a/core/lib/Drupal/Core/Extension/ModuleEvent.php b/core/lib/Drupal/Core/Extension/ModuleEvent.php index 83d5234..472f46c 100644 --- a/core/lib/Drupal/Core/Extension/ModuleEvent.php +++ b/core/lib/Drupal/Core/Extension/ModuleEvent.php @@ -1,28 +1,26 @@ module = $module; @@ -30,7 +28,7 @@ public function __construct($module) { /** - * Returns the installed / uninstall module machine name. + * Returns the module's machine name. * * @return string */