/** * {@inheritdoc} */ public function install(array $theme_list, $install_dependencies = TRUE) { $extension_config = $this->configFactory->getEditable('core.extension'); $theme_data = $this->rebuildThemeData(); if ($install_dependencies) { $theme_list = array_combine($theme_list, $theme_list); if ($missing = array_diff_key($theme_list, $theme_data)) { // One or more of the given themes doesn't exist. throw new \InvalidArgumentException(SafeMarkup::format('Unknown themes: !themes.', array('!themes' => implode(', ', $missing)))); } // Only process themes that are not installed currently. $installed_themes = $extension_config->get('theme') ?: array(); if (!($theme_list = array_diff_key($theme_list, $installed_themes))) { // Nothing to do. All themes already installed. return TRUE; } while (list($theme) = each($theme_list)) { // Add dependencies to the list. The new themes will be processed as // the while loop continues. foreach (array_keys($theme_data[$theme]->requires) as $dependency) { if (!isset($theme_data[$dependency])) { // The dependency does not exist. return FALSE; } // Skip already installed themes. if (!isset($theme_list[$dependency]) && !isset($installed_themes[$dependency])) { $theme_list[$dependency] = $dependency; } } } // Set the actual theme weights. $theme_list = array_map(function ($theme) use($theme_data) { return $theme_data[$theme]->sort; }, $theme_list); // Sort the theme list by their weights (reverse). arsort($theme_list); $theme_list = array_keys($theme_list); } else { $installed_themes = $extension_config->get('theme') ?: array(); } $themes_installed = array(); 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(SafeMarkup::format('Theme name %name is over the maximum allowed length of @max characters.', array('%name' => $key, '@max' => DRUPAL_EXTENSION_NAME_MAX_LENGTH))); } // 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(); // Add the theme to the current list. // @todo Remove all code that relies on $status property. $theme_data[$key]->status = 1; $this->addTheme($theme_data[$key]); // Update the current theme data accordingly. $current_theme_data = $this->state->get('system.theme.data', array()); $current_theme_data[$key] = $theme_data[$key]; $this->state->set('system.theme.data', $current_theme_data); // Reset theme settings. $theme_settings =& drupal_static('theme_get_setting'); unset($theme_settings[$key]); // @todo Remove system_list(). $this->systemListReset(); // 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.', array('%theme' => $key)); } $this->cssCollectionOptimizer->deleteAll(); $this->resetSystem(); // Invoke hook_themes_installed() after the themes have been installed. $this->moduleHandler->invokeAll('themes_installed', array($themes_installed)); return !empty($themes_installed); }
/** * {@inheritdoc} */ public function enable(array $theme_list, $enable_dependencies = TRUE) { $extension_config = $this->configFactory->get('core.extension'); $theme_data = $this->rebuildThemeData(); if ($enable_dependencies) { $theme_list = array_combine($theme_list, $theme_list); if ($missing = array_diff_key($theme_list, $theme_data)) { // One or more of the given themes doesn't exist. throw new \InvalidArgumentException(String::format('Unknown themes: !themes.', array('!themes' => implode(', ', $missing)))); } // Only process themes that are not enabled currently. $installed_themes = $extension_config->get('theme') ?: array(); if (!($theme_list = array_diff_key($theme_list, $installed_themes))) { // Nothing to do. All themes already enabled. return TRUE; } $installed_themes += $extension_config->get('disabled.theme') ?: array(); while (list($theme) = each($theme_list)) { // Add dependencies to the list. The new themes will be processed as // the while loop continues. foreach (array_keys($theme_data[$theme]->requires) as $dependency) { if (!isset($theme_data[$dependency])) { // The dependency does not exist. return FALSE; } // Skip already installed themes. if (!isset($theme_list[$dependency]) && !isset($installed_themes[$dependency])) { $theme_list[$dependency] = $dependency; } } } // Set the actual theme weights. $theme_list = array_map(function ($theme) use($theme_data) { return $theme_data[$theme]->sort; }, $theme_list); // Sort the theme list by their weights (reverse). arsort($theme_list); $theme_list = array_keys($theme_list); } else { $installed_themes = $extension_config->get('theme') ?: array(); $installed_themes += $extension_config->get('disabled.theme') ?: array(); } $themes_enabled = array(); foreach ($theme_list as $key) { // Only process themes that are not already enabled. $enabled = $extension_config->get("theme.{$key}") !== NULL; if ($enabled) { continue; } // Throw an exception if the theme name is too long. if (strlen($key) > DRUPAL_EXTENSION_NAME_MAX_LENGTH) { throw new ExtensionNameLengthException(String::format('Theme name %name is over the maximum allowed length of @max characters.', array('%name' => $key, '@max' => DRUPAL_EXTENSION_NAME_MAX_LENGTH))); } // The value is not used; the weight is ignored for themes currently. $extension_config->set("theme.{$key}", 0)->clear("disabled.theme.{$key}")->save(); // Add the theme to the current list. // @todo Remove all code that relies on $status property. $theme_data[$key]->status = 1; $this->addTheme($theme_data[$key]); // Update the current theme data accordingly. $current_theme_data = $this->state->get('system.theme.data', array()); $current_theme_data[$key] = $theme_data[$key]; $this->state->set('system.theme.data', $current_theme_data); // Reset theme settings. $theme_settings =& drupal_static('theme_get_setting'); unset($theme_settings[$key]); // @todo Remove system_list(). $this->systemListReset(); // Only install default configuration if this theme has not been installed // already. if (!isset($installed_themes[$key])) { // The default config installation storage only knows about the currently // enabled list of themes, so it has to be reset in order to pick up the // default config of the newly installed theme. However, do not reset the // source storage when synchronizing configuration, since that would // needlessly trigger a reload of the whole configuration to be imported. if (!$this->configInstaller->isSyncing()) { $this->configInstaller->resetSourceStorage(); } // Install default configuration of the theme. $this->configInstaller->installDefaultConfig('theme', $key); } $themes_enabled[] = $key; // Record the fact that it was enabled. watchdog('system', '%theme theme enabled.', array('%theme' => $key), WATCHDOG_INFO); } $this->clearCssCache(); $this->resetSystem(); // Invoke hook_themes_enabled() after the themes have been enabled. $this->moduleHandler->invokeAll('themes_enabled', array($themes_enabled)); return !empty($themes_enabled); }