You are here

class OutputHandler in FillPDF 5.0.x

Same name and namespace in other branches
  1. 8.4 src/OutputHandler.php \Drupal\fillpdf\OutputHandler

Class OutputHandler.

@package Drupal\fillpdf

Hierarchy

Expanded class hierarchy of OutputHandler

1 file declares its use of OutputHandler
FillPdfSaveAction.php in src/Plugin/FillPdfActionPlugin/FillPdfSaveAction.php
1 string reference to 'OutputHandler'
fillpdf.services.yml in ./fillpdf.services.yml
fillpdf.services.yml
1 service uses OutputHandler
fillpdf.output_handler in ./fillpdf.services.yml
Drupal\fillpdf\OutputHandler

File

src/OutputHandler.php, line 17

Namespace

Drupal\fillpdf
View source
class OutputHandler implements OutputHandlerInterface {
  use StringTranslationTrait;

  /**
   * The FillPDF token resolver.
   *
   * @var \Drupal\fillpdf\TokenResolverInterface
   */
  protected $tokenResolver;

  /**
   * The logger.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * The FillPdf link manipulator.
   *
   * @var \Drupal\fillpdf\FillPdfLinkManipulatorInterface
   */
  protected $linkManipulator;

  /**
   * The file system.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;

  /**
   * OutputHandler constructor.
   *
   * @param \Drupal\fillpdf\TokenResolverInterface $token_resolver
   *   The FillPdf token resolver.
   * @param \Psr\Log\LoggerInterface $logger
   *   The logger.
   * @param \Drupal\fillpdf\FillPdfLinkManipulatorInterface $link_manipulator
   *   The FillPdf link manipulator.
   * @param \Drupal\Core\File\FileSystemInterface $file_system
   *   The file system.
   */
  public function __construct(TokenResolverInterface $token_resolver, LoggerInterface $logger, FillPdfLinkManipulatorInterface $link_manipulator, FileSystemInterface $file_system) {
    $this->tokenResolver = $token_resolver;
    $this->logger = $logger;
    $this->linkManipulator = $link_manipulator;
    $this->fileSystem = $file_system;
  }

  /**
   * {@inheritdoc}
   */
  public function savePdfToFile(array $configuration, $destination_path_override = NULL) {

    /** @var \Drupal\fillpdf\Entity\FillPdfForm $fillpdf_form */
    $fillpdf_form = $configuration['form'];

    // @todo: Rename 'token_objects' to 'entities' in FillPDF 5.x. Webform
    // submissions are now entities, too.

    /** @var \Drupal\Core\Entity\EntityInterface[] $entities */
    $entities = $configuration['token_objects'];
    $destination_path = 'fillpdf';
    if (!empty($destination_path_override)) {
      $destination_path .= "/{$destination_path_override}";
    }
    elseif (!empty($fillpdf_form->destination_path->value)) {
      $destination_path .= "/{$fillpdf_form->destination_path->value}";
    }
    $resolved_destination_path = $this
      ->processDestinationPath(trim($destination_path), $entities, $fillpdf_form->scheme->value);
    $path_exists = $this->fileSystem
      ->prepareDirectory($resolved_destination_path, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
    $saved_file = FALSE;
    if ($path_exists === FALSE) {
      $this->logger
        ->critical($this
        ->t("The path %destination_path does not exist and could not be\n      automatically created. Therefore, the previous submission was not saved. If\n      the URL contained download=1, then the PDF was still sent to the user's browser.\n      If you were redirecting them to the PDF, they were sent to the homepage instead.\n      If the destination path looks wrong and you have used tokens, check that you have\n      used the correct token and that it is available to FillPDF at the time of PDF\n      generation.", [
        '%destination_path' => $resolved_destination_path,
      ]));
    }
    else {

      // Full steam ahead!
      $saved_file = file_save_data($configuration['data'], "{$resolved_destination_path}/{$configuration['filename']}", FileSystemInterface::EXISTS_RENAME);
      $this
        ->rememberFileContext($saved_file, $configuration['context']);
    }
    return $saved_file;
  }

  /**
   * Processes the destination path.
   *
   * @param string $destination_path
   *   The raw destination path, possibly containing unresolved tokens.
   * @param \Drupal\Core\Entity\EntityInterface[] $entities
   *   An array of entities to be used for replacing tokens.
   * @param string $scheme
   *   (optional) The storage scheme. Defaults to 'public'.
   *
   * @return string
   *   The normalized URI
   */
  protected function processDestinationPath($destination_path, array $entities, $scheme = 'public') {
    $destination_path = (string) $this->tokenResolver
      ->replace($destination_path, $entities, [
      'content' => 'text',
    ]);
    return FillPdf::buildFileUri($scheme, $destination_path);
  }

  /**
   * Saves the file context.
   *
   * @param \Drupal\file\FileInterface $fillpdf_file
   *   File object containing the generated PDF file.
   * @param array $context
   *   An associative array representing the context of the generated file.
   *   This array should match the format returned by
   *   FillPdfLinkManipulator::parseLink().
   *
   * @see \Drupal\fillpdf\FillPdfLinkManipulatorInterface::parseLink()
   * @see FileFieldItemList::postSave()
   */
  protected function rememberFileContext(FileInterface $fillpdf_file, array $context) {
    $fillpdf_link = $this->linkManipulator
      ->generateLink($context);
    $fillpdf_file_context = FillPdfFileContext::create([
      'file' => $fillpdf_file,
      'context' => $fillpdf_link
        ->toUriString(),
    ]);

    // The file field will automatically add file usage information upon save.
    $fillpdf_file_context
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OutputHandler::$fileSystem protected property The file system.
OutputHandler::$linkManipulator protected property The FillPdf link manipulator.
OutputHandler::$logger protected property The logger.
OutputHandler::$tokenResolver protected property The FillPDF token resolver.
OutputHandler::processDestinationPath protected function Processes the destination path.
OutputHandler::rememberFileContext protected function Saves the file context.
OutputHandler::savePdfToFile public function Saves merged PDF data to the filesystem. Overrides OutputHandlerInterface::savePdfToFile
OutputHandler::__construct public function OutputHandler constructor.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.