function ThemeUninstallConfirmForm::buildForm

Same name and namespace in other branches
  1. main core/modules/system/src/Form/ThemeUninstallConfirmForm.php \Drupal\system\Form\ThemeUninstallConfirmForm::buildForm()

Overrides ConfirmFormBase::buildForm

File

core/modules/system/src/Form/ThemeUninstallConfirmForm.php, line 84

Class

ThemeUninstallConfirmForm
Builds a confirmation form to uninstall a theme.

Namespace

Drupal\system\Form

Code

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);
}

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