WebformEntityPrintWebformExporter.php in Webform 6.x
File
modules/webform_entity_print/src/Plugin/WebformExporter/WebformEntityPrintWebformExporter.php
View source
<?php
namespace Drupal\webform_entity_print\Plugin\WebformExporter;
use Drupal\Core\Form\FormStateInterface;
use Drupal\webform\Plugin\WebformExporter\DocumentBaseWebformExporter;
use Drupal\webform\WebformSubmissionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WebformEntityPrintWebformExporter extends DocumentBaseWebformExporter {
protected $request;
protected $printEngineManager;
protected $exportTypeManager;
protected $fileSystem;
protected $printBuilder;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->request = $container
->get('request_stack')
->getCurrentRequest();
$instance->printEngineManager = $container
->get('plugin.manager.entity_print.print_engine');
$instance->exportTypeManager = $container
->get('plugin.manager.entity_print.export_type');
$instance->printBuilder = $container
->get('entity_print.print_builder');
$instance->fileSystem = $container
->get('file_system');
return $instance;
}
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'];
}
}