comparison core/modules/update/src/Form/UpdateManagerUpdate.php @ 0:c75dbcec494b

Initial commit from drush-created site
author Chris Cannam
date Thu, 05 Jul 2018 14:24:15 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:c75dbcec494b
1 <?php
2
3 namespace Drupal\update\Form;
4
5 use Drupal\Core\Extension\ModuleHandlerInterface;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\State\StateInterface;
9 use Drupal\Core\Url;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13 * Configure update settings for this site.
14 *
15 * @internal
16 */
17 class UpdateManagerUpdate extends FormBase {
18
19 /**
20 * The module handler.
21 *
22 * @var \Drupal\Core\Extension\ModuleHandlerInterface
23 */
24 protected $moduleHandler;
25
26 /**
27 * The Drupal state storage service.
28 *
29 * @var \Drupal\Core\State\StateInterface
30 */
31 protected $state;
32
33 /**
34 * Constructs a new UpdateManagerUpdate object.
35 *
36 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
37 * The module handler.
38 * @param \Drupal\Core\State\StateInterface $state
39 * The state service.
40 */
41 public function __construct(ModuleHandlerInterface $module_handler, StateInterface $state) {
42 $this->moduleHandler = $module_handler;
43 $this->state = $state;
44 }
45
46 /**
47 * {@inheritdoc}
48 */
49 public function getFormId() {
50 return 'update_manager_update_form';
51 }
52
53 /**
54 * {@inheritdoc}
55 */
56 public static function create(ContainerInterface $container) {
57 return new static(
58 $container->get('module_handler'),
59 $container->get('state')
60 );
61 }
62
63 /**
64 * {@inheritdoc}
65 */
66 public function buildForm(array $form, FormStateInterface $form_state) {
67 $this->moduleHandler->loadInclude('update', 'inc', 'update.manager');
68
69 $last_markup = [
70 '#theme' => 'update_last_check',
71 '#last' => $this->state->get('update.last_check') ?: 0,
72 ];
73 $form['last_check'] = [
74 '#markup' => \Drupal::service('renderer')->render($last_markup),
75 ];
76
77 if (!_update_manager_check_backends($form, 'update')) {
78 return $form;
79 }
80
81 $available = update_get_available(TRUE);
82 if (empty($available)) {
83 $form['message'] = [
84 '#markup' => $this->t('There was a problem getting update information. Try again later.'),
85 ];
86 return $form;
87 }
88
89 $form['#attached']['library'][] = 'update/drupal.update.admin';
90
91 // This will be a nested array. The first key is the kind of project, which
92 // can be either 'enabled', 'disabled', 'manual' (projects which require
93 // manual updates, such as core). Then, each subarray is an array of
94 // projects of that type, indexed by project short name, and containing an
95 // array of data for cells in that project's row in the appropriate table.
96 $projects = [];
97
98 // This stores the actual download link we're going to update from for each
99 // project in the form, regardless of if it's enabled or disabled.
100 $form['project_downloads'] = ['#tree' => TRUE];
101 $this->moduleHandler->loadInclude('update', 'inc', 'update.compare');
102 $project_data = update_calculate_project_data($available);
103 foreach ($project_data as $name => $project) {
104 // Filter out projects which are up to date already.
105 if ($project['status'] == UPDATE_CURRENT) {
106 continue;
107 }
108 // The project name to display can vary based on the info we have.
109 if (!empty($project['title'])) {
110 if (!empty($project['link'])) {
111 $project_name = $this->l($project['title'], Url::fromUri($project['link']));
112 }
113 else {
114 $project_name = $project['title'];
115 }
116 }
117 elseif (!empty($project['info']['name'])) {
118 $project_name = $project['info']['name'];
119 }
120 else {
121 $project_name = $name;
122 }
123 if ($project['project_type'] == 'theme' || $project['project_type'] == 'theme-disabled') {
124 $project_name .= ' ' . $this->t('(Theme)');
125 }
126
127 if (empty($project['recommended'])) {
128 // If we don't know what to recommend they upgrade to, we should skip
129 // the project entirely.
130 continue;
131 }
132
133 $recommended_release = $project['releases'][$project['recommended']];
134 $recommended_version = '{{ release_version }} (<a href="{{ release_link }}" title="{{ project_title }}">{{ release_notes }}</a>)';
135 if ($recommended_release['version_major'] != $project['existing_major']) {
136 $recommended_version .= '<div title="{{ major_update_warning_title }}" class="update-major-version-warning">{{ major_update_warning_text }}</div>';
137 }
138
139 $recommended_version = [
140 '#type' => 'inline_template',
141 '#template' => $recommended_version,
142 '#context' => [
143 'release_version' => $recommended_release['version'],
144 'release_link' => $recommended_release['release_link'],
145 'project_title' => $this->t('Release notes for @project_title', ['@project_title' => $project['title']]),
146 'major_update_warning_title' => $this->t('Major upgrade warning'),
147 'major_update_warning_text' => $this->t('This update is a major version update which means that it may not be backwards compatible with your currently running version. It is recommended that you read the release notes and proceed at your own risk.'),
148 'release_notes' => $this->t('Release notes'),
149 ],
150 ];
151
152 // Create an entry for this project.
153 $entry = [
154 'title' => $project_name,
155 'installed_version' => $project['existing_version'],
156 'recommended_version' => ['data' => $recommended_version],
157 ];
158
159 switch ($project['status']) {
160 case UPDATE_NOT_SECURE:
161 case UPDATE_REVOKED:
162 $entry['title'] .= ' ' . $this->t('(Security update)');
163 $entry['#weight'] = -2;
164 $type = 'security';
165 break;
166
167 case UPDATE_NOT_SUPPORTED:
168 $type = 'unsupported';
169 $entry['title'] .= ' ' . $this->t('(Unsupported)');
170 $entry['#weight'] = -1;
171 break;
172
173 case UPDATE_UNKNOWN:
174 case UPDATE_NOT_FETCHED:
175 case UPDATE_NOT_CHECKED:
176 case UPDATE_NOT_CURRENT:
177 $type = 'recommended';
178 break;
179
180 default:
181 // Jump out of the switch and onto the next project in foreach.
182 continue 2;
183 }
184
185 // Use the project title for the tableselect checkboxes.
186 $entry['title'] = [
187 'data' => [
188 '#title' => $entry['title'],
189 '#markup' => $entry['title'],
190 ],
191 ];
192 $entry['#attributes'] = ['class' => ['update-' . $type]];
193
194 // Drupal core needs to be upgraded manually.
195 $needs_manual = $project['project_type'] == 'core';
196
197 if ($needs_manual) {
198 // There are no checkboxes in the 'Manual updates' table so it will be
199 // rendered by '#theme' => 'table', not '#theme' => 'tableselect'. Since
200 // the data formats are incompatible, we convert now to the format
201 // expected by '#theme' => 'table'.
202 unset($entry['#weight']);
203 $attributes = $entry['#attributes'];
204 unset($entry['#attributes']);
205 $entry = [
206 'data' => $entry,
207 ] + $attributes;
208 }
209 else {
210 $form['project_downloads'][$name] = [
211 '#type' => 'value',
212 '#value' => $recommended_release['download_link'],
213 ];
214 }
215
216 // Based on what kind of project this is, save the entry into the
217 // appropriate subarray.
218 switch ($project['project_type']) {
219 case 'core':
220 // Core needs manual updates at this time.
221 $projects['manual'][$name] = $entry;
222 break;
223
224 case 'module':
225 case 'theme':
226 $projects['enabled'][$name] = $entry;
227 break;
228
229 case 'module-disabled':
230 case 'theme-disabled':
231 $projects['disabled'][$name] = $entry;
232 break;
233 }
234 }
235
236 if (empty($projects)) {
237 $form['message'] = [
238 '#markup' => $this->t('All of your projects are up to date.'),
239 ];
240 return $form;
241 }
242
243 $headers = [
244 'title' => [
245 'data' => $this->t('Name'),
246 'class' => ['update-project-name'],
247 ],
248 'installed_version' => $this->t('Installed version'),
249 'recommended_version' => $this->t('Recommended version'),
250 ];
251
252 if (!empty($projects['enabled'])) {
253 $form['projects'] = [
254 '#type' => 'tableselect',
255 '#header' => $headers,
256 '#options' => $projects['enabled'],
257 ];
258 if (!empty($projects['disabled'])) {
259 $form['projects']['#prefix'] = '<h2>' . $this->t('Enabled') . '</h2>';
260 }
261 }
262
263 if (!empty($projects['disabled'])) {
264 $form['disabled_projects'] = [
265 '#type' => 'tableselect',
266 '#header' => $headers,
267 '#options' => $projects['disabled'],
268 '#weight' => 1,
269 '#prefix' => '<h2>' . $this->t('Disabled') . '</h2>',
270 ];
271 }
272
273 // If either table has been printed yet, we need a submit button and to
274 // validate the checkboxes.
275 if (!empty($projects['enabled']) || !empty($projects['disabled'])) {
276 $form['actions'] = ['#type' => 'actions'];
277 $form['actions']['submit'] = [
278 '#type' => 'submit',
279 '#value' => $this->t('Download these updates'),
280 ];
281 }
282
283 if (!empty($projects['manual'])) {
284 $prefix = '<h2>' . $this->t('Manual updates required') . '</h2>';
285 $prefix .= '<p>' . $this->t('Automatic updates of Drupal core are not supported at this time.') . '</p>';
286 $form['manual_updates'] = [
287 '#type' => 'table',
288 '#header' => $headers,
289 '#rows' => $projects['manual'],
290 '#prefix' => $prefix,
291 '#weight' => 120,
292 ];
293 }
294
295 return $form;
296 }
297
298 /**
299 * {@inheritdoc}
300 */
301 public function validateForm(array &$form, FormStateInterface $form_state) {
302 if (!$form_state->isValueEmpty('projects')) {
303 $enabled = array_filter($form_state->getValue('projects'));
304 }
305 if (!$form_state->isValueEmpty('disabled_projects')) {
306 $disabled = array_filter($form_state->getValue('disabled_projects'));
307 }
308 if (empty($enabled) && empty($disabled)) {
309 $form_state->setErrorByName('projects', $this->t('You must select at least one project to update.'));
310 }
311 }
312
313 /**
314 * {@inheritdoc}
315 */
316 public function submitForm(array &$form, FormStateInterface $form_state) {
317 $this->moduleHandler->loadInclude('update', 'inc', 'update.manager');
318 $projects = [];
319 foreach (['projects', 'disabled_projects'] as $type) {
320 if (!$form_state->isValueEmpty($type)) {
321 $projects = array_merge($projects, array_keys(array_filter($form_state->getValue($type))));
322 }
323 }
324 $operations = [];
325 foreach ($projects as $project) {
326 $operations[] = [
327 'update_manager_batch_project_get',
328 [
329 $project,
330 $form_state->getValue(['project_downloads', $project]),
331 ],
332 ];
333 }
334 $batch = [
335 'title' => $this->t('Downloading updates'),
336 'init_message' => $this->t('Preparing to download selected updates'),
337 'operations' => $operations,
338 'finished' => 'update_manager_download_batch_finished',
339 'file' => drupal_get_path('module', 'update') . '/update.manager.inc',
340 ];
341 batch_set($batch);
342 }
343
344 }