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

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

namespace Drupal\system\Form;

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

/**
 * Builds a confirmation form for enabling modules with dependencies.
 */
21
class ModulesListConfirmForm extends ConfirmFormBase {
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

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

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

  /**
   * An associative list of modules to enable or disable.
   *
   * @var array
   */
  protected $modules = array();

44 45 46 47 48 49 50
  /**
   * The module installer.
   *
   * @var \Drupal\Core\Extension\ModuleInstallerInterface
   */
  protected $moduleInstaller;

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

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('module_handler'),
73
      $container->get('module_installer'),
74 75
      $container->get('keyvalue.expirable')->get('module_list')
    );
76 77 78 79 80 81
  }

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
82
    return $this->t('Some required modules must be enabled');
83 84 85 86 87
  }

  /**
   * {@inheritdoc}
   */
88
  public function getCancelUrl() {
89
    return new Url('system.modules_list');
90 91 92 93 94 95
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
96
    return $this->t('Continue');
97 98 99 100 101 102
  }

  /**
   * {@inheritdoc}
   */
  public function getDescription() {
103
    return $this->t('Would you like to continue with the above?');
104 105 106 107 108
  }

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

  /**
   * {@inheritdoc}
   */
116
  public function buildForm(array $form, FormStateInterface $form_state) {
117
    $account = $this->currentUser()->id();
118 119 120 121
    $this->modules = $this->keyValueExpirable->get($account);

    // Redirect to the modules list page if the key value store is empty.
    if (!$this->modules) {
122
      return $this->redirect('system.modules_list');
123 124 125 126 127 128
    }

    $items = array();
    // Display a list of required modules that have to be installed as well but
    // were not manually selected.
    foreach ($this->modules['dependencies'] as $module => $dependencies) {
129
      $items[] = $this->formatPlural(count($dependencies), 'You must enable the @required module to install @module.', 'You must enable the @required modules to install @module.', array(
130
        '@module' => $this->modules['install'][$module],
131 132 133 134 135 136 137 138 139
        '@required' => implode(', ', $dependencies),
      ));
    }

    $form['message'] = array(
      '#theme' => 'item_list',
      '#items' => $items,
    );

140
    return parent::buildForm($form, $form_state);
141 142 143 144 145
  }

  /**
   * {@inheritdoc}
   */
146
  public function submitForm(array &$form, FormStateInterface $form_state) {
147
    // Remove the key value store entry.
148
    $account = $this->currentUser()->id();
149 150 151 152 153
    $this->keyValueExpirable->delete($account);

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

154 155
    // Install the given modules.
    if (!empty($this->modules['install'])) {
156 157 158 159
      // Don't catch the exception that this can throw for missing dependencies:
      // the form doesn't allow modules with unmet dependencies, so the only way
      // this can happen is if the filesystem changed between form display and
      // submit, in which case the user has bigger problems.
160
      $this->moduleInstaller->install(array_keys($this->modules['install']));
161 162 163 164 165
    }

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

169
    $form_state->setRedirectUrl($this->getCancelUrl());
170 171 172
  }

}