Version 1
[yaffs-website] / web / modules / contrib / devel / src / Form / DevelReinstall.php
1 <?php
2
3 namespace Drupal\devel\Form;
4
5 use Drupal\Core\Extension\ModuleInstallerInterface;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9
10 /**
11  * Display a dropdown of installed modules with the option to reinstall them.
12  */
13 class DevelReinstall extends FormBase {
14
15   /**
16    * The module installer.
17    *
18    * @var \Drupal\Core\Extension\ModuleInstallerInterface
19    */
20   protected $moduleInstaller;
21
22   /**
23    * Constructs a new DevelReinstall form.
24    *
25    * @param \Drupal\Core\Extension\ModuleInstallerInterface $module_installer
26    *   The module installer.
27    */
28   public function __construct(ModuleInstallerInterface $module_installer) {
29     $this->moduleInstaller = $module_installer;
30   }
31
32   /**
33    * {@inheritdoc}
34    */
35   public static function create(ContainerInterface $container) {
36     return new static(
37       $container->get('module_installer')
38     );
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public function getFormId() {
45     return 'devel_reinstall_form';
46   }
47
48   /**
49    * {@inheritdoc}
50    */
51   public function buildForm(array $form, FormStateInterface $form_state) {
52     // Get a list of all available modules.
53     $modules = system_rebuild_module_data();
54
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';
57     });
58
59     $form['filters'] = array(
60       '#type' => 'container',
61       '#attributes' => array(
62         'class' => array('table-filter', 'js-show'),
63       ),
64     );
65     $form['filters']['text'] = array(
66       '#type' => 'search',
67       '#title' => $this->t('Search'),
68       '#size' => 30,
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.'),
75       ),
76     );
77
78     // Only build the rest of the form if there are any modules available to
79     // uninstall;
80     if (empty($uninstallable)) {
81       return $form;
82     }
83
84     $header = array(
85       'name' => $this->t('Name'),
86       'description' => $this->t('Description'),
87     );
88
89     $rows = array();
90
91     foreach ($uninstallable as $module) {
92       $name = $module->info['name'] ? : $module->getName();
93
94       $rows[$module->getName()] = array(
95         'name' => array(
96           'data' => array(
97             '#type' => 'inline_template',
98             '#template' => '<label class="module-name table-filter-text-source">{{ module_name }}</label>',
99             '#context' => array('module_name' => $name),
100           )
101         ),
102         'description' => array(
103           'data' => $module->info['description'],
104           'class' => array('description'),
105         ),
106       );
107     }
108
109     $form['reinstall'] = array(
110       '#type' => 'tableselect',
111       '#header' => $header,
112       '#options' => $rows,
113       '#js_select' => FALSE,
114       '#empty' => $this->t('No modules are available to uninstall.'),
115     );
116
117     $form['#attached']['library'][] = 'system/drupal.system.modules';
118
119     $form['actions'] = array('#type' => 'actions');
120     $form['actions']['submit'] = array(
121       '#type' => 'submit',
122       '#value' => $this->t('Reinstall'),
123     );
124
125     return $form;
126   }
127
128   /**
129    * {@inheritdoc}
130    */
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.'));
135     }
136   }
137
138   /**
139    * {@inheritdoc}
140    */
141   public function submitForm(array &$form, FormStateInterface $form_state) {
142     try {
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))));
148     }
149     catch (\Exception $e) {
150       drupal_set_message($this->t('Unable to reinstall modules. Error: %error.', array('%error' => $e->getMessage())), 'error');
151     }
152   }
153
154 }