You are here

public function UpgradeStatusForm::exportReport in Upgrade Status 8.3

Same name and namespace in other branches
  1. 8 src/Form/UpgradeStatusForm.php \Drupal\upgrade_status\Form\UpgradeStatusForm::exportReport()
  2. 8.2 src/Form/UpgradeStatusForm.php \Drupal\upgrade_status\Form\UpgradeStatusForm::exportReport()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

string $format: Either 'html' or 'ascii' depending on what the format should be.

1 call to UpgradeStatusForm::exportReport()
UpgradeStatusForm::exportReportASCII in src/Form/UpgradeStatusForm.php
Form submission handler.

File

src/Form/UpgradeStatusForm.php, line 1156

Class

UpgradeStatusForm

Namespace

Drupal\upgrade_status\Form

Code

public function exportReport(array &$form, FormStateInterface $form_state, string $format = 'html') {
  $extensions = [];
  $projects = $this->projectCollector
    ->collectProjects();
  $submitted = $form_state
    ->getValues();
  $next_steps = $this->projectCollector
    ->getNextStepInfo();
  foreach ($next_steps as $next_step => $step_label) {
    if (!empty($submitted[$next_step]['data']['list'])) {
      foreach ($submitted[$next_step]['data']['list'] as $item) {
        if (isset($projects[$item])) {
          $type = $projects[$item]->info['upgrade_status_type'] == ProjectCollector::TYPE_CUSTOM ? 'custom' : 'contrib';
          $extensions[$type][$item] = $format == 'html' ? $this->resultFormatter
            ->formatResult($projects[$item]) : $this->resultFormatter
            ->formatAsciiResult($projects[$item]);
        }
      }
    }
  }
  if (empty($extensions)) {
    $this
      ->messenger()
      ->addError('No projects selected to export.');
    return;
  }
  $build = [
    '#theme' => 'upgrade_status_' . $format . '_export',
    '#projects' => $extensions,
  ];
  $fileDate = $this->resultFormatter
    ->formatDateTime(0, 'html_datetime');
  $extension = $format == 'html' ? '.html' : '.txt';
  $filename = 'upgrade-status-export-' . $fileDate . $extension;
  $response = new Response($this->renderer
    ->renderRoot($build));
  $response->headers
    ->set('Content-Disposition', 'attachment; filename="' . $filename . '"');
  $form_state
    ->setResponse($response);
}