moduleHandler = $module_handler; $this->state = $state; } /** * {@inheritdoc} */ public function getFormId() { return 'update_manager_update_form'; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static( $container->get('module_handler'), $container->get('state') ); } /** * {@inheritdoc} */ public function buildForm(array $form, FormStateInterface $form_state) { $this->moduleHandler->loadInclude('update', 'inc', 'update.manager'); $last_markup = array( '#theme' => 'update_last_check', '#last' => $this->state->get('update.last_check') ?: 0, ); $form['last_check'] = array( '#markup' => \Drupal::service('renderer')->render($last_markup), ); if (!_update_manager_check_backends($form, 'update')) { return $form; } $available = update_get_available(TRUE); if (empty($available)) { $form['message'] = array( '#markup' => $this->t('There was a problem getting update information. Try again later.'), ); return $form; } $form['#attached']['library'][] = 'update/drupal.update.admin'; // This will be a nested array. The first key is the kind of project, which // can be either 'enabled', 'disabled', 'manual' (projects which require // manual updates, such as core). Then, each subarray is an array of // projects of that type, indexed by project short name, and containing an // array of data for cells in that project's row in the appropriate table. $projects = array(); // This stores the actual download link we're going to update from for each // project in the form, regardless of if it's enabled or disabled. $form['project_downloads'] = array('#tree' => TRUE); $this->moduleHandler->loadInclude('update', 'inc', 'update.compare'); $project_data = update_calculate_project_data($available); foreach ($project_data as $name => $project) { // Filter out projects which are up to date already. if ($project['status'] == UPDATE_CURRENT) { continue; } // The project name to display can vary based on the info we have. if (!empty($project['title'])) { if (!empty($project['link'])) { $project_name = $this->l($project['title'], Url::fromUri($project['link'])); } else { $project_name = $project['title']; } } elseif (!empty($project['info']['name'])) { $project_name = $project['info']['name']; } else { $project_name = $name; } if ($project['project_type'] == 'theme' || $project['project_type'] == 'theme-disabled') { $project_name .= ' ' . $this->t('(Theme)'); } if (empty($project['recommended'])) { // If we don't know what to recommend they upgrade to, we should skip // the project entirely. continue; } $recommended_release = $project['releases'][$project['recommended']]; $recommended_version = '{{ release_version }} ({{ release_notes }})'; if ($recommended_release['version_major'] != $project['existing_major']) { $recommended_version .= '
' . $this->t('Updates of Drupal core are not supported at this time.') . '
'; $form['manual_updates'] = array( '#type' => 'table', '#header' => $headers, '#rows' => $projects['manual'], '#prefix' => $prefix, '#weight' => 120, ); } return $form; } /** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { if (!$form_state->isValueEmpty('projects')) { $enabled = array_filter($form_state->getValue('projects')); } if (!$form_state->isValueEmpty('disabled_projects')) { $disabled = array_filter($form_state->getValue('disabled_projects')); } if (empty($enabled) && empty($disabled)) { $form_state->setErrorByName('projects', $this->t('You must select at least one project to update.')); } } /** * {@inheritdoc} */ public function submitForm(array &$form, FormStateInterface $form_state) { $this->moduleHandler->loadInclude('update', 'inc', 'update.manager'); $projects = array(); foreach (array('projects', 'disabled_projects') as $type) { if (!$form_state->isValueEmpty($type)) { $projects = array_merge($projects, array_keys(array_filter($form_state->getValue($type)))); } } $operations = array(); foreach ($projects as $project) { $operations[] = array( 'update_manager_batch_project_get', array( $project, $form_state->getValue(array('project_downloads', $project)), ), ); } $batch = array( 'title' => $this->t('Downloading updates'), 'init_message' => $this->t('Preparing to download selected updates'), 'operations' => $operations, 'finished' => 'update_manager_download_batch_finished', 'file' => drupal_get_path('module', 'update') . '/update.manager.inc', ); batch_set($batch); } }