Newer
Older
Alex Pott
committed
<?php
/**
* @file
* Contains \Drupal\system\Form\ModulesListConfirmForm.
*/
namespace Drupal\system\Form;
use Drupal\Core\Extension\ModuleHandlerInterface;
Alex Pott
committed
use Drupal\Core\Extension\ModuleInstallerInterface;
Alex Pott
committed
use Drupal\Core\Form\ConfirmFormBase;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
Alex Pott
committed
use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
Angie Byron
committed
use Drupal\Core\Url;
Alex Pott
committed
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Builds a confirmation form for enabling modules with dependencies.
*/
Angie Byron
committed
class ModulesListConfirmForm extends ConfirmFormBase {
Alex Pott
committed
/**
* 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();
Alex Pott
committed
/**
* The module installer.
*
* @var \Drupal\Core\Extension\ModuleInstallerInterface
*/
protected $moduleInstaller;
Alex Pott
committed
/**
* Constructs a ModulesListConfirmForm object.
*
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler.
Alex Pott
committed
* @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
* The module installer.
Alex Pott
committed
* @param \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $key_value_expirable
* The key value expirable factory.
*/
Alex Pott
committed
public function __construct(ModuleHandlerInterface $module_handler, ModuleInstallerInterface $module_installer, KeyValueStoreExpirableInterface $key_value_expirable) {
Alex Pott
committed
$this->moduleHandler = $module_handler;
Alex Pott
committed
$this->moduleInstaller = $module_installer;
Alex Pott
committed
$this->keyValueExpirable = $key_value_expirable;
Alex Pott
committed
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('module_handler'),
Alex Pott
committed
$container->get('module_installer'),
Alex Pott
committed
$container->get('keyvalue.expirable')->get('module_list')
);
Alex Pott
committed
}
/**
* {@inheritdoc}
*/
public function getQuestion() {
Alex Pott
committed
return $this->t('Some required modules must be enabled');
Alex Pott
committed
}
/**
* {@inheritdoc}
*/
public function getCancelUrl() {
Angie Byron
committed
return new Url('system.modules_list');
Alex Pott
committed
}
/**
* {@inheritdoc}
*/
public function getConfirmText() {
Alex Pott
committed
return $this->t('Continue');
Alex Pott
committed
}
/**
* {@inheritdoc}
*/
public function getDescription() {
Alex Pott
committed
return $this->t('Would you like to continue with the above?');
Alex Pott
committed
}
/**
* {@inheritdoc}
*/
Alex Pott
committed
public function getFormId() {
Alex Pott
committed
return 'system_modules_confirm_form';
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function buildForm(array $form, FormStateInterface $form_state) {
$account = $this->currentUser()->id();
Alex Pott
committed
$this->modules = $this->keyValueExpirable->get($account);
// Redirect to the modules list page if the key value store is empty.
if (!$this->modules) {
Alex Pott
committed
return $this->redirect('system.modules_list');
Alex Pott
committed
}
$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) {
$items[] = format_plural(count($dependencies), 'You must enable the @required module to install @module.', 'You must enable the @required modules to install @module.', array(
Dries Buytaert
committed
'@module' => $this->modules['install'][$module],
Alex Pott
committed
'@required' => implode(', ', $dependencies),
));
}
$form['message'] = array(
'#theme' => 'item_list',
'#items' => $items,
);
Alex Pott
committed
return parent::buildForm($form, $form_state);
Alex Pott
committed
}
/**
* {@inheritdoc}
*/
Dries Buytaert
committed
public function submitForm(array &$form, FormStateInterface $form_state) {
Alex Pott
committed
// Remove the key value store entry.
$account = $this->currentUser()->id();
Alex Pott
committed
$this->keyValueExpirable->delete($account);
// Gets list of modules prior to install process.
$before = $this->moduleHandler->getModuleList();
Dries Buytaert
committed
// Install the given modules.
if (!empty($this->modules['install'])) {
Alex Pott
committed
$this->moduleInstaller->install(array_keys($this->modules['install']));
Alex Pott
committed
}
// Gets module list after install process, flushes caches and displays a
// message if there are changes.
if ($before != $this->moduleHandler->getModuleList()) {
Alex Pott
committed
drupal_set_message($this->t('The configuration options have been saved.'));
Alex Pott
committed
}
Alex Pott
committed
$form_state->setRedirectUrl($this->getCancelUrl());