ModulesListConfirmForm.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. namespace Drupal\system\Form;
  3. use Drupal\Core\Config\PreExistingConfigException;
  4. use Drupal\Core\Config\UnmetDependenciesException;
  5. use Drupal\Core\Extension\ModuleHandlerInterface;
  6. use Drupal\Core\Extension\ModuleInstallerInterface;
  7. use Drupal\Core\Form\ConfirmFormBase;
  8. use Drupal\Core\Form\FormStateInterface;
  9. use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
  10. use Drupal\Core\Url;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. /**
  13. * Builds a confirmation form for enabling modules with dependencies.
  14. *
  15. * @internal
  16. */
  17. class ModulesListConfirmForm extends ConfirmFormBase {
  18. /**
  19. * The module handler service.
  20. *
  21. * @var \Drupal\Core\Extension\ModuleHandlerInterface
  22. */
  23. protected $moduleHandler;
  24. /**
  25. * The expirable key value store.
  26. *
  27. * @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface
  28. */
  29. protected $keyValueExpirable;
  30. /**
  31. * An associative list of modules to enable or disable.
  32. *
  33. * @var array
  34. */
  35. protected $modules = [];
  36. /**
  37. * The module installer.
  38. *
  39. * @var \Drupal\Core\Extension\ModuleInstallerInterface
  40. */
  41. protected $moduleInstaller;
  42. /**
  43. * Constructs a ModulesListConfirmForm object.
  44. *
  45. * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
  46. * The module handler.
  47. * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
  48. * The module installer.
  49. * @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $key_value_expirable
  50. * The key value expirable factory.
  51. */
  52. public function __construct(ModuleHandlerInterface $module_handler, ModuleInstallerInterface $module_installer, KeyValueStoreExpirableInterface $key_value_expirable) {
  53. $this->moduleHandler = $module_handler;
  54. $this->moduleInstaller = $module_installer;
  55. $this->keyValueExpirable = $key_value_expirable;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public static function create(ContainerInterface $container) {
  61. return new static(
  62. $container->get('module_handler'),
  63. $container->get('module_installer'),
  64. $container->get('keyvalue.expirable')->get('module_list')
  65. );
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function getQuestion() {
  71. return $this->t('Some required modules must be enabled');
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function getCancelUrl() {
  77. return new Url('system.modules_list');
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function getConfirmText() {
  83. return $this->t('Continue');
  84. }
  85. /**
  86. * {@inheritdoc}
  87. */
  88. public function getDescription() {
  89. return $this->t('Would you like to continue with the above?');
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. public function getFormId() {
  95. return 'system_modules_confirm_form';
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function buildForm(array $form, FormStateInterface $form_state) {
  101. $account = $this->currentUser()->id();
  102. $this->modules = $this->keyValueExpirable->get($account);
  103. // Redirect to the modules list page if the key value store is empty.
  104. if (!$this->modules) {
  105. return $this->redirect('system.modules_list');
  106. }
  107. $items = $this->buildMessageList();
  108. $form['message'] = [
  109. '#theme' => 'item_list',
  110. '#items' => $items,
  111. ];
  112. return parent::buildForm($form, $form_state);
  113. }
  114. /**
  115. * Builds the message list for the confirmation form.
  116. *
  117. * @return \Drupal\Component\Render\MarkupInterface[]
  118. * Array of markup for the list of messages on the form.
  119. *
  120. * @see \Drupal\system\Form\ModulesListForm::buildModuleList()
  121. */
  122. protected function buildMessageList() {
  123. $items = [];
  124. if (!empty($this->modules['dependencies'])) {
  125. // Display a list of required modules that have to be installed as well
  126. // but were not manually selected.
  127. foreach ($this->modules['dependencies'] as $module => $dependencies) {
  128. $items[] = $this->formatPlural(count($dependencies), 'You must enable the @required module to install @module.', 'You must enable the @required modules to install @module.', [
  129. '@module' => $this->modules['install'][$module],
  130. // It is safe to implode this because module names are not translated
  131. // markup and so will not be double-escaped.
  132. '@required' => implode(', ', $dependencies),
  133. ]);
  134. }
  135. }
  136. return $items;
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function submitForm(array &$form, FormStateInterface $form_state) {
  142. // Remove the key value store entry.
  143. $account = $this->currentUser()->id();
  144. $this->keyValueExpirable->delete($account);
  145. if (!empty($this->modules['install'])) {
  146. // Don't catch the exception that this can throw for missing dependencies:
  147. // the form doesn't allow modules with unmet dependencies, so the only way
  148. // this can happen is if the filesystem changed between form display and
  149. // submit, in which case the user has bigger problems.
  150. try {
  151. // Install the given modules.
  152. $this->moduleInstaller->install(array_keys($this->modules['install']));
  153. }
  154. catch (PreExistingConfigException $e) {
  155. $config_objects = $e->flattenConfigObjects($e->getConfigObjects());
  156. $this->messenger()->addError(
  157. $this->formatPlural(
  158. count($config_objects),
  159. 'Unable to install @extension, %config_names already exists in active configuration.',
  160. 'Unable to install @extension, %config_names already exist in active configuration.',
  161. [
  162. '%config_names' => implode(', ', $config_objects),
  163. '@extension' => $this->modules['install'][$e->getExtension()],
  164. ])
  165. );
  166. return;
  167. }
  168. catch (UnmetDependenciesException $e) {
  169. $this->messenger()->addError(
  170. $e->getTranslatedMessage($this->getStringTranslation(), $this->modules['install'][$e->getExtension()])
  171. );
  172. return;
  173. }
  174. $module_names = array_values($this->modules['install']);
  175. $this->messenger()->addStatus($this->formatPlural(count($module_names), 'Module %name has been enabled.', '@count modules have been enabled: %names.', [
  176. '%name' => $module_names[0],
  177. '%names' => implode(', ', $module_names),
  178. ]));
  179. }
  180. $form_state->setRedirectUrl($this->getCancelUrl());
  181. }
  182. }