View source
<?php
namespace Drupal\webform_entity_print\Plugin\WebformExporter;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\entity_print\Plugin\ExportTypeManagerInterface;
use Drupal\entity_print\PrintBuilderInterface;
use Drupal\entity_print\Plugin\EntityPrintPluginManagerInterface;
use Drupal\webform\Plugin\WebformElementManagerInterface;
use Drupal\webform\Plugin\WebformExporter\DocumentBaseWebformExporter;
use Drupal\webform\WebformSubmissionInterface;
use Drupal\webform\WebformTokenManagerInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
class WebformEntityPrintWebformExporter extends DocumentBaseWebformExporter {
protected $request;
protected $printEngineManager;
protected $exportTypeManager;
protected $fileSystem;
protected $printBuilder;
public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerInterface $logger, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, WebformElementManagerInterface $element_manager, WebformTokenManagerInterface $token_manager, RequestStack $request_stack, EntityPrintPluginManagerInterface $print_engine_manager, ExportTypeManagerInterface $export_type_manager, PrintBuilderInterface $print_builder, FileSystemInterface $file_system) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $logger, $config_factory, $entity_type_manager, $element_manager, $token_manager);
$this->request = $request_stack
->getCurrentRequest();
$this->printEngineManager = $print_engine_manager;
$this->exportTypeManager = $export_type_manager;
$this->printBuilder = $print_builder;
$this->fileSystem = $file_system ?: \Drupal::service('file_system');
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('logger.factory')
->get('webform'), $container
->get('config.factory'), $container
->get('entity_type.manager'), $container
->get('plugin.manager.webform.element'), $container
->get('webform.token_manager'), $container
->get('request_stack'), $container
->get('plugin.manager.entity_print.print_engine'), $container
->get('plugin.manager.entity_print.export_type'), $container
->get('entity_print.print_builder'), $container
->get('file_system'));
}
public function defaultConfiguration() {
return parent::defaultConfiguration() + [
'view_mode' => 'html',
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form = parent::buildConfigurationForm($form, $form_state);
$form['view_mode'] = [
'#type' => 'select',
'#title' => $this
->t('View mode'),
'#options' => [
'html' => $this
->t('HTML'),
'table' => $this
->t('Table'),
],
'#required' => TRUE,
'#default_value' => $this->configuration['view_mode'],
];
return $form;
}
public function writeSubmission(WebformSubmissionInterface $webform_submission) {
$configuration = $this
->getConfiguration();
$this->request->request
->set('_webform_entity_print', TRUE);
$this->request->request
->set('_webform_submissions_view_mode', $configuration['view_mode']);
$export_type_id = $this
->getExportTypeId();
$print_engine = $this->printEngineManager
->createSelectedInstance($export_type_id);
$scheme = 'temporary';
$file_extension = $this
->getExportTypeFileExtension();
$file_name = $this
->getSubmissionBaseName($webform_submission) . '.' . $file_extension;
$temporary_file_path = $this->printBuilder
->savePrintable([
$webform_submission,
], $print_engine, $scheme, $file_name);
if ($temporary_file_path) {
$this
->addToArchive(file_get_contents($temporary_file_path), $file_name);
$this->fileSystem
->delete($temporary_file_path);
}
}
public function getBatchLimit() {
return 10;
}
protected function getExportTypeId() {
return str_replace('webform_entity_print:', '', $this
->getPluginId());
}
protected function getExportTypeDefinition() {
$export_type_id = $this
->getExportTypeId();
return $this->exportTypeManager
->getDefinition($export_type_id);
}
protected function getExportTypeFileExtension() {
$definition = $this
->getExportTypeDefinition();
return $definition['file_extension'];
}
}