class ThemeExperimentalConfirmForm in Drupal 10
Same name and namespace in other branches
- 8 core/modules/system/src/Form/ThemeExperimentalConfirmForm.php \Drupal\system\Form\ThemeExperimentalConfirmForm
- 9 core/modules/system/src/Form/ThemeExperimentalConfirmForm.php \Drupal\system\Form\ThemeExperimentalConfirmForm
Builds a confirmation form for enabling experimental themes.
@internal
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\Core\Form\ConfirmFormBase implements ConfirmFormInterface
- class \Drupal\system\Form\ThemeExperimentalConfirmForm
- class \Drupal\Core\Form\ConfirmFormBase implements ConfirmFormInterface
Expanded class hierarchy of ThemeExperimentalConfirmForm
1 file declares its use of ThemeExperimentalConfirmForm
- ThemeController.php in core/
modules/ system/ src/ Controller/ ThemeController.php
File
- core/
modules/ system/ src/ Form/ ThemeExperimentalConfirmForm.php, line 19
Namespace
Drupal\system\FormView source
class ThemeExperimentalConfirmForm extends ConfirmFormBase {
/**
* An extension discovery instance.
*
* @var \Drupal\Core\Extension\ThemeExtensionList
*/
protected $themeList;
/**
* The theme installer service.
*
* @var \Drupal\Core\Extension\ThemeInstallerInterface
*/
protected $themeInstaller;
/**
* Constructs a ThemeExperimentalConfirmForm object.
*
* @param \Drupal\Core\Extension\ThemeExtensionList $theme_list
* The theme extension list.
* @param \Drupal\Core\Extension\ThemeInstallerInterface $theme_installer
* The theme installer.
*/
public function __construct(ThemeExtensionList $theme_list, ThemeInstallerInterface $theme_installer) {
$this->themeList = $theme_list;
$this->themeInstaller = $theme_installer;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('extension.list.theme'), $container
->get('theme_installer'));
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
return $this
->t('Are you sure you wish to install an experimental theme?');
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
return new Url('system.themes_page');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
return $this
->t('Continue');
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return $this
->t('Would you like to continue with the above?');
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'system_themes_experimental_confirm_form';
}
/**
* {@inheritdoc}
*/
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)) {
// Display a list of required themes that have to be installed as well.
$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),
// It is safe to implode this because theme names are not translated
// markup and so will not be double-escaped.
'@required' => implode(', ', array_map($get_label, $dependencies)),
]);
}
// Add the list of experimental themes after any other messages.
$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);
}
/**
* {@inheritdoc}
*/
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) {
// Set the default theme.
$config
->set('default', $theme)
->save();
// The status message depends on whether an admin theme is currently
// in use: an empty string means the admin theme is set to be the
// default theme.
$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());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConfirmFormBase:: |
public | function |
Returns a caption for the link which cancels the action. Overrides ConfirmFormInterface:: |
2 |
ConfirmFormBase:: |
public | function |
Returns the internal name used to refer to the confirmation item. Overrides ConfirmFormInterface:: |
|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 3 |
FormBase:: |
protected | property | The request stack. | |
FormBase:: |
protected | property | The route match. | |
FormBase:: |
protected | function | Retrieves a configuration object. | |
FormBase:: |
protected | function | Gets the config factory for this form. | 3 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
protected | function | Gets the current user. | |
FormBase:: |
protected | function | Gets the request object. | |
FormBase:: |
protected | function | Gets the route match. | |
FormBase:: |
protected | function | Gets the logger for a specific channel. | |
FormBase:: |
protected | function | Returns a redirect response object for the specified route. | |
FormBase:: |
public | function | Resets the configuration factory. | |
FormBase:: |
public | function | Sets the config factory for this form. | |
FormBase:: |
public | function | Sets the request stack object to use. | |
FormBase:: |
public | function |
Form validation handler. Overrides FormInterface:: |
65 |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 18 |
MessengerTrait:: |
public | function | Gets the messenger. | 18 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 3 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 1 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
ThemeExperimentalConfirmForm:: |
protected | property | The theme installer service. | |
ThemeExperimentalConfirmForm:: |
protected | property | An extension discovery instance. | |
ThemeExperimentalConfirmForm:: |
public | function |
Form constructor. Overrides ConfirmFormBase:: |
|
ThemeExperimentalConfirmForm:: |
public static | function |
Instantiates a new instance of this class. Overrides FormBase:: |
|
ThemeExperimentalConfirmForm:: |
public | function |
Returns the route to go to if the user cancels the action. Overrides ConfirmFormInterface:: |
|
ThemeExperimentalConfirmForm:: |
public | function |
Returns a caption for the button that confirms the action. Overrides ConfirmFormBase:: |
|
ThemeExperimentalConfirmForm:: |
public | function |
Returns additional text to display as a description. Overrides ConfirmFormBase:: |
|
ThemeExperimentalConfirmForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
ThemeExperimentalConfirmForm:: |
public | function |
Returns the question to ask the user. Overrides ConfirmFormInterface:: |
|
ThemeExperimentalConfirmForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
ThemeExperimentalConfirmForm:: |
public | function | Constructs a ThemeExperimentalConfirmForm object. |