function ThemeInstallController::installTheme

Same name and namespace in other branches
  1. 11.x core/modules/system/tests/modules/nightwatch_theme_install_utility/src/Controller/ThemeInstallController.php \Drupal\nightwatch_theme_install_utility\Controller\ThemeInstallController::installTheme()
  2. 10 core/modules/system/tests/modules/nightwatch_theme_install_utility/src/Controller/ThemeInstallController.php \Drupal\nightwatch_theme_install_utility\Controller\ThemeInstallController::installTheme()

Installs a theme.

Parameters

string $theme: The theme to install.

string $default_or_admin: Which type of theme to install, can be `default` or `admin`.

Return value

array A render array confirming installation.

2 calls to ThemeInstallController::installTheme()
ThemeInstallController::installAdmin in core/modules/system/tests/modules/nightwatch_theme_install_utility/src/Controller/ThemeInstallController.php
Install a theme as the admin theme.
ThemeInstallController::installDefault in core/modules/system/tests/modules/nightwatch_theme_install_utility/src/Controller/ThemeInstallController.php
Install a theme as default.

File

core/modules/system/tests/modules/nightwatch_theme_install_utility/src/Controller/ThemeInstallController.php, line 73

Class

ThemeInstallController
Provides an easier way for Nightwatch tests to install themes.

Namespace

Drupal\nightwatch_theme_install_utility\Controller

Code

private function installTheme($theme, $default_or_admin) : array {
  assert(in_array($default_or_admin, [
    'default',
    'admin',
  ]), 'The $default_or_admin parameter must be `default` or `admin`');
  $config = $this->configFactory
    ->getEditable('system.theme');
  // The ThemeAccess event listener is constructed alongside all access checks
  // prior to this method being called, and is injected into classes which
  // indirectly call this method. This means that the list of themes in the
  // container is not available to the access check when determining the
  // active theme immediately after installing a theme and setting it as the
  // admin theme. This issue only happens when installing a theme and
  // attempting to render via that theme during the same request, so
  // work around it by triggering theme negotiation prior to installing
  // the new theme.
  $route_match = \Drupal::routeMatch();
  \Drupal::service('theme.manager')->getActiveTheme($route_match);
  $this->themeInstaller
    ->install([
    $theme,
  ]);
  $config->set($default_or_admin, $theme)
    ->save();
  return [
    '#type' => 'container',
    '#attributes' => [
      'id' => 'theme-installed',
    ],
    '#markup' => "Installed {$theme} as {$default_or_admin} theme",
  ];
}

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