3 namespace Drupal\devel\Form;
5 use Drupal\Core\Extension\ModuleInstallerInterface;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
11 * Display a dropdown of installed modules with the option to reinstall them.
13 class DevelReinstall extends FormBase {
16 * The module installer.
18 * @var \Drupal\Core\Extension\ModuleInstallerInterface
20 protected $moduleInstaller;
23 * Constructs a new DevelReinstall form.
25 * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
26 * The module installer.
28 public function __construct(ModuleInstallerInterface $module_installer) {
29 $this->moduleInstaller = $module_installer;
35 public static function create(ContainerInterface $container) {
37 $container->get('module_installer')
44 public function getFormId() {
45 return 'devel_reinstall_form';
51 public function buildForm(array $form, FormStateInterface $form_state) {
52 // Get a list of all available modules.
53 $modules = system_rebuild_module_data();
55 $uninstallable = array_filter($modules, function ($module) use ($modules) {
56 return empty($modules[$module->getName()]->info['required']) && drupal_get_installed_schema_version($module->getName()) > SCHEMA_UNINSTALLED && $module->getName() !== 'devel';
59 $form['filters'] = array(
60 '#type' => 'container',
61 '#attributes' => array(
62 'class' => array('table-filter', 'js-show'),
65 $form['filters']['text'] = array(
67 '#title' => $this->t('Search'),
69 '#placeholder' => $this->t('Enter module name'),
70 '#attributes' => array(
71 'class' => array('table-filter-text'),
72 'data-table' => '#devel-reinstall-form',
73 'autocomplete' => 'off',
74 'title' => $this->t('Enter a part of the module name or description to filter by.'),
78 // Only build the rest of the form if there are any modules available to
80 if (empty($uninstallable)) {
85 'name' => $this->t('Name'),
86 'description' => $this->t('Description'),
91 foreach ($uninstallable as $module) {
92 $name = $module->info['name'] ? : $module->getName();
94 $rows[$module->getName()] = array(
97 '#type' => 'inline_template',
98 '#template' => '<label class="module-name table-filter-text-source">{{ module_name }}</label>',
99 '#context' => array('module_name' => $name),
102 'description' => array(
103 'data' => $module->info['description'],
104 'class' => array('description'),
109 $form['reinstall'] = array(
110 '#type' => 'tableselect',
111 '#header' => $header,
113 '#js_select' => FALSE,
114 '#empty' => $this->t('No modules are available to uninstall.'),
117 $form['#attached']['library'][] = 'system/drupal.system.modules';
119 $form['actions'] = array('#type' => 'actions');
120 $form['actions']['submit'] = array(
122 '#value' => $this->t('Reinstall'),
131 public function validateForm(array &$form, FormStateInterface $form_state) {
132 // Form submitted, but no modules selected.
133 if (!array_filter($form_state->getValue('reinstall'))) {
134 $form_state->setErrorByName('reinstall', $this->t('No modules selected.'));
141 public function submitForm(array &$form, FormStateInterface $form_state) {
143 $modules = $form_state->getValue('reinstall');
144 $reinstall = array_keys(array_filter($modules));
145 $this->moduleInstaller->uninstall($reinstall, FALSE);
146 $this->moduleInstaller->install($reinstall, FALSE);
147 drupal_set_message($this->t('Uninstalled and installed: %names.', array('%names' => implode(', ', $reinstall))));
149 catch (\Exception $e) {
150 drupal_set_message($this->t('Unable to reinstall modules. Error: %error.', array('%error' => $e->getMessage())), 'error');