You are here

public function ThemeController::install in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/src/Controller/ThemeController.php \Drupal\system\Controller\ThemeController::install()

Installs a theme.

Parameters

\Symfony\Component\HttpFoundation\Request $request: A request object containing a theme name and a valid token.

Return value

\Symfony\Component\HttpFoundation\RedirectResponse|array Redirects back to the appearance admin page or the confirmation form if an experimental theme will be installed.

Throws

\Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException Throws access denied when no theme or token is set in the request or when the token is invalid.

1 string reference to 'ThemeController::install'
system.routing.yml in core/modules/system/system.routing.yml
core/modules/system/system.routing.yml

File

core/modules/system/src/Controller/ThemeController.php, line 131

Class

ThemeController
Controller for theme handling.

Namespace

Drupal\system\Controller

Code

public function install(Request $request) {
  $theme = $request->query
    ->get('theme');
  if (isset($theme)) {

    // Display confirmation form in case of experimental theme.
    if ($this
      ->willInstallExperimentalTheme($theme)) {
      return $this
        ->formBuilder()
        ->getForm(ThemeExperimentalConfirmForm::class, $theme);
    }
    try {
      if ($this->themeInstaller
        ->install([
        $theme,
      ])) {
        $themes = $this->themeHandler
          ->listInfo();
        $this
          ->messenger()
          ->addStatus($this
          ->t('The %theme theme has been installed.', [
          '%theme' => $themes[$theme]->info['name'],
        ]));
      }
      else {
        $this
          ->messenger()
          ->addError($this
          ->t('The %theme theme was not found.', [
          '%theme' => $theme,
        ]));
      }
    } catch (PreExistingConfigException $e) {
      $config_objects = $e
        ->flattenConfigObjects($e
        ->getConfigObjects());
      $this
        ->messenger()
        ->addError($this
        ->formatPlural(count($config_objects), 'Unable to install @extension, %config_names already exists in active configuration.', 'Unable to install @extension, %config_names already exist in active configuration.', [
        '%config_names' => implode(', ', $config_objects),
        '@extension' => $theme,
      ]));
    } catch (UnmetDependenciesException $e) {
      $this
        ->messenger()
        ->addError($e
        ->getTranslatedMessage($this
        ->getStringTranslation(), $theme));
    } catch (MissingDependencyException $e) {
      $this
        ->messenger()
        ->addError($this
        ->t('Unable to install @theme due to missing module dependencies.', [
        '@theme' => $theme,
      ]));
    }
    return $this
      ->redirect('system.themes_page');
  }
  throw new AccessDeniedHttpException();
}