Verified Commit 7ef17907 authored by godotislate's avatar godotislate
Browse files

feat: #3096170 Add Confirmation Page for theme uninstall

By: mstrelan
By: longwave
By: nicxvan
By: smustgrave
By: penyaskito
By: godotislate
By: sivaji_ganesh_jojodae
By: catch
By: benjifisher
By: rkoller
By: simohell
By: the_g_bomb
By: worldlinemine
(cherry picked from commit 392da0f5)
parent 28426e34
Loading
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -28211,6 +28211,12 @@
	'count' => 1,
	'path' => __DIR__ . '/modules/system/src/Form/ThemeSettingsForm.php',
];
$ignoreErrors[] = [
	'message' => '#^Method Drupal\\\\system\\\\Form\\\\ThemeUninstallConfirmForm\\:\\:addDependencyListsToForm\\(\\) has no return type specified\\.$#',
	'identifier' => 'missingType.return',
	'count' => 1,
	'path' => __DIR__ . '/modules/system/src/Form/ThemeUninstallConfirmForm.php',
];
$ignoreErrors[] = [
	'message' => '#^Variable \\$pdo_message might not be defined\\.$#',
	'identifier' => 'variable.undefined',
+0 −33
Original line number Diff line number Diff line
@@ -58,39 +58,6 @@ public function __construct(ThemeHandlerInterface $theme_handler, ThemeExtension
    $this->themeInstaller = $theme_installer;
  }

  /**
   * Uninstalls a theme.
   *
   * @param string $theme
   *   The theme name.
   *
   * @return \Symfony\Component\HttpFoundation\RedirectResponse
   *   Redirects back to the appearance admin page.
   */
  public function uninstall(#[MapQueryParameter] string $theme) {
    $config = $this->config('system.theme');

    // Get current list of themes.
    $themes = $this->themeHandler->listInfo();

    // Check if the specified theme is one recognized by the system.
    if (!empty($themes[$theme])) {
      // Do not uninstall the default or admin theme.
      if ($theme === $config->get('default') || $theme === $config->get('admin')) {
        $this->messenger()->addError($this->t('%theme is the default theme and cannot be uninstalled.', ['%theme' => $themes[$theme]->info['name']]));
      }
      else {
        $this->themeInstaller->uninstall([$theme]);
        $this->messenger()->addStatus($this->t('The %theme theme has been uninstalled.', ['%theme' => $themes[$theme]->info['name']]));
      }
    }
    else {
      $this->messenger()->addError($this->t('The %theme theme was not found.', ['%theme' => $theme]));
    }

    return $this->redirect('system.themes_page');
  }

  /**
   * Installs a theme.
   *
+150 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\system\Form;

use Drupal\Core\Config\ConfigManagerInterface;
use Drupal\Core\Config\Entity\ConfigDependencyDeleteFormTrait;
use Drupal\Core\DependencyInjection\AutowireTrait;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Extension\ThemeInstallerInterface;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Attribute\MapQueryParameter;

/**
 * Builds a confirmation form to uninstall a theme.
 *
 * @internal
 */
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());
  }

}
+2 −2
Original line number Diff line number Diff line
@@ -285,10 +285,10 @@ system.modules_list_non_stable_confirm:
system.theme_uninstall:
  path: '/admin/appearance/uninstall'
  defaults:
    _controller: '\Drupal\system\Controller\ThemeController::uninstall'
    _form: '\Drupal\system\Form\ThemeUninstallConfirmForm'
    _title: 'Uninstall theme'
  requirements:
    _permission: 'administer themes'
    _csrf_token: 'TRUE'

system.theme_install:
  path: '/admin/appearance/install'
+2 −0
Original line number Diff line number Diff line
@@ -524,8 +524,10 @@ public function testUninstallingThemes(): void {

    // Uninstall each of the two themes starting with Olivero.
    $this->cssSelect('a[title="Uninstall Olivero theme"]')[0]->click();
    $this->submitForm([], 'Uninstall');
    $this->assertSession()->responseContains('The <em class="placeholder">Olivero</em> theme has been uninstalled');
    $this->cssSelect('a[title="Uninstall Claro theme"]')[0]->click();
    $this->submitForm([], 'Uninstall');
    $this->assertSession()->responseContains('The <em class="placeholder">Claro</em> theme has been uninstalled');
  }

Loading