3 namespace Drupal\update\Form;
5 use Drupal\Core\Extension\ModuleHandlerInterface;
6 use Drupal\Core\Form\FormBase;
7 use Drupal\Core\Form\FormStateInterface;
8 use Drupal\Core\State\StateInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Configure update settings for this site.
15 class UpdateManagerUpdate extends FormBase {
20 * @var \Drupal\Core\Extension\ModuleHandlerInterface
22 protected $moduleHandler;
25 * The Drupal state storage service.
27 * @var \Drupal\Core\State\StateInterface
32 * Constructs a new UpdateManagerUpdate object.
34 * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
36 * @param \Drupal\Core\State\StateInterface $state
39 public function __construct(ModuleHandlerInterface $module_handler, StateInterface $state) {
40 $this->moduleHandler = $module_handler;
41 $this->state = $state;
47 public function getFormId() {
48 return 'update_manager_update_form';
54 public static function create(ContainerInterface $container) {
56 $container->get('module_handler'),
57 $container->get('state')
64 public function buildForm(array $form, FormStateInterface $form_state) {
65 $this->moduleHandler->loadInclude('update', 'inc', 'update.manager');
68 '#theme' => 'update_last_check',
69 '#last' => $this->state->get('update.last_check') ?: 0,
71 $form['last_check'] = [
72 '#markup' => drupal_render($last_markup),
75 if (!_update_manager_check_backends($form, 'update')) {
79 $available = update_get_available(TRUE);
80 if (empty($available)) {
82 '#markup' => $this->t('There was a problem getting update information. Try again later.'),
87 $form['#attached']['library'][] = 'update/drupal.update.admin';
89 // This will be a nested array. The first key is the kind of project, which
90 // can be either 'enabled', 'disabled', 'manual' (projects which require
91 // manual updates, such as core). Then, each subarray is an array of
92 // projects of that type, indexed by project short name, and containing an
93 // array of data for cells in that project's row in the appropriate table.
96 // This stores the actual download link we're going to update from for each
97 // project in the form, regardless of if it's enabled or disabled.
98 $form['project_downloads'] = ['#tree' => TRUE];
99 $this->moduleHandler->loadInclude('update', 'inc', 'update.compare');
100 $project_data = update_calculate_project_data($available);
101 foreach ($project_data as $name => $project) {
102 // Filter out projects which are up to date already.
103 if ($project['status'] == UPDATE_CURRENT) {
106 // The project name to display can vary based on the info we have.
107 if (!empty($project['title'])) {
108 if (!empty($project['link'])) {
109 $project_name = $this->l($project['title'], Url::fromUri($project['link']));
112 $project_name = $project['title'];
115 elseif (!empty($project['info']['name'])) {
116 $project_name = $project['info']['name'];
119 $project_name = $name;
121 if ($project['project_type'] == 'theme' || $project['project_type'] == 'theme-disabled') {
122 $project_name .= ' ' . $this->t('(Theme)');
125 if (empty($project['recommended'])) {
126 // If we don't know what to recommend they upgrade to, we should skip
127 // the project entirely.
131 $recommended_release = $project['releases'][$project['recommended']];
132 $recommended_version = '{{ release_version }} (<a href="{{ release_link }}" title="{{ project_title }}">{{ release_notes }}</a>)';
133 if ($recommended_release['version_major'] != $project['existing_major']) {
134 $recommended_version .= '<div title="{{ major_update_warning_title }}" class="update-major-version-warning">{{ major_update_warning_text }}</div>';
137 $recommended_version = [
138 '#type' => 'inline_template',
139 '#template' => $recommended_version,
141 'release_version' => $recommended_release['version'],
142 'release_link' => $recommended_release['release_link'],
143 'project_title' => $this->t('Release notes for @project_title', ['@project_title' => $project['title']]),
144 'major_update_warning_title' => $this->t('Major upgrade warning'),
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.'),
146 'release_notes' => $this->t('Release notes'),
150 // Create an entry for this project.
152 'title' => $project_name,
153 'installed_version' => $project['existing_version'],
154 'recommended_version' => ['data' => $recommended_version],
157 switch ($project['status']) {
158 case UPDATE_NOT_SECURE:
160 $entry['title'] .= ' ' . $this->t('(Security update)');
161 $entry['#weight'] = -2;
165 case UPDATE_NOT_SUPPORTED:
166 $type = 'unsupported';
167 $entry['title'] .= ' ' . $this->t('(Unsupported)');
168 $entry['#weight'] = -1;
172 case UPDATE_NOT_FETCHED:
173 case UPDATE_NOT_CHECKED:
174 case UPDATE_NOT_CURRENT:
175 $type = 'recommended';
179 // Jump out of the switch and onto the next project in foreach.
183 // Use the project title for the tableselect checkboxes.
184 $entry['title'] = ['data' => [
185 '#title' => $entry['title'],
186 '#markup' => $entry['title'],
188 $entry['#attributes'] = ['class' => ['update-' . $type]];
190 // Drupal core needs to be upgraded manually.
191 $needs_manual = $project['project_type'] == 'core';
194 // There are no checkboxes in the 'Manual updates' table so it will be
195 // rendered by '#theme' => 'table', not '#theme' => 'tableselect'. Since
196 // the data formats are incompatible, we convert now to the format
197 // expected by '#theme' => 'table'.
198 unset($entry['#weight']);
199 $attributes = $entry['#attributes'];
200 unset($entry['#attributes']);
206 $form['project_downloads'][$name] = [
208 '#value' => $recommended_release['download_link'],
212 // Based on what kind of project this is, save the entry into the
213 // appropriate subarray.
214 switch ($project['project_type']) {
216 // Core needs manual updates at this time.
217 $projects['manual'][$name] = $entry;
222 $projects['enabled'][$name] = $entry;
225 case 'module-disabled':
226 case 'theme-disabled':
227 $projects['disabled'][$name] = $entry;
232 if (empty($projects)) {
234 '#markup' => $this->t('All of your projects are up to date.'),
241 'data' => $this->t('Name'),
242 'class' => ['update-project-name'],
244 'installed_version' => $this->t('Installed version'),
245 'recommended_version' => $this->t('Recommended version'),
248 if (!empty($projects['enabled'])) {
249 $form['projects'] = [
250 '#type' => 'tableselect',
251 '#header' => $headers,
252 '#options' => $projects['enabled'],
254 if (!empty($projects['disabled'])) {
255 $form['projects']['#prefix'] = '<h2>' . $this->t('Enabled') . '</h2>';
259 if (!empty($projects['disabled'])) {
260 $form['disabled_projects'] = [
261 '#type' => 'tableselect',
262 '#header' => $headers,
263 '#options' => $projects['disabled'],
265 '#prefix' => '<h2>' . $this->t('Disabled') . '</h2>',
269 // If either table has been printed yet, we need a submit button and to
270 // validate the checkboxes.
271 if (!empty($projects['enabled']) || !empty($projects['disabled'])) {
272 $form['actions'] = ['#type' => 'actions'];
273 $form['actions']['submit'] = [
275 '#value' => $this->t('Download these updates'),
279 if (!empty($projects['manual'])) {
280 $prefix = '<h2>' . $this->t('Manual updates required') . '</h2>';
281 $prefix .= '<p>' . $this->t('Updates of Drupal core are not supported at this time.') . '</p>';
282 $form['manual_updates'] = [
284 '#header' => $headers,
285 '#rows' => $projects['manual'],
286 '#prefix' => $prefix,
297 public function validateForm(array &$form, FormStateInterface $form_state) {
298 if (!$form_state->isValueEmpty('projects')) {
299 $enabled = array_filter($form_state->getValue('projects'));
301 if (!$form_state->isValueEmpty('disabled_projects')) {
302 $disabled = array_filter($form_state->getValue('disabled_projects'));
304 if (empty($enabled) && empty($disabled)) {
305 $form_state->setErrorByName('projects', $this->t('You must select at least one project to update.'));
312 public function submitForm(array &$form, FormStateInterface $form_state) {
313 $this->moduleHandler->loadInclude('update', 'inc', 'update.manager');
315 foreach (['projects', 'disabled_projects'] as $type) {
316 if (!$form_state->isValueEmpty($type)) {
317 $projects = array_merge($projects, array_keys(array_filter($form_state->getValue($type))));
321 foreach ($projects as $project) {
323 'update_manager_batch_project_get',
326 $form_state->getValue(['project_downloads', $project]),
331 'title' => $this->t('Downloading updates'),
332 'init_message' => $this->t('Preparing to download selected updates'),
333 'operations' => $operations,
334 'finished' => 'update_manager_download_batch_finished',
335 'file' => drupal_get_path('module', 'update') . '/update.manager.inc',