You are here

public function InstallProfile::switch in Helper 8

Switches the site's install profile.

Note the following:

  • This does not run the new profile's install hooks.
  • Does not enable any of the profile's dependencies.
  • This does not check that currently enabled modules are located in the current profile's code and will no longer be available once switching to the new profile.
  • Does not run any uninstallation of the current profile, like removing any tables defined in hook_schema().

@author https://www.drupal.org/project/profile_switcher

Parameters

string $profile: The machine name of the profile to switch to.

int $schema_version: The schema version to set for the install profile.

File

src/InstallProfile.php, line 96

Class

InstallProfile
Helpers related to working with install profiles.

Namespace

Drupal\helper

Code

public function switch($profile, $schema_version = NULL) {
  $current_profile = \Drupal::installProfile();

  // Forces ExtensionDiscovery to rerun for profiles.
  $this->state
    ->delete('system.profile.files');

  // Set the profile in configuration.
  $extension_config = $this->configFactory
    ->getEditable('core.extension');
  $extension_config
    ->set('profile', $profile);
  $extension_config
    ->save();
  drupal_flush_all_caches();

  // Install profiles are also registered as enabled modules.
  // Remove the old profile and add in the new one.
  $extension_config
    ->clear("module.{$current_profile}");

  // The install profile is always given a weight of 1000 by the core
  // extension system.
  $extension_config
    ->set("module.{$profile}", 1000);
  $extension_config
    ->save();

  // Clear caches again.
  drupal_flush_all_caches();

  // Find the correct schema version of the install profile to set as the
  // current schema version. Assume use the latest schema version if a value
  // has not been provided.
  $schema_version = $schema_version ?: $this
    ->getLatestSchemaVersion($profile);

  // Remove the schema value for the old install profile, and set the schema
  // for the new one.
  $this->schemaKeyValue
    ->delete($current_profile);
  $this->schemaKeyValue
    ->set($profile, $schema_version);

  // Clear caches again.
  drupal_flush_all_caches();
}