function ThemeInstaller::install

Same name and namespace in other branches
  1. 10 core/lib/Drupal/Core/Extension/ThemeInstaller.php \Drupal\Core\Extension\ThemeInstaller::install()
  2. 9 core/lib/Drupal/Core/Extension/ThemeInstaller.php \Drupal\Core\Extension\ThemeInstaller::install()
  3. 8.9.x core/lib/Drupal/Core/Extension/ThemeInstaller.php \Drupal\Core\Extension\ThemeInstaller::install()
  4. main core/lib/Drupal/Core/Extension/ThemeInstaller.php \Drupal\Core\Extension\ThemeInstaller::install()

Installs a given list of themes.

Parameters

array $theme_list: An array of theme names.

bool $install_dependencies: (optional) If TRUE, dependencies will automatically be installed in the correct order. This incurs a significant performance cost, so use FALSE if you know $theme_list is already complete and in the correct order.

Return value

bool Whether any of the given themes have been installed.

Overrides ThemeInstallerInterface::install

File

core/lib/Drupal/Core/Extension/ThemeInstaller.php, line 135

Class

ThemeInstaller
Manages theme installation/uninstallation.

Namespace

Drupal\Core\Extension

Code

public function install(array $theme_list, $install_dependencies = TRUE) {
  $extension_config = $this->configFactory
    ->getEditable('core.extension');
  $theme_data = $this->themeExtensionList
    ->reset()
    ->getList();
  $installed_themes = $extension_config->get('theme') ?: [];
  if ($install_dependencies) {
    // Themes explicitly requested that are not yet installed. Deprecation
    // notices are emitted only for these, not for resolved dependencies.
    $requested_themes = array_diff(array_values($theme_list), array_keys($installed_themes));
    // Resolve the full, dependency-sorted list of themes to install. This
    // also validates that the themes and their dependencies are installable.
    $theme_list = $this->getThemesToInstall($theme_list);
    if (empty($theme_list)) {
      // Nothing to do. All themes already installed.
      return TRUE;
    }
    foreach ($requested_themes as $theme) {
      if (isset($theme_data[$theme]) && $theme_data[$theme]->info[ExtensionLifecycle::LIFECYCLE_IDENTIFIER] === ExtensionLifecycle::DEPRECATED) {
        // phpcs:ignore Drupal.Semantics.FunctionTriggerError
        @trigger_error("The theme '{$theme}' is deprecated. See " . $theme_data[$theme]->info['lifecycle_link'], E_USER_DEPRECATED);
      }
    }
  }
  $themes_installed = [];
  foreach ($theme_list as $key) {
    // Only process themes that are not already installed.
    $installed = $extension_config->get("theme.{$key}") !== NULL;
    if ($installed) {
      continue;
    }
    // Throw an exception if the theme name is too long.
    if (strlen($key) > DRUPAL_EXTENSION_NAME_MAX_LENGTH) {
      throw new ExtensionNameLengthException("Theme name {$key} is over the maximum allowed length of " . DRUPAL_EXTENSION_NAME_MAX_LENGTH . ' characters.');
    }
    // Throw an exception if a module with the same name is enabled.
    $installed_modules = $extension_config->get('module') ?: [];
    if (isset($installed_modules[$key])) {
      throw new ExtensionNameReservedException("Theme name {$key} is already in use by an installed module.");
    }
    // Validate default configuration of the theme. If there is existing
    // configuration then stop installing.
    $this->configInstaller
      ->checkConfigurationToInstall('theme', $key);
    // The value is not used; the weight is ignored for themes currently.
    $extension_config->set("theme.{$key}", 0)
      ->save();
    // Reset theme listing.
    $this->themeHandler
      ->reset();
    // Only install default configuration if this theme has not been installed
    // already.
    if (!isset($installed_themes[$key])) {
      // Install default configuration of the theme.
      $this->configInstaller
        ->installDefaultConfig('theme', $key);
    }
    $themes_installed[] = $key;
    // Record the fact that it was installed.
    $this->logger
      ->info('%theme theme installed.', [
      '%theme' => $key,
    ]);
  }
  // Add new themes to the list of installed themes.
  $register_themes = array_merge(array_keys($installed_themes), $themes_installed);
  // Get list of extensions for the new list of themes.
  $register_themes = array_intersect_key($theme_data, array_flip($register_themes));
  $this->resetSystem($register_themes);
  // Invoke hook_themes_installed() after the themes have been installed.
  $this->moduleHandler
    ->invokeAll('themes_installed', [
    $themes_installed,
  ]);
  return !empty($themes_installed);
}

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