ThemeInstallController.php

Same filename and directory in other branches
  1. 11.x core/modules/system/tests/modules/nightwatch_theme_install_utility/src/Controller/ThemeInstallController.php
  2. 10 core/modules/system/tests/modules/nightwatch_theme_install_utility/src/Controller/ThemeInstallController.php

Namespace

Drupal\nightwatch_theme_install_utility\Controller

File

core/modules/system/tests/modules/nightwatch_theme_install_utility/src/Controller/ThemeInstallController.php

View source
<?php

declare (strict_types=1);
namespace Drupal\nightwatch_theme_install_utility\Controller;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Extension\ThemeInstallerInterface;

/**
 * Provides an easier way for Nightwatch tests to install themes.
 */
class ThemeInstallController extends ControllerBase {
  
  /**
   * The theme installer service.
   *
   * @var \Drupal\Core\Extension\ThemeInstallerInterface
   */
  protected $themeInstaller;
  
  /**
   * Constructs a new ThemeInstallController.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\Extension\ThemeInstallerInterface $theme_installer
   *   The theme installer.
   */
  public function __construct(ConfigFactoryInterface $config_factory, ThemeInstallerInterface $theme_installer) {
    $this->configFactory = $config_factory;
    $this->themeInstaller = $theme_installer;
  }
  
  /**
   * Install a theme as default.
   *
   * @param string $theme
   *   The theme to install as the default theme.
   *
   * @return array
   *   A render array confirming installation.
   */
  public function installDefault(string $theme) {
    return $this->installTheme($theme, 'default');
  }
  
  /**
   * Install a theme as the admin theme.
   *
   * @param string $theme
   *   The theme to install as the admin theme.
   *
   * @return array
   *   A render array confirming installation.
   */
  public function installAdmin($theme) {
    return $this->installTheme($theme, 'admin');
  }
  
  /**
   * Installs a theme.
   *
   * @param string $theme
   *   The theme to install.
   * @param string $default_or_admin
   *   Which type of theme to install, can be `default` or `admin`.
   *
   * @return array
   *   A render array confirming installation.
   */
  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",
    ];
  }

}

Classes

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

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