You are here

class EntityExportCsvDownload in Entity Export CSV 8

Define the entity export csv download controller.

Hierarchy

Expanded class hierarchy of EntityExportCsvDownload

File

src/Controller/EntityExportCsvDownload.php, line 18

Namespace

Drupal\entity_export_csv\Controller
View source
class EntityExportCsvDownload implements ContainerInjectionInterface {
  use StringTranslationTrait;

  /**
   * The request.
   *
   * @var null|\Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * The csrf token generator.
   *
   * @var \Drupal\Core\Access\CsrfTokenGenerator
   */
  protected $csrfToken;

  /**
   * The entity export csv download constructor.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   * @param \Drupal\Core\Access\CsrfTokenGenerator $csrf_token
   *   The csrf token generator.
   */
  public function __construct(RequestStack $request_stack, CsrfTokenGenerator $csrf_token) {
    $this->request = $request_stack
      ->getCurrentRequest();
    $this->csrfToken = $csrf_token;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('request_stack'), $container
      ->get('csrf_token'));
  }

  /**
   * Build the results page.
   *
   * @return array
   *   A Drupal render array.
   */
  public function resultsExport() {
    $request = $this
      ->getRequest();
    if (!$request->query
      ->has('results')) {
      throw new NotFoundHttpException($this
        ->t('Missing export download results.'));
    }
    $results = $request->query
      ->get('results');
    $file_uri = isset($results['file']) ? $results['file'] : NULL;
    $token = $this->csrfToken
      ->get($file_uri);
    $query_options = [
      'query' => [
        'token' => $token,
        'file' => $file_uri,
      ],
    ];
    $download_url = Url::fromRoute('entity_content_export.download', [], $query_options)
      ->toString();
    $build['results'] = [
      '#markup' => $this
        ->t("The download should automatically start shortly. If it doesn't, click\n         <a data-auto-download href='@download_url'>Download</a>.", [
        '@download_url' => $download_url,
      ]),
      '#attached' => [
        'library' => [
          'entity_content_csv/auto-download',
        ],
      ],
    ];
    $build['actions'] = [
      '#type' => 'actions',
    ];
    $build['actions']['back'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Back to Export page.'),
      '#url' => Url::fromRoute('entity_export_csv.export_form'),
      '#options' => [
        'attributes' => [
          'class' => [
            'btn',
            'btn-primary',
            'btn-export-back',
          ],
        ],
      ],
    ];
    return $build;
  }

  /**
   * Download entity content exported file.
   *
   * @return \Symfony\Component\HttpFoundation\Response
   *   The response.
   */
  public function downloadExport() {
    $token = $this
      ->getRequest()->query
      ->get('token');
    $file_uri = $this
      ->getRequest()->query
      ->get('file');
    if (empty($token) || !$this->csrfToken
      ->validate($token, $file_uri)) {
      throw new AccessDeniedHttpException();
    }
    if (!isset($file_uri) || !file_exists($file_uri)) {
      throw new NotFoundHttpException($this
        ->t('Missing or not found entity content exported file.'));
    }
    return (new BinaryFileResponse($file_uri))
      ->deleteFileAfterSend(TRUE)
      ->setContentDisposition('attachment', basename($file_uri));
  }

  /**
   * Get current request object.
   *
   * @return null|\Symfony\Component\HttpFoundation\Request
   *   The request.
   */
  protected function getRequest() {
    return $this->request;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityExportCsvDownload::$csrfToken protected property The csrf token generator.
EntityExportCsvDownload::$request protected property The request.
EntityExportCsvDownload::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
EntityExportCsvDownload::downloadExport public function Download entity content exported file.
EntityExportCsvDownload::getRequest protected function Get current request object.
EntityExportCsvDownload::resultsExport public function Build the results page.
EntityExportCsvDownload::__construct public function The entity export csv download constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.