function system_update_8805

Change the provider of the 'path_alias' entity type and its base fields.

File

core/modules/system/system.install, line 2763

Code

function system_update_8805() {
  // If the path alias module is not installed, it means that
  // system_update_8803() ran as part of Drupal 8.8.0-alpha1, in which case we
  // need to enable the module and change the provider of the 'path_alias'
  // entity type.
  if (!\Drupal::moduleHandler()->moduleExists('path_alias')) {
    \Drupal::service('module_installer')->install([
      'path_alias',
    ], FALSE);
    /** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $last_installed_schema_repository */
    $last_installed_schema_repository = \Drupal::service('entity.last_installed_schema.repository');
    $entity_type = $last_installed_schema_repository->getLastInstalledDefinition('path_alias');
    // Set the new class for the entity type.
    $entity_type->setClass(PathAlias::class);
    // Set the new provider for the entity type.
    $reflection = new ReflectionClass($entity_type);
    $property = $reflection->getProperty('provider');
    $property->setAccessible(TRUE);
    $property->setValue($entity_type, 'path_alias');
    $last_installed_schema_repository->setLastInstalledDefinition($entity_type);
    $field_storage_definitions = $last_installed_schema_repository->getLastInstalledFieldStorageDefinitions('path_alias');
    foreach ($field_storage_definitions as $field_storage_definition) {
      if ($field_storage_definition->isBaseField()) {
        $field_storage_definition->setProvider('path_alias');
        $last_installed_schema_repository->setLastInstalledFieldStorageDefinition($field_storage_definition);
      }
    }
  }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.