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
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\Core\Action\ActionBase implements ActionInterface
- class \Drupal\entity_print\Plugin\Action\PdfDownload implements ContainerFactoryPluginInterface
- class \Drupal\Core\Action\ActionBase implements ActionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
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\ActionView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PdfDownload:: |
protected | property | Access manager. | |
PdfDownload:: |
protected | property | Our custom configuration. | |
PdfDownload:: |
protected | property | The PDF builder service. | |
PdfDownload:: |
protected | property | The PDF engine implementation. | |
PdfDownload:: |
protected | property | The Entity Print plugin manager. | |
PdfDownload:: |
public | function |
Checks object access. Overrides ActionInterface:: |
|
PdfDownload:: |
public static | function |
Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface:: |
|
PdfDownload:: |
public | function |
Executes the plugin. Overrides ExecutableInterface:: |
|
PdfDownload:: |
public | function |
Executes the plugin for an array of objects. Overrides ActionBase:: |
|
PdfDownload:: |
protected | function | Gets the PDF engine implementation. | |
PdfDownload:: |
protected | function | Sends the response using a stream and catches any errors. | |
PdfDownload:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
|
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |