Chris@0: 'entity.manager']; Chris@18: Chris@18: /** Chris@18: * The entity field manager service. Chris@0: * Chris@18: * @var \Drupal\Core\Entity\EntityFieldManagerInterface Chris@0: */ Chris@18: protected $entityFieldManager; Chris@18: Chris@18: /** Chris@18: * The entity type listener service. Chris@18: * Chris@18: * @var \Drupal\Core\Entity\EntityTypeListenerInterface Chris@18: */ Chris@18: protected $entityTypeListener; Chris@18: Chris@18: /** Chris@18: * The entity type manager service. Chris@18: * Chris@18: * @var \Drupal\Core\Entity\EntityTypeManagerInterface Chris@18: */ Chris@18: protected $entityTypeManager; Chris@18: Chris@18: /** Chris@18: * The field storage definition listener service. Chris@18: * Chris@18: * @var \Drupal\Core\Field\FieldStorageDefinitionListenerInterface Chris@18: */ Chris@18: protected $fieldStorageDefinitionListener; Chris@0: Chris@0: /** Chris@17: * The last installed schema repository. Chris@17: * Chris@17: * @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface Chris@17: */ Chris@17: protected $entityLastInstalledSchemaRepository; Chris@17: Chris@17: /** Chris@0: * Constructs a new EntityDefinitionUpdateManager. Chris@0: * Chris@18: * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager Chris@18: * The entity type manager service. Chris@17: * @param \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository Chris@17: * The last installed schema repository service. Chris@18: * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager Chris@18: * The entity field manager service. Chris@18: * @param \Drupal\Core\Entity\EntityTypeListenerInterface $entity_type_listener Chris@18: * The entity type listener interface. Chris@18: * @param \Drupal\Core\Field\FieldStorageDefinitionListenerInterface $field_storage_definition_listener Chris@18: * The field storage definition listener service. Chris@0: */ Chris@18: public function __construct(EntityTypeManagerInterface $entity_type_manager, EntityLastInstalledSchemaRepositoryInterface $entity_last_installed_schema_repository = NULL, EntityFieldManagerInterface $entity_field_manager = NULL, EntityTypeListenerInterface $entity_type_listener = NULL, FieldStorageDefinitionListenerInterface $field_storage_definition_listener = NULL) { Chris@18: if ($entity_type_manager instanceof EntityManagerInterface) { Chris@18: @trigger_error('Passing the entity.manager service to EntityDefinitionUpdateManager::__construct() is deprecated in Drupal 8.7.0 and will be removed before Drupal 9.0.0. Pass the new dependencies instead. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); Chris@18: $this->entityTypeManager = \Drupal::entityTypeManager(); Chris@18: } Chris@18: else { Chris@18: $this->entityTypeManager = $entity_type_manager; Chris@18: } Chris@17: Chris@18: if ($entity_last_installed_schema_repository) { Chris@18: $this->entityLastInstalledSchemaRepository = $entity_last_installed_schema_repository; Chris@17: } Chris@18: else { Chris@18: @trigger_error('The entity.last_installed_schema.repository service must be passed to EntityDefinitionUpdateManager::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); Chris@18: $this->entityLastInstalledSchemaRepository = \Drupal::service('entity.last_installed_schema.repository'); Chris@18: } Chris@18: Chris@18: if ($entity_field_manager) { Chris@18: $this->entityFieldManager = $entity_field_manager; Chris@18: } Chris@18: else { Chris@18: @trigger_error('The entity_field.manager service must be passed to EntityDefinitionUpdateManager::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); Chris@18: $this->entityFieldManager = \Drupal::service('entity_field.manager'); Chris@18: } Chris@18: Chris@18: if ($entity_type_listener) { Chris@18: $this->entityTypeListener = $entity_type_listener; Chris@18: } Chris@18: else { Chris@18: @trigger_error('The entity_type.listener service must be passed to EntityDefinitionUpdateManager::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); Chris@18: $this->entityTypeListener = \Drupal::service('entity_type.listener'); Chris@18: } Chris@18: Chris@18: if ($field_storage_definition_listener) { Chris@18: $this->fieldStorageDefinitionListener = $field_storage_definition_listener; Chris@18: } Chris@18: else { Chris@18: @trigger_error('The field_storage_definition.listener service must be passed to EntityDefinitionUpdateManager::__construct(), it is required before Drupal 9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED); Chris@18: $this->fieldStorageDefinitionListener = \Drupal::service('field_storage_definition.listener'); Chris@18: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function needsUpdates() { Chris@0: return (bool) $this->getChangeList(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getChangeSummary() { Chris@0: $summary = []; Chris@0: Chris@0: foreach ($this->getChangeList() as $entity_type_id => $change_list) { Chris@0: // Process entity type definition changes. Chris@0: if (!empty($change_list['entity_type'])) { Chris@18: $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); Chris@0: Chris@0: switch ($change_list['entity_type']) { Chris@0: case static::DEFINITION_CREATED: Chris@0: $summary[$entity_type_id][] = $this->t('The %entity_type entity type needs to be installed.', ['%entity_type' => $entity_type->getLabel()]); Chris@0: break; Chris@0: Chris@0: case static::DEFINITION_UPDATED: Chris@0: $summary[$entity_type_id][] = $this->t('The %entity_type entity type needs to be updated.', ['%entity_type' => $entity_type->getLabel()]); Chris@0: break; Chris@0: } Chris@0: } Chris@0: Chris@0: // Process field storage definition changes. Chris@0: if (!empty($change_list['field_storage_definitions'])) { Chris@18: $storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions($entity_type_id); Chris@17: $original_storage_definitions = $this->entityLastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions($entity_type_id); Chris@0: Chris@0: foreach ($change_list['field_storage_definitions'] as $field_name => $change) { Chris@0: switch ($change) { Chris@0: case static::DEFINITION_CREATED: Chris@0: $summary[$entity_type_id][] = $this->t('The %field_name field needs to be installed.', ['%field_name' => $storage_definitions[$field_name]->getLabel()]); Chris@0: break; Chris@0: Chris@0: case static::DEFINITION_UPDATED: Chris@0: $summary[$entity_type_id][] = $this->t('The %field_name field needs to be updated.', ['%field_name' => $storage_definitions[$field_name]->getLabel()]); Chris@0: break; Chris@0: Chris@0: case static::DEFINITION_DELETED: Chris@0: $summary[$entity_type_id][] = $this->t('The %field_name field needs to be uninstalled.', ['%field_name' => $original_storage_definitions[$field_name]->getLabel()]); Chris@0: break; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return $summary; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function applyUpdates() { Chris@18: trigger_error('EntityDefinitionUpdateManagerInterface::applyUpdates() is deprecated in 8.7.0 and will be removed before Drupal 9.0.0. Use \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface::getChangeList() and execute each entity type and field storage update manually instead. See https://www.drupal.org/node/3034742.', E_USER_DEPRECATED); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getEntityType($entity_type_id) { Chris@17: $entity_type = $this->entityLastInstalledSchemaRepository->getLastInstalledDefinition($entity_type_id); Chris@0: return $entity_type ? clone $entity_type : NULL; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@17: public function getEntityTypes() { Chris@17: return $this->entityLastInstalledSchemaRepository->getLastInstalledDefinitions(); Chris@17: } Chris@17: Chris@17: /** Chris@17: * {@inheritdoc} Chris@17: */ Chris@0: public function installEntityType(EntityTypeInterface $entity_type) { Chris@18: $this->clearCachedDefinitions(); Chris@18: $this->entityTypeListener->onEntityTypeCreate($entity_type); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function updateEntityType(EntityTypeInterface $entity_type) { Chris@0: $original = $this->getEntityType($entity_type->id()); Chris@18: $this->clearCachedDefinitions(); Chris@18: $this->entityTypeListener->onEntityTypeUpdate($entity_type, $original); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function uninstallEntityType(EntityTypeInterface $entity_type) { Chris@18: $this->clearCachedDefinitions(); Chris@18: $this->entityTypeListener->onEntityTypeDelete($entity_type); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function updateFieldableEntityType(EntityTypeInterface $entity_type, array $field_storage_definitions, array &$sandbox = NULL) { Chris@18: $original = $this->getEntityType($entity_type->id()); Chris@18: Chris@18: if ($this->requiresEntityDataMigration($entity_type, $original) && $sandbox === NULL) { Chris@18: throw new \InvalidArgumentException('The entity schema update for the ' . $entity_type->id() . ' entity type requires a data migration.'); Chris@18: } Chris@18: Chris@18: $original_field_storage_definitions = $this->entityLastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions($entity_type->id()); Chris@18: $this->entityTypeListener->onFieldableEntityTypeUpdate($entity_type, $original, $field_storage_definitions, $original_field_storage_definitions, $sandbox); Chris@18: $this->clearCachedDefinitions(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function installFieldStorageDefinition($name, $entity_type_id, $provider, FieldStorageDefinitionInterface $storage_definition) { Chris@0: // @todo Pass a mutable field definition interface when we have one. See Chris@0: // https://www.drupal.org/node/2346329. Chris@0: if ($storage_definition instanceof BaseFieldDefinition) { Chris@0: $storage_definition Chris@0: ->setName($name) Chris@0: ->setTargetEntityTypeId($entity_type_id) Chris@0: ->setProvider($provider) Chris@0: ->setTargetBundle(NULL); Chris@0: } Chris@18: $this->clearCachedDefinitions(); Chris@18: $this->fieldStorageDefinitionListener->onFieldStorageDefinitionCreate($storage_definition); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getFieldStorageDefinition($name, $entity_type_id) { Chris@17: $storage_definitions = $this->entityLastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions($entity_type_id); Chris@0: return isset($storage_definitions[$name]) ? clone $storage_definitions[$name] : NULL; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function updateFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition) { Chris@0: $original = $this->getFieldStorageDefinition($storage_definition->getName(), $storage_definition->getTargetEntityTypeId()); Chris@18: $this->clearCachedDefinitions(); Chris@18: $this->fieldStorageDefinitionListener->onFieldStorageDefinitionUpdate($storage_definition, $original); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function uninstallFieldStorageDefinition(FieldStorageDefinitionInterface $storage_definition) { Chris@18: $this->clearCachedDefinitions(); Chris@18: $this->fieldStorageDefinitionListener->onFieldStorageDefinitionDelete($storage_definition); Chris@0: } Chris@0: Chris@0: /** Chris@18: * {@inheritdoc} Chris@0: */ Chris@18: public function getChangeList() { Chris@18: $this->entityTypeManager->useCaches(FALSE); Chris@18: $this->entityFieldManager->useCaches(FALSE); Chris@0: $change_list = []; Chris@0: Chris@18: foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) { Chris@17: $original = $this->entityLastInstalledSchemaRepository->getLastInstalledDefinition($entity_type_id); Chris@0: Chris@0: // @todo Support non-storage-schema-changing definition updates too: Chris@0: // https://www.drupal.org/node/2336895. Chris@0: if (!$original) { Chris@0: $change_list[$entity_type_id]['entity_type'] = static::DEFINITION_CREATED; Chris@0: } Chris@0: else { Chris@0: if ($this->requiresEntityStorageSchemaChanges($entity_type, $original)) { Chris@0: $change_list[$entity_type_id]['entity_type'] = static::DEFINITION_UPDATED; Chris@0: } Chris@0: Chris@18: if ($this->entityTypeManager->getStorage($entity_type_id) instanceof DynamicallyFieldableEntityStorageInterface) { Chris@0: $field_changes = []; Chris@18: $storage_definitions = $this->entityFieldManager->getFieldStorageDefinitions($entity_type_id); Chris@17: $original_storage_definitions = $this->entityLastInstalledSchemaRepository->getLastInstalledFieldStorageDefinitions($entity_type_id); Chris@0: Chris@0: // Detect created field storage definitions. Chris@0: foreach (array_diff_key($storage_definitions, $original_storage_definitions) as $field_name => $storage_definition) { Chris@0: $field_changes[$field_name] = static::DEFINITION_CREATED; Chris@0: } Chris@0: Chris@0: // Detect deleted field storage definitions. Chris@0: foreach (array_diff_key($original_storage_definitions, $storage_definitions) as $field_name => $original_storage_definition) { Chris@0: $field_changes[$field_name] = static::DEFINITION_DELETED; Chris@0: } Chris@0: Chris@0: // Detect updated field storage definitions. Chris@0: foreach (array_intersect_key($storage_definitions, $original_storage_definitions) as $field_name => $storage_definition) { Chris@0: // @todo Support non-storage-schema-changing definition updates too: Chris@0: // https://www.drupal.org/node/2336895. So long as we're checking Chris@0: // based on schema change requirements rather than definition Chris@0: // equality, skip the check if the entity type itself needs to be Chris@0: // updated, since that can affect the schema of all fields, so we Chris@0: // want to process that update first without reporting false Chris@0: // positives here. Chris@0: if (!isset($change_list[$entity_type_id]['entity_type']) && $this->requiresFieldStorageSchemaChanges($storage_definition, $original_storage_definitions[$field_name])) { Chris@0: $field_changes[$field_name] = static::DEFINITION_UPDATED; Chris@0: } Chris@0: } Chris@0: Chris@0: if ($field_changes) { Chris@0: $change_list[$entity_type_id]['field_storage_definitions'] = $field_changes; Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: // @todo Support deleting entity definitions when we support base field Chris@14: // purging. Chris@14: // @see https://www.drupal.org/node/2907779 Chris@0: Chris@18: $this->entityTypeManager->useCaches(TRUE); Chris@18: $this->entityFieldManager->useCaches(TRUE); Chris@0: Chris@0: return array_filter($change_list); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks if the changes to the entity type requires storage schema changes. Chris@0: * Chris@0: * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type Chris@0: * The updated entity type definition. Chris@0: * @param \Drupal\Core\Entity\EntityTypeInterface $original Chris@0: * The original entity type definition. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if storage schema changes are required, FALSE otherwise. Chris@0: */ Chris@0: protected function requiresEntityStorageSchemaChanges(EntityTypeInterface $entity_type, EntityTypeInterface $original) { Chris@18: $storage = $this->entityTypeManager->getStorage($entity_type->id()); Chris@0: return ($storage instanceof EntityStorageSchemaInterface) && $storage->requiresEntityStorageSchemaChanges($entity_type, $original); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Checks if the changes to the storage definition requires schema changes. Chris@0: * Chris@0: * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $storage_definition Chris@0: * The updated field storage definition. Chris@0: * @param \Drupal\Core\Field\FieldStorageDefinitionInterface $original Chris@0: * The original field storage definition. Chris@0: * Chris@0: * @return bool Chris@0: * TRUE if storage schema changes are required, FALSE otherwise. Chris@0: */ Chris@0: protected function requiresFieldStorageSchemaChanges(FieldStorageDefinitionInterface $storage_definition, FieldStorageDefinitionInterface $original) { Chris@18: $storage = $this->entityTypeManager->getStorage($storage_definition->getTargetEntityTypeId()); Chris@0: return ($storage instanceof DynamicallyFieldableEntityStorageSchemaInterface) && $storage->requiresFieldStorageSchemaChanges($storage_definition, $original); Chris@0: } Chris@0: Chris@18: /** Chris@18: * Checks if existing data would be lost if the schema changes were applied. Chris@18: * Chris@18: * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type Chris@18: * The updated entity type definition. Chris@18: * @param \Drupal\Core\Entity\EntityTypeInterface $original Chris@18: * The original entity type definition. Chris@18: * Chris@18: * @return bool Chris@18: * TRUE if data migration is required, FALSE otherwise. Chris@18: */ Chris@18: protected function requiresEntityDataMigration(EntityTypeInterface $entity_type, EntityTypeInterface $original) { Chris@18: $storage = $this->entityTypeManager->getStorage($entity_type->id()); Chris@18: return ($storage instanceof EntityStorageSchemaInterface) && $storage->requiresEntityDataMigration($entity_type, $original); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Clears necessary caches to apply entity/field definition updates. Chris@18: */ Chris@18: protected function clearCachedDefinitions() { Chris@18: $this->entityTypeManager->clearCachedDefinitions(); Chris@18: $this->entityFieldManager->clearCachedFieldDefinitions(); Chris@18: } Chris@18: Chris@0: }