ModulesUninstallForm.php 6.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

/**
 * @file
 * Contains \Drupal\system\Form\ModulesUninstallForm.
 */

namespace Drupal\system\Form;

use Drupal\Core\Extension\ModuleHandlerInterface;
11
use Drupal\Core\Extension\ModuleInstallerInterface;
12
use Drupal\Core\Form\FormBase;
13
use Drupal\Core\Form\FormStateInterface;
14 15
use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
16 17 18 19

/**
 * Provides a form for uninstalling modules.
 */
20
class ModulesUninstallForm extends FormBase {
21 22 23 24 25 26 27 28

  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

29 30 31 32 33 34 35
  /**
   * The module installer service.
   *
   * @var \Drupal\Core\Extension\ModuleInstallerInterface
   */
  protected $moduleInstaller;

36 37 38 39 40 41 42 43 44 45 46 47 48
  /**
   * The expirable key value store.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
   */
  protected $keyValueExpirable;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('module_handler'),
49
      $container->get('module_installer'),
50
      $container->get('keyvalue.expirable')->get('modules_uninstall')
51 52 53 54 55 56 57 58
    );
  }

  /**
   * Constructs a ModulesUninstallForm object.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
59 60
   * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
   *   The module installer.
61 62 63
   * @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $key_value_expirable
   *   The key value expirable factory.
   */
64
  public function __construct(ModuleHandlerInterface $module_handler, ModuleInstallerInterface $module_installer, KeyValueStoreExpirableInterface $key_value_expirable) {
65
    $this->moduleHandler = $module_handler;
66
    $this->moduleInstaller = $module_installer;
67 68 69 70 71 72
    $this->keyValueExpirable = $key_value_expirable;
  }

  /**
   * {@inheritdoc}
   */
73
  public function getFormId() {
74 75 76 77 78 79
    return 'system_modules_uninstall';
  }

  /**
   * {@inheritdoc}
   */
80
  public function buildForm(array $form, FormStateInterface $form_state) {
81 82 83
    // Make sure the install API is available.
    include_once DRUPAL_ROOT . '/core/includes/install.inc';

84
    // Get a list of all available modules.
85
    $modules = system_rebuild_module_data();
86
    $uninstallable = array_filter($modules, function ($module) use ($modules) {
87
      return empty($modules[$module->getName()]->info['required']) && drupal_get_installed_schema_version($module->getName()) > SCHEMA_UNINSTALLED;
88 89
    });

90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
    // Include system.admin.inc so we can use the sort callbacks.
    $this->moduleHandler->loadInclude('system', 'inc', 'system.admin');

    $form['filters'] = array(
      '#type' => 'container',
      '#attributes' => array(
        'class' => array('table-filter', 'js-show'),
      ),
    );

    $form['filters']['text'] = array(
      '#type' => 'search',
      '#title' => $this->t('Search'),
      '#size' => 30,
      '#placeholder' => $this->t('Enter module name'),
      '#attributes' => array(
        'class' => array('table-filter-text'),
        'data-table' => '#system-modules-uninstall',
        'autocomplete' => 'off',
        'title' => $this->t('Enter a part of the module name or description to filter by.'),
      ),
    );

113 114 115 116
    $form['modules'] = array();

    // Only build the rest of the form if there are any modules available to
    // uninstall;
117
    if (empty($uninstallable)) {
118 119 120 121 122 123
      return $form;
    }

    $profile = drupal_get_profile();

    // Sort all modules by their name.
124
    uasort($uninstallable, 'system_sort_modules_by_info_name');
125
    $validation_reasons = $this->moduleInstaller->validateUninstall(array_keys($uninstallable));
126 127

    $form['uninstall'] = array('#tree' => TRUE);
128
    foreach ($uninstallable as $module_key => $module) {
129 130 131 132
      $name = $module->info['name'] ?: $module->getName();
      $form['modules'][$module->getName()]['#module_name'] = $name;
      $form['modules'][$module->getName()]['name']['#markup'] = $name;
      $form['modules'][$module->getName()]['description']['#markup'] = $this->t($module->info['description']);
133

134
      $form['uninstall'][$module->getName()] = array(
135
        '#type' => 'checkbox',
136
        '#title' => $this->t('Uninstall @module module', array('@module' => $name)),
137 138 139
        '#title_display' => 'invisible',
      );

140 141 142 143 144 145
      // If a validator returns reasons not to uninstall a module,
      // list the reasons and disable the check box.
      if (isset($validation_reasons[$module_key])) {
        $form['modules'][$module->getName()]['#validation_reasons'] = $validation_reasons[$module_key];
        $form['uninstall'][$module->getName()]['#disabled'] = TRUE;
      }
146 147 148 149 150 151
      // All modules which depend on this one must be uninstalled first, before
      // we can allow this module to be uninstalled. (The installation profile
      // is excluded from this list.)
      foreach (array_keys($module->required_by) as $dependent) {
        if ($dependent != $profile && drupal_get_installed_schema_version($dependent) != SCHEMA_UNINSTALLED) {
          $name = isset($modules[$dependent]->info['name']) ? $modules[$dependent]->info['name'] : $dependent;
152 153
          $form['modules'][$module->getName()]['#required_by'][] = $name;
          $form['uninstall'][$module->getName()]['#disabled'] = TRUE;
154 155 156 157
        }
      }
    }

158
    $form['#attached']['library'][] = 'system/drupal.system.modules';
159 160 161
    $form['actions'] = array('#type' => 'actions');
    $form['actions']['submit'] = array(
      '#type' => 'submit',
162
      '#value' => $this->t('Uninstall'),
163 164 165 166 167 168 169 170
    );

    return $form;
  }

  /**
   * {@inheritdoc}
   */
171
  public function validateForm(array &$form, FormStateInterface $form_state) {
172
    // Form submitted, but no modules selected.
173
    if (!array_filter($form_state->getValue('uninstall'))) {
174
      drupal_set_message($this->t('No modules selected.'), 'error');
175
      $form_state->setRedirect('system.modules_uninstall');
176 177 178 179 180 181
    }
  }

  /**
   * {@inheritdoc}
   */
182
  public function submitForm(array &$form, FormStateInterface $form_state) {
183
    // Save all the values in an expirable key value store.
184
    $modules = $form_state->getValue('uninstall');
185
    $uninstall = array_keys(array_filter($modules));
186
    $account = $this->currentUser()->id();
187 188 189
    $this->keyValueExpirable->setWithExpire($account, $uninstall, 60);

    // Redirect to the confirm form.
190
    $form_state->setRedirect('system.modules_uninstall_confirm');
191 192
  }
}