class ThemeUninstallConfirmForm
Same name and namespace in other branches
- 11.x core/modules/system/src/Form/ThemeUninstallConfirmForm.php \Drupal\system\Form\ThemeUninstallConfirmForm
Builds a confirmation form to uninstall a theme.
@internal
Hierarchy
- class \Drupal\Core\Form\FormBase implements \Drupal\Core\Form\FormInterface, \Drupal\Core\DependencyInjection\ContainerInjectionInterface uses \Drupal\Core\DependencyInjection\AutowireTrait, \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Htmx\HtmxRequestInfoTrait, \Drupal\Core\Logger\LoggerChannelTrait, \Drupal\Core\Messenger\MessengerTrait, \Drupal\Core\Routing\RedirectDestinationTrait, \Drupal\Core\StringTranslation\StringTranslationTrait
- class \Drupal\Core\Form\ConfirmFormBase implements \Drupal\Core\Form\ConfirmFormInterface extends \Drupal\Core\Form\FormBase
- class \Drupal\system\Form\ThemeUninstallConfirmForm uses \Drupal\Core\DependencyInjection\AutowireTrait, \Drupal\Core\Config\Entity\ConfigDependencyDeleteFormTrait extends \Drupal\Core\Form\ConfirmFormBase
- class \Drupal\Core\Form\ConfirmFormBase implements \Drupal\Core\Form\ConfirmFormInterface extends \Drupal\Core\Form\FormBase
Expanded class hierarchy of ThemeUninstallConfirmForm
1 string reference to 'ThemeUninstallConfirmForm'
- system.routing.yml in core/
modules/ system/ system.routing.yml - core/modules/system/system.routing.yml
File
-
core/
modules/ system/ src/ Form/ ThemeUninstallConfirmForm.php, line 26
Namespace
Drupal\system\FormView source
class ThemeUninstallConfirmForm extends ConfirmFormBase {
use AutowireTrait;
use ConfigDependencyDeleteFormTrait;
/**
* The theme label.
*/
protected string $themeLabel = '';
public function __construct(protected ThemeHandlerInterface $themeHandler, protected ThemeInstallerInterface $themeInstaller, protected ConfigManagerInterface $configManager, protected EntityTypeManagerInterface $entityTypeManager) {
}
/**
* {@inheritdoc}
*/
public function getQuestion() : TranslatableMarkup {
if ($this->themeLabel) {
return $this->t('Uninstall %theme theme', [
'%theme' => $this->themeLabel,
]);
}
return $this->t('Uninstall theme');
}
/**
* {@inheritdoc}
*/
public function getConfirmText() : TranslatableMarkup {
return $this->t('Uninstall');
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() : Url {
return new Url('system.themes_page');
}
/**
* {@inheritdoc}
*/
public function getDescription() : TranslatableMarkup {
return $this->t('Would you like to continue with uninstalling the above?');
}
/**
* {@inheritdoc}
*/
public function getFormId() : string {
return 'system_theme_uninstall_confirm_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state, #[MapQueryParameter] string $theme = '') : RedirectResponse|array {
if (empty($theme)) {
throw new AccessDeniedHttpException();
}
// Get current list of themes.
$themes = $this->themeHandler
->listInfo();
if (empty($themes[$theme])) {
$this->messenger()
->addError($this->t('The %theme theme was not found.', [
'%theme' => $theme,
]));
return new RedirectResponse($this->getCancelUrl()
->toString());
}
$this->themeLabel = $themes[$theme]->info['name'];
$config = $this->config('system.theme');
if ($theme === $config->get('default')) {
$this->messenger()
->addError($this->t('%theme is the default theme and cannot be uninstalled.', [
'%theme' => $themes[$theme]->info['name'],
]));
return new RedirectResponse($this->getCancelUrl()
->toString());
}
if ($theme === $config->get('admin')) {
$this->messenger()
->addError($this->t('%theme is the admin theme and cannot be uninstalled.', [
'%theme' => $themes[$theme]->info['name'],
]));
return new RedirectResponse($this->getCancelUrl()
->toString());
}
$theme_info = $themes[$theme];
$dependent_themes = [];
if (!empty($theme_info->sub_themes)) {
foreach ($theme_info->sub_themes as $sub_theme => $sub_label) {
if (!empty($themes[$sub_theme]->status)) {
$dependent_themes[] = $sub_label;
}
}
}
if (!empty($dependent_themes)) {
$this->messenger()
->addError($this->t('%theme cannot be uninstalled because the following themes depend on it: %themes', [
'%theme' => $theme_info->info['name'],
'%themes' => implode(', ', $dependent_themes),
]));
return new RedirectResponse($this->getCancelUrl()
->toString());
}
$form['text']['#markup'] = '<p>' . $this->t('The <em>%theme</em> theme will be completely uninstalled from your site, and all data from this theme will be lost!', [
'%theme' => $theme_info->info['name'],
]) . '</p>';
// List the dependent entities.
$this->addDependencyListsToForm($form, 'theme', [
$theme,
], $this->configManager, $this->entityTypeManager);
$form['theme'] = [
'#type' => 'value',
'#value' => $theme,
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) : void {
$themes = $this->themeHandler
->listInfo();
$theme = $form_state->getValue('theme');
$this->themeInstaller
->uninstall([
$form_state->getValue('theme'),
]);
$this->messenger()
->addStatus($this->t('The %theme theme has been uninstalled.', [
'%theme' => $themes[$theme]->info['name'],
]));
$form_state->setRedirectUrl($this->getCancelUrl());
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.