Namespace
Drupal\nightwatch_theme_install_utility\Controller
File
core/modules/system/tests/modules/nightwatch_theme_install_utility/src/Controller/ThemeInstallController.php
View source
<?php
namespace Drupal\nightwatch_theme_install_utility\Controller;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Extension\ThemeInstallerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ThemeInstallController extends ControllerBase {
protected $themeInstaller;
public function __construct(ConfigFactoryInterface $config_factory, ThemeInstallerInterface $theme_installer) {
$this->configFactory = $config_factory;
$this->themeInstaller = $theme_installer;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('theme_installer'));
}
public function installDefault(string $theme) {
return $this
->installTheme($theme, 'default');
}
public function installAdmin($theme) {
return $this
->installTheme($theme, 'admin');
}
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');
$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