View source
<?php
namespace Drupal\entity_print\Plugin\Action;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Component\Utility\Xss;
use Drupal\Core\Access\AccessManagerInterface;
use Drupal\Core\Action\ActionBase;
use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\entity_print\PdfBuilderInterface;
use Drupal\entity_print\PdfEngineException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\StreamedResponse;
class PdfDownload extends ActionBase implements ContainerFactoryPluginInterface {
protected $accessManager;
protected $pdfBuilder;
protected $pluginManager;
protected $entityPrintConfig;
protected $pdfEngine;
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;
}
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'));
}
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
$route_params = [
'entity_id' => $object
->id(),
'entity_type' => $object
->getEntityTypeId(),
];
return $this->accessManager
->checkNamedRoute('entity_print.view', $route_params, $account, $return_as_object);
}
public function execute($entity = NULL) {
$this
->sendResponse(function () use ($entity) {
$this->pdfBuilder
->getEntityRenderedAsPdf($entity, $this
->getPdfEngine(), TRUE);
});
}
public function executeMultiple(array $entities) {
$this
->sendResponse(function () use ($entities) {
$this->pdfBuilder
->getMultipleEntitiesRenderedAsPdf($entities, $this
->getPdfEngine(), TRUE);
});
}
protected function sendResponse(callable $callback) {
try {
(new StreamedResponse($callback))
->send();
} catch (PdfEngineException $e) {
drupal_set_message(new FormattableMarkup(Xss::filter($e
->getMessage()), []), 'error');
}
}
protected function getPdfEngine() {
if (!isset($this->pdfEngine)) {
$this->pdfEngine = $this->pluginManager
->createInstance($this->entityPrintConfig
->get('pdf_engine'));
}
return $this->pdfEngine;
}
}