annotate core/modules/update/src/Form/UpdateManagerUpdate.php @ 0:4c8ae668cc8c

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