annotate core/modules/update/src/Form/UpdateManagerUpdate.php @ 19:fa3358dc1485 tip

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