View source
<?php
namespace Drupal\system\Form;
use Drupal\Core\Config\PreExistingConfigException;
use Drupal\Core\Config\UnmetDependenciesException;
use Drupal\Core\Extension\ThemeExtensionList;
use Drupal\Core\Extension\ThemeInstallerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ThemeExperimentalConfirmForm extends ConfirmFormBase {
protected $themeList;
protected $themeInstaller;
public function __construct(ThemeExtensionList $theme_list, ThemeInstallerInterface $theme_installer) {
$this->themeList = $theme_list;
$this->themeInstaller = $theme_installer;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('extension.list.theme'), $container
->get('theme_installer'));
}
public function getQuestion() {
return $this
->t('Are you sure you wish to install an experimental theme?');
}
public function getCancelUrl() {
return new Url('system.themes_page');
}
public function getConfirmText() {
return $this
->t('Continue');
}
public function getDescription() {
return $this
->t('Would you like to continue with the above?');
}
public function getFormId() {
return 'system_themes_experimental_confirm_form';
}
public function buildForm(array $form, FormStateInterface $form_state) {
$theme = $form_state
->getBuildInfo()['args'][0] ? $form_state
->getBuildInfo()['args'][0] : NULL;
$all_themes = $this->themeList
->getList();
if (!isset($all_themes[$theme])) {
return $this
->redirect('system.themes_page');
}
$this
->messenger()
->addWarning($this
->t('Experimental themes are provided for testing purposes only. Use at your own risk.'));
$dependencies = array_keys($all_themes[$theme]->requires);
$themes = array_merge([
$theme,
], $dependencies);
$is_experimental = function ($theme) use ($all_themes) {
return isset($all_themes[$theme]) && $all_themes[$theme]
->isExperimental();
};
$get_label = function ($theme) use ($all_themes) {
return $all_themes[$theme]->info['name'];
};
$items = [];
if (!empty($dependencies)) {
$items[] = $this
->formatPlural(count($dependencies), 'You must enable the @required theme to install @theme.', 'You must enable the @required themes to install @theme.', [
'@theme' => $get_label($theme),
'@required' => implode(', ', array_map($get_label, $dependencies)),
]);
}
$items[] = $this
->t('The following themes are experimental: @themes', [
'@themes' => implode(', ', array_map($get_label, array_filter($themes, $is_experimental))),
]);
$form['message'] = [
'#theme' => 'item_list',
'#items' => $items,
];
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$args = $form_state
->getBuildInfo()['args'];
$theme = $args[0] ?? NULL;
$set_default = $args[1] ?? FALSE;
$themes = $this->themeList
->getList();
$config = $this
->configFactory()
->getEditable('system.theme');
try {
if ($this->themeInstaller
->install([
$theme,
])) {
if ($set_default) {
$config
->set('default', $theme)
->save();
$admin_theme = $config
->get('admin');
if (!empty($admin_theme) && $admin_theme !== $theme) {
$this
->messenger()
->addStatus($this
->t('Please note that the administration theme is still set to the %admin_theme theme; consequently, the theme on this page remains unchanged. All non-administrative sections of the site, however, will show the selected %selected_theme theme by default.', [
'%admin_theme' => $themes[$admin_theme]->info['name'],
'%selected_theme' => $themes[$theme]->info['name'],
]));
}
else {
$this
->messenger()
->addStatus($this
->t('%theme is now the default theme.', [
'%theme' => $themes[$theme]->info['name'],
]));
}
}
else {
$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));
}
$form_state
->setRedirectUrl($this
->getCancelUrl());
}
}