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\ConfigurableActionBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\entity_print\Plugin\ExportTypeManagerInterface;
use Drupal\entity_print\PrintBuilderInterface;
use Drupal\entity_print\PrintEngineException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\StreamedResponse;
class PrintDownload extends ConfigurableActionBase implements ContainerFactoryPluginInterface {
protected $accessManager;
protected $printBuilder;
protected $entityPrintPluginManager;
protected $exportTypeManager;
protected $printEngine;
public function __construct(array $configuration, $plugin_id, $plugin_definition, AccessManagerInterface $access_manager, PrintBuilderInterface $print_builder, PluginManagerInterface $entity_print_plugin_manager, ExportTypeManagerInterface $export_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->accessManager = $access_manager;
$this->printBuilder = $print_builder;
$this->entityPrintPluginManager = $entity_print_plugin_manager;
$this->exportTypeManager = $export_type_manager;
}
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.print_builder'), $container
->get('plugin.manager.entity_print.print_engine'), $container
->get('plugin.manager.entity_print.export_type'));
}
public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
$route_params = [
'export_type' => $this->configuration['export_type'],
'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
->executeMultiple([
$entity,
]);
}
public function executeMultiple(array $entities) {
try {
(new StreamedResponse(function () use ($entities) {
$this->printBuilder
->deliverPrintable($entities, $this->entityPrintPluginManager
->createSelectedInstance($this->configuration['export_type']), TRUE);
}))
->send();
} catch (PrintEngineException $e) {
$this
->messenger()
->addError(new FormattableMarkup(Xss::filter($e
->getMessage()), []));
}
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['export_type'] = [
'#type' => 'select',
'#title' => $this
->t('Export type'),
'#options' => $this->exportTypeManager
->getFormOptions(),
'#required' => TRUE,
'#default_value' => !empty($this->configuration['export_type']) ? $this->configuration['export_type'] : NULL,
];
return $form;
}
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['export_type'] = $form_state
->getValue('export_type');
}
}