You are here

class PdfDownload in Entity Print 8

Downloads the PDF for an entity.

@Action( id = "entity_print_download_action", label = @Translation("Download PDF"), type = "node" )

@TODO, support multiple entity types once core is fixed.

Hierarchy

Expanded class hierarchy of PdfDownload

See also

https://www.drupal.org/node/2011038

File

src/Plugin/Action/PdfDownload.php, line 30

Namespace

Drupal\entity_print\Plugin\Action
View source
class PdfDownload extends ActionBase implements ContainerFactoryPluginInterface {

  /**
   * Access manager.
   *
   * @var \Drupal\Core\Access\AccessManagerInterface
   */
  protected $accessManager;

  /**
   * The PDF builder service.
   *
   * @var \Drupal\entity_print\PdfBuilderInterface
   */
  protected $pdfBuilder;

  /**
   * The Entity Print plugin manager.
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface
   */
  protected $pluginManager;

  /**
   * Our custom configuration.
   *
   * @var \Drupal\Core\Config\ImmutableConfig
   */
  protected $entityPrintConfig;

  /**
   * The PDF engine implementation.
   *
   * @var \Drupal\entity_print\Plugin\PdfEngineInterface
   */
  protected $pdfEngine;

  /**
   * {@inheritdoc}
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, AccessManagerInterface $access_manager, PdfBuilderInterface $pdf_builder, PluginManagerInterface $plugin_manager, ImmutableConfig $entity_print_config) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->accessManager = $access_manager;
    $this->pdfBuilder = $pdf_builder;
    $this->pluginManager = $plugin_manager;
    $this->entityPrintConfig = $entity_print_config;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('access_manager'), $container
      ->get('entity_print.pdf_manager'), $container
      ->get('plugin.manager.entity_print.pdf_engine'), $container
      ->get('config.factory')
      ->get('entity_print.settings'));
  }

  /**
   * {@inheritdoc}
   */
  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {

    /** @var \Drupal\node\NodeInterface $object */
    $route_params = [
      'entity_id' => $object
        ->id(),
      'entity_type' => $object
        ->getEntityTypeId(),
    ];
    return $this->accessManager
      ->checkNamedRoute('entity_print.view', $route_params, $account, $return_as_object);
  }

  /**
   * {@inheritdoc}
   */
  public function execute($entity = NULL) {
    $this
      ->sendResponse(function () use ($entity) {
      $this->pdfBuilder
        ->getEntityRenderedAsPdf($entity, $this
        ->getPdfEngine(), TRUE);
    });
  }

  /**
   * {@inheritdoc}
   */
  public function executeMultiple(array $entities) {
    $this
      ->sendResponse(function () use ($entities) {
      $this->pdfBuilder
        ->getMultipleEntitiesRenderedAsPdf($entities, $this
        ->getPdfEngine(), TRUE);
    });
  }

  /**
   * Sends the response using a stream and catches any errors.
   *
   * @param callable $callback
   *   The callable responding for rendering the content.
   */
  protected function sendResponse(callable $callback) {
    try {
      (new StreamedResponse($callback))
        ->send();
    } catch (PdfEngineException $e) {
      drupal_set_message(new FormattableMarkup(Xss::filter($e
        ->getMessage()), []), 'error');
    }
  }

  /**
   * Gets the PDF engine implementation.
   *
   * @return \Drupal\entity_print\Plugin\PdfEngineInterface
   *   The PDF Engine implementation.
   */
  protected function getPdfEngine() {
    if (!isset($this->pdfEngine)) {
      $this->pdfEngine = $this->pluginManager
        ->createInstance($this->entityPrintConfig
        ->get('pdf_engine'));
    }
    return $this->pdfEngine;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
PdfDownload::$accessManager protected property Access manager.
PdfDownload::$entityPrintConfig protected property Our custom configuration.
PdfDownload::$pdfBuilder protected property The PDF builder service.
PdfDownload::$pdfEngine protected property The PDF engine implementation.
PdfDownload::$pluginManager protected property The Entity Print plugin manager.
PdfDownload::access public function Checks object access. Overrides ActionInterface::access
PdfDownload::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
PdfDownload::execute public function Executes the plugin. Overrides ExecutableInterface::execute
PdfDownload::executeMultiple public function Executes the plugin for an array of objects. Overrides ActionBase::executeMultiple
PdfDownload::getPdfEngine protected function Gets the PDF engine implementation.
PdfDownload::sendResponse protected function Sends the response using a stream and catches any errors.
PdfDownload::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
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.