You are here

public static function DataExport::finishBatch in Views data export 8

Implements callback for batch finish.

Parameters

bool $success: Indicates whether we hit a fatal PHP error.

array $results: Contains batch results.

array $operations: If $success is FALSE, contains the operations that remained unprocessed.

Return value

\Symfony\Component\HttpFoundation\RedirectResponse Where to redirect when batching ended.

File

src/Plugin/views/display/DataExport.php, line 881

Class

DataExport
Provides a data export display plugin.

Namespace

Drupal\views_data_export\Plugin\views\display

Code

public static function finishBatch($success, array $results, array $operations) {

  // Set Drupal status message to let the user know the results of the export.
  // The 'success' parameter means no fatal PHP errors were detected.
  // All other error management should be handled using 'results'.
  $response = new RedirectResponse($results['redirect_url']);
  if ($success && isset($results['vde_file']) && file_exists($results['vde_file'])) {

    // Check the permissions of the file to grant access and allow
    // modules to hook into permissions via hook_file_download().
    $headers = \Drupal::moduleHandler()
      ->invokeAll('file_download', [
      $results['vde_file'],
    ]);

    // Require at least one module granting access and none denying access.
    if (!empty($headers) && !in_array(-1, $headers)) {

      // Create a web server accessible URL for the private file.
      // Permissions for accessing this URL will be inherited from the View
      // display's configuration.
      $url = file_create_url($results['vde_file']);
      $message = t('Export complete. Download the file <a download href=":download_url"  data-download-enabled="false" id="vde-automatic-download">here</a>.', [
        ':download_url' => $url,
      ]);

      // If the user specified instant download than redirect to the file.
      if ($results['automatic_download']) {

        // Prevents browser from displaying JSON data if automatic download
        // is selected.
        if (!preg_match("/^.*\\.(json)\$/i", $results['vde_file'])) {
          $message = t('Export complete. Download the file <a download href=":download_url" data-download-enabled="true" id="vde-automatic-download">here</a> if file is not automatically downloaded.', [
            ':download_url' => $url,
          ]);
        }
      }
      \Drupal::messenger()
        ->addMessage($message);
    }
    return $response;
  }
  else {
    $message = t('Export failed. Make sure the private file system is configured and check the error log.');
    \Drupal::messenger()
      ->addError($message);
    return $response;
  }
}