You are here

protected function HandlePdfController::handlePopulatedPdf in FillPDF 8.4

Same name and namespace in other branches
  1. 5.0.x src/Controller/HandlePdfController.php \Drupal\fillpdf\Controller\HandlePdfController::handlePopulatedPdf()

Figure out what to do with the PDF and do it.

Parameters

\Drupal\fillpdf\FillPdfFormInterface $fillpdf_form: An object containing the loaded record from {fillpdf_forms}.

string $pdf_data: A string containing the content of the merged PDF.

array $context: The request context as returned by FillPdfLinkManipulatorInterface::parseLink().

string $filename: Filename of the merged PDF.

\Drupal\Core\Entity\EntityInterface[] $entities: An array of entities to be used for replacing tokens. These may be still needed for generating the destination path, if the file is saved.

Return value

\Symfony\Component\HttpFoundation\Response The action plugin's response object.

Throws

\Drupal\Component\Plugin\Exception\PluginException

\Drupal\Core\Entity\EntityMalformedException

See also

\Drupal\fillpdf\FillPdfLinkManipulatorInterface::parseLink()

1 call to HandlePdfController::handlePopulatedPdf()
HandlePdfController::populatePdf in src/Controller/HandlePdfController.php
Populates PDF template from context.

File

src/Controller/HandlePdfController.php, line 203

Class

HandlePdfController
Class HandlePdfController.

Namespace

Drupal\fillpdf\Controller

Code

protected function handlePopulatedPdf(FillPdfFormInterface $fillpdf_form, $pdf_data, array $context, $filename, array $entities) {
  $force_download = FALSE;
  if (!empty($context['force_download'])) {
    $force_download = TRUE;
  }

  // Determine the appropriate action for the PDF.
  $scheme = $fillpdf_form
    ->getStorageScheme();
  $is_available = array_key_exists($scheme, \Drupal::service('stream_wrapper_manager')
    ->getWrappers(StreamWrapperInterface::WRITE_VISIBLE));
  $is_allowed = in_array($scheme, \Drupal::config('fillpdf.settings')
    ->get('allowed_schemes') ?: []);
  if (empty($scheme)) {
    $action_plugin_id = 'download';
  }
  elseif (!$is_available || !$is_allowed) {

    // @todo: We don't need the ID once an admin_title is #required,
    // see https://www.drupal.org/project/fillpdf/issues/3040776.
    $label = $fillpdf_form
      ->label() . " ({$fillpdf_form->id()})";
    $this
      ->getLogger('fillpdf')
      ->critical('Saving a generated PDF file in unavailable storage scheme %scheme failed.', [
      '%scheme' => "{$scheme}://",
    ]);
    if ($this
      ->currentUser()
      ->hasPermission('administer pdfs')) {
      $this
        ->messenger()
        ->addError($this
        ->t('File storage scheme %scheme:// is unavailable, so a PDF file generated from FillPDF form @link could not be stored.', [
        '%scheme' => $scheme,
        '@link' => Link::fromTextAndUrl($label, $fillpdf_form
          ->toUrl())
          ->toString(),
      ]));
    }

    // Make sure the file is only sent to the browser.
    $action_plugin_id = 'download';
  }
  else {
    $redirect = !empty($fillpdf_form->destination_redirect->value);
    $action_plugin_id = $redirect ? 'redirect' : 'save';
  }

  // @todo: Remove in FillPDF 5.x. The filename is not part of the context and
  // is separately available anyway.
  $context['filename'] = $filename;

  // @todo: Rename 'token_objects' to 'entities' in FillPDF 5.x. Webform
  // submissions are now entities, too.
  $action_configuration = [
    'form' => $fillpdf_form,
    'context' => $context,
    'token_objects' => $entities,
    'data' => $pdf_data,
    'filename' => $filename,
  ];

  /** @var \Drupal\fillpdf\Plugin\FillPdfActionPluginInterface $fillpdf_action */
  $fillpdf_action = $this->actionManager
    ->createInstance($action_plugin_id, $action_configuration);
  $response = $fillpdf_action
    ->execute();

  // If we are forcing a download, then manually get a Response from
  // the download action and return that. Side effects of other plugins will
  // still happen, obviously.
  if ($force_download) {

    /** @var FillPdfDownloadAction $download_action */
    $download_action = $this->actionManager
      ->createInstance('download', $action_configuration);
    $response = $download_action
      ->execute();
  }
  return $response;
}