ModulesListForm.php 19.2 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

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

namespace Drupal\system\Form;

10
use Drupal\Component\Utility\String;
11
use Drupal\Component\Utility\Unicode;
12
use Drupal\Core\Controller\TitleResolverInterface;
13
use Drupal\Core\Access\AccessManagerInterface;
14
use Drupal\Core\Entity\EntityManagerInterface;
15
use Drupal\Core\Extension\Extension;
16
use Drupal\Core\Extension\ModuleHandlerInterface;
17
use Drupal\Core\Form\FormBase;
18
use Drupal\Core\Form\FormStateInterface;
19
use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
20
use Drupal\Core\Menu\MenuLinkManagerInterface;
21
use Drupal\Core\Render\Element;
22
use Drupal\Core\Routing\RouteMatchInterface;
23
use Drupal\Core\Routing\RouteProviderInterface;
24
use Drupal\Core\Session\AccountInterface;
25
use Symfony\Component\DependencyInjection\ContainerInterface;
26
use Symfony\Component\HttpFoundation\Request;
27 28

/**
29
 * Provides module installation interface.
30 31 32
 *
 * The list of modules gets populated by module.info.yml files, which contain
 * each module's name, description, and information about which modules it
33
 * requires. See \Drupal\Core\Extension\InfoParser for info on module.info.yml
34 35
 * descriptors.
 */
36
class ModulesListForm extends FormBase {
37

38 39 40 41 42 43 44
  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

45 46 47 48 49 50 51 52 53 54 55 56 57 58
  /**
   * The module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

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

59 60 61 62 63 64 65 66
  /**
   * The entity manager.
   *
   * @var \Drupal\Core\Entity\EntityManagerInterface
   */
  protected $entityManager;

  /**
67
   * The title resolver.
68
   *
69
   * @var \Drupal\Core\Controller\TitleResolverInterface
70
   */
71 72 73 74 75 76 77 78
  protected $titleResolver;

  /**
   * The route provider.
   *
   * @var \Drupal\Core\Routing\RouteProviderInterface
   */
  protected $routeProvider;
79

80 81 82 83 84 85 86
  /**
   * The current route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

87 88 89 90 91 92 93
  /**
   * The menu link manager.
   *
   * @var \Drupal\Core\Menu\MenuLinkManagerInterface
   */
  protected $menuLinkManager;

94 95 96 97 98 99
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('module_handler'),
100
      $container->get('keyvalue.expirable')->get('module_list'),
101 102
      $container->get('access_manager'),
      $container->get('entity.manager'),
103
      $container->get('current_user'),
104 105 106 107
      $container->get('current_route_match'),
      $container->get('title_resolver'),
      $container->get('router.route_provider'),
      $container->get('plugin.manager.menu.link')
108 109 110 111 112 113 114 115 116 117
    );
  }

  /**
   * Constructs a ModulesListForm object.
   *
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler.
   * @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $key_value_expirable
   *   The key value expirable factory.
118
   * @param \Drupal\Core\Access\AccessManagerInterface $access_manager
119
   *   Access manager.
120 121
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager.
122 123
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
124 125
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
126 127 128 129 130 131
   * @param \Drupal\Core\Controller\TitleResolverInterface $title_resolver
   *   The title resolver.
   * @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
   *   The route provider.
   * @param \Drupal\Core\Menu\MenuLinkManagerInterface $menu_link_manager
   *   The menu link manager.
132
   */
133
  public function __construct(ModuleHandlerInterface $module_handler, KeyValueStoreExpirableInterface $key_value_expirable, AccessManagerInterface $access_manager, EntityManagerInterface $entity_manager, AccountInterface $current_user,  RouteMatchInterface $route_match, TitleResolverInterface $title_resolver, RouteProviderInterface $route_provider, MenuLinkManagerInterface $menu_link_manager) {
134 135
    $this->moduleHandler = $module_handler;
    $this->keyValueExpirable = $key_value_expirable;
136
    $this->accessManager = $access_manager;
137
    $this->entityManager = $entity_manager;
138
    $this->currentUser = $current_user;
139
    $this->routeMatch = $route_match;
140 141 142
    $this->titleResolver = $title_resolver;
    $this->routeProvider = $route_provider;
    $this->menuLinkManager = $menu_link_manager;
143 144 145 146 147
  }

  /**
   * {@inheritdoc}
   */
148
  public function getFormId() {
149 150 151 152 153 154
    return 'system_modules';
  }

  /**
   * {@inheritdoc}
   */
155
  public function buildForm(array $form, FormStateInterface $form_state) {
156
    require_once DRUPAL_ROOT . '/core/includes/install.inc';
157
    $distribution = String::checkPlain(drupal_install_profile_distribution_name());
158 159 160 161 162 163 164 165 166 167 168 169 170

    // 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',
171
      '#title' => $this->t('Search'),
172
      '#size' => 30,
173
      '#placeholder' => $this->t('Enter module name'),
174 175 176 177
      '#attributes' => array(
        'class' => array('table-filter-text'),
        'data-table' => '#system-modules',
        'autocomplete' => 'off',
178
        'title' => $this->t('Enter a part of the module name or description to filter by.'),
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
      ),
    );

    // 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.
    $form['modules']['#tree'] = TRUE;
    foreach ($modules as $filename => $module) {
      if (empty($module->info['hidden'])) {
        $package = $module->info['package'];
        $form['modules'][$package][$filename] = $this->buildRow($modules, $module, $distribution);
      }
    }

    // Add a wrapper around every package.
196
    foreach (Element::children($form['modules']) as $package) {
197 198
      $form['modules'][$package] += array(
        '#type' => 'details',
199
        '#title' => $this->t($package),
200
        '#open' => TRUE,
201 202
        '#theme' => 'system_modules_details',
        '#header' => array(
203 204 205
          array('data' => $this->t('Installed'), 'class' => array('checkbox', 'visually-hidden')),
          array('data' => $this->t('Name'), 'class' => array('name', 'visually-hidden')),
          array('data' => $this->t('Description'), 'class' => array('description', 'visually-hidden', RESPONSIVE_PRIORITY_LOW)),
206
        ),
207
        '#attributes' => array('class' => array('package-listing')),
208 209 210 211 212 213
        // Ensure that the "Core" package comes first.
        '#weight' => $package == 'Core' ? -10 : NULL,
      );
    }

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

216
    $form['#attached']['library'][] = 'system/drupal.system.modules';
217 218 219
    $form['actions'] = array('#type' => 'actions');
    $form['actions']['submit'] = array(
      '#type' => 'submit',
220
      '#value' => $this->t('Save configuration'),
221 222 223 224 225 226 227 228 229 230
    );

    return $form;
  }

  /**
   * Builds a table row for the system modules page.
   *
   * @param array $modules
   *   The list existing modules.
231
   * @param \Drupal\Core\Extension\Extension $module
232 233 234 235 236 237
   *   The module for which to build the form row.
   * @param $distribution
   *
   * @return array
   *   The form row for the given module.
   */
238
  protected function buildRow(array $modules, Extension $module, $distribution) {
239 240 241 242 243 244
    // Set the basic properties.
    $row['#required'] = array();
    $row['#requires'] = array();
    $row['#required_by'] = array();

    $row['name']['#markup'] = $module->info['name'];
245
    $row['description']['#markup'] = $this->t($module->info['description']);
246 247 248 249
    $row['version']['#markup'] = $module->info['version'];

    // Generate link for module's help page, if there is one.
    $row['links']['help'] = array();
250
    if ($this->moduleHandler->moduleExists('help') && $module->status && in_array($module->getName(), $this->moduleHandler->getImplementations('help'))) {
251
      if ($this->moduleHandler->invoke($module->getName(), 'help', array('help.page.' . $module->getName(), $this->routeMatch))) {
252 253
        $row['links']['help'] = array(
          '#type' => 'link',
254
          '#title' => $this->t('Help'),
255
          '#href' => 'admin/help/' . $module->getName(),
256
          '#options' => array('attributes' => array('class' =>  array('module-link', 'module-link-help'), 'title' => $this->t('Help'))),
257 258 259 260 261 262
        );
      }
    }

    // Generate link for module's permission, if the user has access to it.
    $row['links']['permissions'] = array();
263
    if ($module->status && \Drupal::currentUser()->hasPermission('administer permissions') && in_array($module->getName(), $this->moduleHandler->getImplementations('permission'))) {
264 265
      $row['links']['permissions'] = array(
        '#type' => 'link',
266
        '#title' => $this->t('Permissions'),
267
        '#href' => 'admin/people/permissions',
268
        '#options' => array('fragment' => 'module-' . $module->getName(), 'attributes' => array('class' => array('module-link', 'module-link-permissions'), 'title' => $this->t('Configure permissions'))),
269 270 271 272 273 274
      );
    }

    // Generate link for module's configuration page, if it has one.
    $row['links']['configure'] = array();
    if ($module->status && isset($module->info['configure'])) {
275 276
      $route_parameters = isset($module->info['configure_parameters']) ? $module->info['configure_parameters'] : array();
      if ($this->accessManager->checkNamedRoute($module->info['configure'], $route_parameters, $this->currentUser)) {
277 278 279 280 281 282 283 284 285 286 287 288 289 290

        $links = $this->menuLinkManager->loadLinksByRoute($module->info['configure']);
        /** @var \Drupal\Core\Menu\MenuLinkInterface $link */
        $link = reset($links);
        // Most configure links have a corresponding menu link, though some just
        // have a route.
        if ($link) {
          $description = $link->getDescription();
        }
        else {
          $request = new Request();
          $request->attributes->set('_route_name', $module->info['configure']);
          $route_object = $this->routeProvider->getRouteByName($module->info['configure']);
          $request->attributes->set('_route', $route_object);
291
          $request->attributes->add($route_parameters);
292 293 294
          $description = $this->titleResolver->getTitle($request, $route_object);
        }

295 296
        $row['links']['configure'] = array(
          '#type' => 'link',
297
          '#title' => $this->t('Configure'),
298
          '#route_name' => $module->info['configure'],
299
          '#route_parameters' => $route_parameters,
300 301 302
          '#options' => array(
            'attributes' => array(
              'class' => array('module-link', 'module-link-configure'),
303
              'title' => $description,
304 305
            ),
          ),
306 307 308 309 310 311 312
        );
      }
    }

    // Present a checkbox for installing and indicating the status of a module.
    $row['enable'] = array(
      '#type' => 'checkbox',
313
      '#title' => $this->t('Install'),
314
      '#default_value' => (bool) $module->status,
315
      '#disabled' => (bool) $module->status,
316 317 318 319 320 321 322 323 324 325 326
    );

    // 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;
      $row['#required_by'][] = $distribution . (!empty($module->info['explanation']) ? ' ('. $module->info['explanation'] .')' : '');
    }

    // Check the compatibilities.
    $compatible = TRUE;
327 328 329 330

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

    // Check the core compatibility.
333
    if ($module->info['core'] != \Drupal::CORE_COMPATIBILITY) {
334
      $compatible = FALSE;
335
      $reasons[] = $this->t('This version is not compatible with Drupal !core_version and should be replaced.', array(
336
        '!core_version' => \Drupal::CORE_COMPATIBILITY,
337 338 339 340 341 342 343
      ));
    }

    // 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 ? '.*' : '');
344
      $reasons[] = $this->t('This module requires PHP version @php_required and is incompatible with PHP version !php_version.', array(
345 346 347 348 349 350 351
        '@php_required' => $required,
        '!php_version' => phpversion(),
      ));
    }

    // If this module is not compatible, disable the checkbox.
    if (!$compatible) {
352
      $status = implode(' ', $reasons);
353
      $row['enable']['#disabled'] = TRUE;
354 355
      $row['description']['#markup'] = $status;
      $row['#attributes']['class'][] = 'incompatible';
356 357 358 359 360
    }

    // If this module requires other modules, add them to the array.
    foreach ($module->requires as $dependency => $version) {
      if (!isset($modules[$dependency])) {
361
        $row['#requires'][$dependency] = $this->t('@module (<span class="admin-missing">missing</span>)', array('@module' => Unicode::ucfirst($dependency)));
362 363 364 365 366 367 368
        $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.
369
        if ($incompatible_version = drupal_check_incompatibility($version, str_replace(\Drupal::CORE_COMPATIBILITY . '-', '', $modules[$dependency]->info['version']))) {
370
          $row['#requires'][$dependency] = $this->t('@module (<span class="admin-missing">incompatible with</span> version @version)', array(
371 372 373 374 375 376 377
            '@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.
378
        elseif ($modules[$dependency]->info['core'] != \Drupal::CORE_COMPATIBILITY) {
379
          $row['#requires'][$dependency] = $this->t('@module (<span class="admin-missing">incompatible with</span> this version of Drupal core)', array(
380 381 382 383 384
            '@module' => $name,
          ));
          $row['enable']['#disabled'] = TRUE;
        }
        elseif ($modules[$dependency]->status) {
385
          $row['#requires'][$dependency] = $this->t('@module', array('@module' => $name));
386 387
        }
        else {
388
          $row['#requires'][$dependency] = $this->t('@module (<span class="admin-disabled">disabled</span>)', array('@module' => $name));
389 390 391 392 393 394 395 396 397
        }
      }
    }

    // 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) {
398
          $row['#required_by'][$dependent] = $this->t('@module', array('@module' => $modules[$dependent]->info['name']));
399 400 401
          $row['enable']['#disabled'] = TRUE;
        }
        else {
402
          $row['#required_by'][$dependent] = $this->t('@module (<span class="admin-disabled">disabled</span>)', array('@module' => $modules[$dependent]->info['name']));
403 404 405 406 407 408 409 410
        }
      }
    }

    return $row;
  }

  /**
411
   * Helper function for building a list of modules to install.
412
   *
413 414
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The current state of the form.
415 416
   *
   * @return array
417
   *   An array of modules to install and their dependencies.
418
   */
419
  protected function buildModuleList(FormStateInterface $form_state) {
420
    $packages = $form_state->getValue('modules');
421

422
    // Build a list of modules to install.
423
    $modules = array(
424
      'install' => array(),
425 426 427
      'dependencies' => array(),
    );

428
    // Required modules have to be installed.
429 430 431
    // @todo This should really not be handled here.
    $data = system_rebuild_module_data();
    foreach ($data as $name => $module) {
432 433
      if (!empty($module->required) && !$this->moduleHandler->moduleExists($name)) {
        $modules['install'][$name] = $module->info['name'];
434 435 436 437 438 439
      }
    }

    // First, build a list of all modules that were selected.
    foreach ($packages as $items) {
      foreach ($items as $name => $checkbox) {
440 441
        if ($checkbox['enable'] && !$this->moduleHandler->moduleExists($name)) {
          $modules['install'][$name] = $data[$name]->info['name'];
442 443 444 445 446
        }
      }
    }

    // Add all dependencies to a list.
447
    while (list($module) = each($modules['install'])) {
448
      foreach (array_keys($data[$module]->requires) as $dependency) {
449
        if (!isset($modules['install'][$dependency]) && !$this->moduleHandler->moduleExists($dependency)) {
450
          $modules['dependencies'][$module][$dependency] = $data[$dependency]->info['name'];
451
          $modules['install'][$dependency] = $data[$dependency]->info['name'];
452 453 454 455 456 457 458 459 460
        }
      }
    }

    // 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.
461 462 463
    foreach (array_keys($modules['install']) as $module) {
      if (!drupal_check_module($module)) {
        unset($modules['install'][$module]);
464
        foreach (array_keys($data[$module]->required_by) as $dependent) {
465
          unset($modules['install'][$dependent]);
466 467 468 469 470 471 472 473 474 475 476
          unset($modules['dependencies'][$dependent]);
        }
      }
    }

    return $modules;
  }

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

481 482 483
    // Check if we have to install any dependencies. If there is one or more
    // dependencies that are not installed yet, redirect to the confirmation
    // form.
484 485
    if (!empty($modules['dependencies']) || !empty($modules['missing'])) {
      // Write the list of changed module states into a key value store.
486
      $account = $this->currentUser()->id();
487 488 489
      $this->keyValueExpirable->setWithExpire($account, $modules, 60);

      // Redirect to the confirmation form.
490
      $form_state->setRedirect('system.modules_list_confirm');
491 492 493 494 495 496 497 498 499 500

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

    // Gets list of modules prior to install process.
    $before = $this->moduleHandler->getModuleList();

    // There seem to be no dependencies that would need approval.
501 502
    if (!empty($modules['install'])) {
      $this->moduleHandler->install(array_keys($modules['install']));
503 504 505 506 507 508 509 510 511 512 513
    }

    // Gets module list after install process, flushes caches and displays a
    // message if there are changes.
    if ($before != $this->moduleHandler->getModuleList()) {
      drupal_flush_all_caches();
      drupal_set_message(t('The configuration options have been saved.'));
    }
  }

}