ModulesListForm.php 18.3 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\system\Form;

5
use Drupal\Component\Utility\Unicode;
6
use Drupal\Core\Config\PreExistingConfigException;
7
use Drupal\Core\Config\UnmetDependenciesException;
8
use Drupal\Core\Access\AccessManagerInterface;
9
use Drupal\Core\Extension\Extension;
10
use Drupal\Core\Extension\ModuleHandlerInterface;
11
use Drupal\Core\Extension\ModuleInstallerInterface;
12
use Drupal\Core\Form\FormBase;
13
use Drupal\Core\Form\FormStateInterface;
14
use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
15
use Drupal\Core\Render\Element;
16
use Drupal\Core\Session\AccountInterface;
17
use Drupal\user\PermissionHandlerInterface;
18
use Drupal\Core\Url;
19 20 21
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
22
 * Provides module installation interface.
23 24 25
 *
 * The list of modules gets populated by module.info.yml files, which contain
 * each module's name, description, and information about which modules it
26
 * requires. See \Drupal\Core\Extension\InfoParser for info on module.info.yml
27 28
 * descriptors.
 */
29
class ModulesListForm extends FormBase {
30

31 32 33 34 35 36 37
  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

38 39 40 41 42 43 44 45 46 47 48 49 50 51
  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * The expirable key value store.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
   */
  protected $keyValueExpirable;

52 53 54 55 56 57 58
  /**
   * The module installer.
   *
   * @var \Drupal\Core\Extension\ModuleInstallerInterface
   */
  protected $moduleInstaller;

59 60 61 62 63 64 65
  /**
   * The permission handler.
   *
   * @var \Drupal\user\PermissionHandlerInterface
   */
  protected $permissionHandler;

66 67 68 69 70 71
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('module_handler'),
72
      $container->get('module_installer'),
73
      $container->get('keyvalue.expirable')->get('module_list'),
74
      $container->get('access_manager'),
75
      $container->get('current_user'),
76
      $container->get('user.permissions')
77 78 79 80 81 82 83 84
    );
  }

  /**
   * Constructs a ModulesListForm object.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
85 86
   * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
   *   The module installer.
87 88
   * @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $key_value_expirable
   *   The key value expirable factory.
89
   * @param \Drupal\Core\Access\AccessManagerInterface $access_manager
90
   *   Access manager.
91 92
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
93 94
   * @param \Drupal\user\PermissionHandlerInterface $permission_handler
   *   The permission handler.
95
   */
96
  public function __construct(ModuleHandlerInterface $module_handler, ModuleInstallerInterface $module_installer, KeyValueStoreExpirableInterface $key_value_expirable, AccessManagerInterface $access_manager, AccountInterface $current_user, PermissionHandlerInterface $permission_handler) {
97
    $this->moduleHandler = $module_handler;
98
    $this->moduleInstaller = $module_installer;
99
    $this->keyValueExpirable = $key_value_expirable;
100
    $this->accessManager = $access_manager;
101
    $this->currentUser = $current_user;
102
    $this->permissionHandler = $permission_handler;
103 104 105 106 107
  }

  /**
   * {@inheritdoc}
   */
108
  public function getFormId() {
109 110 111 112 113 114
    return 'system_modules';
  }

  /**
   * {@inheritdoc}
   */
115
  public function buildForm(array $form, FormStateInterface $form_state) {
116
    require_once DRUPAL_ROOT . '/core/includes/install.inc';
117
    $distribution = drupal_install_profile_distribution_name();
118 119 120 121 122 123 124 125 126 127 128 129 130

    // 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',
131 132
      '#title' => $this->t('Filter modules'),
      '#title_display' => 'invisible',
133
      '#size' => 30,
134 135
      '#placeholder' => $this->t('Filter by name or description'),
      '#description' => $this->t('Enter a part of the module name or description'),
136 137 138 139 140 141 142 143 144 145 146 147
      '#attributes' => array(
        'class' => array('table-filter-text'),
        'data-table' => '#system-modules',
        'autocomplete' => 'off',
      ),
    );

    // Sort all modules by their names.
    $modules = system_rebuild_module_data();
    uasort($modules, 'system_sort_modules_by_info_name');

    // Iterate over each of the modules.
148
    $form['modules']['#tree'] = TRUE;
149 150 151 152
    foreach ($modules as $filename => $module) {
      if (empty($module->info['hidden'])) {
        $package = $module->info['package'];
        $form['modules'][$package][$filename] = $this->buildRow($modules, $module, $distribution);
153
        $form['modules'][$package][$filename]['#parents'] = ['modules', $filename];
154 155 156 157
      }
    }

    // Add a wrapper around every package.
158
    foreach (Element::children($form['modules']) as $package) {
159 160
      $form['modules'][$package] += array(
        '#type' => 'details',
161
        '#title' => $this->t($package),
162
        '#open' => TRUE,
163
        '#theme' => 'system_modules_details',
164
        '#attributes' => array('class' => array('package-listing')),
165 166 167 168 169
        // Ensure that the "Core" package comes first.
        '#weight' => $package == 'Core' ? -10 : NULL,
      );
    }

170 171 172 173 174 175
    // If testing modules are shown, collapse the corresponding package by
    // default.
    if (isset($form['modules']['Testing'])) {
      $form['modules']['Testing']['#open'] = FALSE;
    }

176
    // Lastly, sort all packages by title.
177
    uasort($form['modules'], array('\Drupal\Component\Utility\SortArray', 'sortByTitleProperty'));
178

179
    $form['#attached']['library'][] = 'system/drupal.system.modules';
180 181 182
    $form['actions'] = array('#type' => 'actions');
    $form['actions']['submit'] = array(
      '#type' => 'submit',
183
      '#value' => $this->t('Install'),
184
      '#button_type' => 'primary',
185 186 187 188 189 190 191 192 193 194
    );

    return $form;
  }

  /**
   * Builds a table row for the system modules page.
   *
   * @param array $modules
   *   The list existing modules.
195
   * @param \Drupal\Core\Extension\Extension $module
196 197 198 199 200 201
   *   The module for which to build the form row.
   * @param $distribution
   *
   * @return array
   *   The form row for the given module.
   */
202
  protected function buildRow(array $modules, Extension $module, $distribution) {
203 204 205 206 207 208
    // Set the basic properties.
    $row['#required'] = array();
    $row['#requires'] = array();
    $row['#required_by'] = array();

    $row['name']['#markup'] = $module->info['name'];
209
    $row['description']['#markup'] = $this->t($module->info['description']);
210 211
    $row['version']['#markup'] = $module->info['version'];

212 213 214
    // Generate link for module's help page. Assume that if a hook_help()
    // implementation exists then the module provides an overview page, rather
    // than checking to see if the page exists, which is costly.
215
    if ($this->moduleHandler->moduleExists('help') && $module->status && in_array($module->getName(), $this->moduleHandler->getImplementations('help'))) {
216 217 218 219
      $row['links']['help'] = array(
        '#type' => 'link',
        '#title' => $this->t('Help'),
        '#url' => Url::fromRoute('help.page', ['name' => $module->getName()]),
220
        '#options' => array('attributes' => array('class' => array('module-link', 'module-link-help'), 'title' => $this->t('Help'))),
221
      );
222 223 224
    }

    // Generate link for module's permission, if the user has access to it.
225
    if ($module->status && $this->currentUser->hasPermission('administer permissions') && $this->permissionHandler->moduleProvidesPermissions($module->getName())) {
226 227
      $row['links']['permissions'] = array(
        '#type' => 'link',
228
        '#title' => $this->t('Permissions'),
229
        '#url' => Url::fromRoute('user.admin_permissions'),
230
        '#options' => array('fragment' => 'module-' . $module->getName(), 'attributes' => array('class' => array('module-link', 'module-link-permissions'), 'title' => $this->t('Configure permissions'))),
231 232 233 234 235
      );
    }

    // Generate link for module's configuration page, if it has one.
    if ($module->status && isset($module->info['configure'])) {
236 237
      $route_parameters = isset($module->info['configure_parameters']) ? $module->info['configure_parameters'] : array();
      if ($this->accessManager->checkNamedRoute($module->info['configure'], $route_parameters, $this->currentUser)) {
238 239
        $row['links']['configure'] = array(
          '#type' => 'link',
240
          '#title' => $this->t('Configure <span class="visually-hidden">the @module module</span>', ['@module' => $module->info['name']]),
241
          '#url' => Url::fromRoute($module->info['configure'], $route_parameters),
242 243 244 245 246
          '#options' => array(
            'attributes' => array(
              'class' => array('module-link', 'module-link-configure'),
            ),
          ),
247 248 249 250 251 252 253
        );
      }
    }

    // Present a checkbox for installing and indicating the status of a module.
    $row['enable'] = array(
      '#type' => 'checkbox',
254
      '#title' => $this->t('Install'),
255
      '#default_value' => (bool) $module->status,
256
      '#disabled' => (bool) $module->status,
257 258 259 260 261 262
    );

    // Disable the checkbox for required modules.
    if (!empty($module->info['required'])) {
      // Used when displaying modules that are required by the installation profile
      $row['enable']['#disabled'] = TRUE;
263
      $row['#required_by'][] = $distribution . (!empty($module->info['explanation']) ? ' (' . $module->info['explanation'] . ')' : '');
264 265 266 267
    }

    // Check the compatibilities.
    $compatible = TRUE;
268 269 270 271

    // Initialize an empty array of reasons why the module is incompatible. Add
    // each reason as a separate element of the array.
    $reasons = array();
272 273

    // Check the core compatibility.
274
    if ($module->info['core'] != \Drupal::CORE_COMPATIBILITY) {
275
      $compatible = FALSE;
276 277
      $reasons[] = $this->t('This version is not compatible with Drupal @core_version and should be replaced.', array(
        '@core_version' => \Drupal::CORE_COMPATIBILITY,
278 279 280 281 282 283 284
      ));
    }

    // Ensure this module is compatible with the currently installed version of PHP.
    if (version_compare(phpversion(), $module->info['php']) < 0) {
      $compatible = FALSE;
      $required = $module->info['php'] . (substr_count($module->info['php'], '.') < 2 ? '.*' : '');
285
      $reasons[] = $this->t('This module requires PHP version @php_required and is incompatible with PHP version @php_version.', array(
286
        '@php_required' => $required,
287
        '@php_version' => phpversion(),
288 289 290 291 292
      ));
    }

    // If this module is not compatible, disable the checkbox.
    if (!$compatible) {
293
      $status = implode(' ', $reasons);
294
      $row['enable']['#disabled'] = TRUE;
295 296
      $row['description']['#markup'] = $status;
      $row['#attributes']['class'][] = 'incompatible';
297 298 299 300 301
    }

    // If this module requires other modules, add them to the array.
    foreach ($module->requires as $dependency => $version) {
      if (!isset($modules[$dependency])) {
302
        $row['#requires'][$dependency] = $this->t('@module (<span class="admin-missing">missing</span>)', array('@module' => Unicode::ucfirst($dependency)));
303 304 305 306 307 308 309
        $row['enable']['#disabled'] = TRUE;
      }
      // Only display visible modules.
      elseif (empty($modules[$dependency]->hidden)) {
        $name = $modules[$dependency]->info['name'];
        // Disable the module's checkbox if it is incompatible with the
        // dependency's version.
310
        if ($incompatible_version = drupal_check_incompatibility($version, str_replace(\Drupal::CORE_COMPATIBILITY . '-', '', $modules[$dependency]->info['version']))) {
311
          $row['#requires'][$dependency] = $this->t('@module (<span class="admin-missing">incompatible with</span> version @version)', array(
312 313 314 315 316 317 318
            '@module' => $name . $incompatible_version,
            '@version' => $modules[$dependency]->info['version'],
          ));
          $row['enable']['#disabled'] = TRUE;
        }
        // Disable the checkbox if the dependency is incompatible with this
        // version of Drupal core.
319
        elseif ($modules[$dependency]->info['core'] != \Drupal::CORE_COMPATIBILITY) {
320
          $row['#requires'][$dependency] = $this->t('@module (<span class="admin-missing">incompatible with</span> this version of Drupal core)', array(
321 322 323 324 325
            '@module' => $name,
          ));
          $row['enable']['#disabled'] = TRUE;
        }
        elseif ($modules[$dependency]->status) {
326
          $row['#requires'][$dependency] = $this->t('@module', array('@module' => $name));
327 328
        }
        else {
329
          $row['#requires'][$dependency] = $this->t('@module (<span class="admin-disabled">disabled</span>)', array('@module' => $name));
330 331 332 333 334 335 336 337 338
        }
      }
    }

    // If this module is required by other modules, list those, and then make it
    // impossible to disable this one.
    foreach ($module->required_by as $dependent => $version) {
      if (isset($modules[$dependent]) && empty($modules[$dependent]->info['hidden'])) {
        if ($modules[$dependent]->status == 1 && $module->status == 1) {
339
          $row['#required_by'][$dependent] = $this->t('@module', array('@module' => $modules[$dependent]->info['name']));
340 341 342
          $row['enable']['#disabled'] = TRUE;
        }
        else {
343
          $row['#required_by'][$dependent] = $this->t('@module (<span class="admin-disabled">disabled</span>)', array('@module' => $modules[$dependent]->info['name']));
344 345 346 347 348 349 350 351
        }
      }
    }

    return $row;
  }

  /**
352
   * Helper function for building a list of modules to install.
353
   *
354 355
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
356 357
   *
   * @return array
358
   *   An array of modules to install and their dependencies.
359
   */
360
  protected function buildModuleList(FormStateInterface $form_state) {
361
    // Build a list of modules to install.
362
    $modules = array(
363
      'install' => array(),
364
      'dependencies' => array(),
365
      'experimental' => [],
366 367 368 369
    );

    $data = system_rebuild_module_data();
    foreach ($data as $name => $module) {
370 371 372 373 374 375
      // If the module is installed there is nothing to do.
      if ($this->moduleHandler->moduleExists($name)) {
        continue;
      }
      // Required modules have to be installed.
      if (!empty($module->required)) {
376
        $modules['install'][$name] = $module->info['name'];
377
      }
378 379 380 381 382 383
      // Selected modules should be installed.
      elseif (($checkbox = $form_state->getValue(['modules', $name], FALSE)) && $checkbox['enable']) {
        $modules['install'][$name] = $data[$name]->info['name'];
        // Identify experimental modules.
        if ($data[$name]->info['package'] == 'Core (Experimental)') {
          $modules['experimental'][$name] = $data[$name]->info['name'];
384 385 386 387 388
        }
      }
    }

    // Add all dependencies to a list.
389
    while (list($module) = each($modules['install'])) {
390
      foreach (array_keys($data[$module]->requires) as $dependency) {
391
        if (!isset($modules['install'][$dependency]) && !$this->moduleHandler->moduleExists($dependency)) {
392
          $modules['dependencies'][$module][$dependency] = $data[$dependency]->info['name'];
393
          $modules['install'][$dependency] = $data[$dependency]->info['name'];
394 395 396 397 398

          // Identify experimental modules.
          if ($data[$dependency]->info['package'] == 'Core (Experimental)') {
            $modules['experimental'][$dependency] = $data[$dependency]->info['name'];
          }
399 400 401 402 403 404 405 406 407
        }
      }
    }

    // Make sure the install API is available.
    include_once DRUPAL_ROOT . '/core/includes/install.inc';

    // Invoke hook_requirements('install'). If failures are detected, make
    // sure the dependent modules aren't installed either.
408 409 410
    foreach (array_keys($modules['install']) as $module) {
      if (!drupal_check_module($module)) {
        unset($modules['install'][$module]);
411
        unset($modules['experimental'][$module]);
412
        foreach (array_keys($data[$module]->required_by) as $dependent) {
413
          unset($modules['install'][$dependent]);
414 415 416 417 418 419 420 421 422 423 424
          unset($modules['dependencies'][$dependent]);
        }
      }
    }

    return $modules;
  }

  /**
   * {@inheritdoc}
   */
425
  public function submitForm(array &$form, FormStateInterface $form_state) {
426
    // Retrieve a list of modules to install and their dependencies.
427 428
    $modules = $this->buildModuleList($form_state);

429 430 431 432
    // Redirect to a confirmation form if needed.
    if (!empty($modules['experimental']) || !empty($modules['dependencies'])) {

      $route_name = !empty($modules['experimental']) ? 'system.modules_list_experimental_confirm' : 'system.modules_list_confirm';
433
      // Write the list of changed module states into a key value store.
434
      $account = $this->currentUser()->id();
435 436 437
      $this->keyValueExpirable->setWithExpire($account, $modules, 60);

      // Redirect to the confirmation form.
438
      $form_state->setRedirect($route_name);
439 440 441 442 443 444

      // We can exit here because at least one modules has dependencies
      // which we have to prompt the user for in a confirmation form.
      return;
    }

445
    // Install the given modules.
446
    if (!empty($modules['install'])) {
447 448
      try {
        $this->moduleInstaller->install(array_keys($modules['install']));
449 450 451 452 453
        $module_names = array_values($modules['install']);
        drupal_set_message($this->formatPlural(count($module_names), 'Module %name has been enabled.', '@count modules have been enabled: %names.', array(
          '%name' => $module_names[0],
          '%names' => implode(', ', $module_names),
        )));
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
      }
      catch (PreExistingConfigException $e) {
        $config_objects = $e->flattenConfigObjects($e->getConfigObjects());
        drupal_set_message(
          $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.',
            array(
              '%config_names' => implode(', ', $config_objects),
              '@extension' => $modules['install'][$e->getExtension()]
            )),
          'error'
        );
        return;
      }
470 471 472 473 474 475 476
      catch (UnmetDependenciesException $e) {
        drupal_set_message(
          $e->getTranslatedMessage($this->getStringTranslation(), $modules['install'][$e->getExtension()]),
          'error'
        );
        return;
      }
477 478 479 480
    }
  }

}