You are here

class WebformEntityPrintWebformExporter in Webform 6.x

Same name and namespace in other branches
  1. 8.5 modules/webform_entity_print/src/Plugin/WebformExporter/WebformEntityPrintWebformExporter.php \Drupal\webform_entity_print\Plugin\WebformExporter\WebformEntityPrintWebformExporter

Defines a Webform Entity Print PDF exporter.

Plugin annotation


@WebformExporter(
  id = "webform_entity_print",
  archive = TRUE,
  options = FALSE,
  deriver = "Drupal\webform_entity_print\Plugin\Derivative\WebformEntityPrintWebformExporterDeriver",
)

Hierarchy

Expanded class hierarchy of WebformEntityPrintWebformExporter

File

modules/webform_entity_print/src/Plugin/WebformExporter/WebformEntityPrintWebformExporter.php, line 20

Namespace

Drupal\webform_entity_print\Plugin\WebformExporter
View source
class WebformEntityPrintWebformExporter extends DocumentBaseWebformExporter {

  /**
   * The current request.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * The plugin manager for our Print engines.
   *
   * @var \Drupal\entity_print\Plugin\EntityPrintPluginManagerInterface
   */
  protected $printEngineManager;

  /**
   * The export type manager.
   *
   * @var \Drupal\entity_print\Plugin\ExportTypeManagerInterface
   */
  protected $exportTypeManager;

  /**
   * The file system service.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;

  /**
   * The Print builder.
   *
   * @var \Drupal\entity_print\PrintBuilderInterface
   */
  protected $printBuilder;

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return parent::defaultConfiguration() + [
      'view_mode' => 'html',
    ];
  }

  /**
   * {@inheritdoc}
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function writeSubmission(WebformSubmissionInterface $webform_submission) {
    $configuration = $this
      ->getConfiguration();

    // Make sure Webform Entity Print template is used.
    // @see webform_entity_print_entity_view_alter()
    $this->request->request
      ->set('_webform_entity_print', TRUE);

    // Set view mode.
    // @see \Drupal\webform\WebformSubmissionViewBuilder::view
    $this->request->request
      ->set('_webform_submissions_view_mode', $configuration['view_mode']);

    // Get print engine.
    $export_type_id = $this
      ->getExportTypeId();
    $print_engine = $this->printEngineManager
      ->createSelectedInstance($export_type_id);

    // Get scheme.
    $scheme = 'temporary';

    // Get file name.
    $file_extension = $this
      ->getExportTypeFileExtension();
    $file_name = $this
      ->getSubmissionBaseName($webform_submission) . '.' . $file_extension;

    // Save printable document.
    $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);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getBatchLimit() {

    // Limit batch document export to 10 submissions.
    return 10;
  }

  /****************************************************************************/

  // Export type methods.

  /****************************************************************************/

  /**
   * Get export type id.
   *
   * @return string
   *   The export type id.
   */
  protected function getExportTypeId() {
    return str_replace('webform_entity_print:', '', $this
      ->getPluginId());
  }

  /**
   * Get export type definition.
   *
   * @return array
   *   Export type definition.
   */
  protected function getExportTypeDefinition() {
    $export_type_id = $this
      ->getExportTypeId();
    return $this->exportTypeManager
      ->getDefinition($export_type_id);
  }

  /**
   * Get export type file extension.
   *
   * @return string
   *   Export type file extension.
   */
  protected function getExportTypeFileExtension() {
    $definition = $this
      ->getExportTypeDefinition();
    return $definition['file_extension'];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 98
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WebformEntityPrintWebformExporter::$exportTypeManager protected property The export type manager.
WebformEntityPrintWebformExporter::$fileSystem protected property The file system service.
WebformEntityPrintWebformExporter::$printBuilder protected property The Print builder.
WebformEntityPrintWebformExporter::$printEngineManager protected property The plugin manager for our Print engines.
WebformEntityPrintWebformExporter::$request protected property The current request.
WebformEntityPrintWebformExporter::buildConfigurationForm public function Form constructor. Overrides DocumentBaseWebformExporter::buildConfigurationForm
WebformEntityPrintWebformExporter::create public static function Creates an instance of the plugin. Overrides WebformExporterBase::create
WebformEntityPrintWebformExporter::defaultConfiguration public function Gets default configuration for this plugin. Overrides DocumentBaseWebformExporter::defaultConfiguration
WebformEntityPrintWebformExporter::getBatchLimit public function Get the number of submissions to be exported with each batch. Overrides WebformExporterBase::getBatchLimit
WebformEntityPrintWebformExporter::getExportTypeDefinition protected function Get export type definition.
WebformEntityPrintWebformExporter::getExportTypeFileExtension protected function Get export type file extension.
WebformEntityPrintWebformExporter::getExportTypeId protected function Get export type id.
WebformEntityPrintWebformExporter::writeSubmission public function Write submission to export. Overrides WebformExporterBase::writeSubmission
WebformEntityStorageTrait::$entityStorageToTypeMappings protected property An associate array of entity type storage aliases.
WebformEntityStorageTrait::$entityTypeManager protected property The entity type manager. 5
WebformEntityStorageTrait::getEntityStorage protected function Retrieves the entity storage.
WebformEntityStorageTrait::getSubmissionStorage protected function Retrieves the webform submission storage.
WebformEntityStorageTrait::getWebformStorage protected function Retrieves the webform storage.
WebformEntityStorageTrait::__get public function Implements the magic __get() method.
WebformExporterBase::$archive protected property Cached archive object.
WebformExporterBase::$configFactory protected property The configuration factory.
WebformExporterBase::$elementManager protected property The webform element manager.
WebformExporterBase::$logger protected property A logger instance.
WebformExporterBase::$tokenManager protected property The webform token manager.
WebformExporterBase::addToArchive public function Add file, directory, or content to exporter archive. Overrides WebformExporterInterface::addToArchive
WebformExporterBase::addToTarArchive protected function Add file, directory, or content to Tar archive.
WebformExporterBase::addToZipFile protected function Add file, directory, or content to ZIP file.
WebformExporterBase::closeExport public function Close export. Overrides WebformExporterInterface::closeExport
WebformExporterBase::createExport public function Create export. Overrides WebformExporterInterface::createExport
WebformExporterBase::description public function Returns the results exporter description. Overrides WebformExporterInterface::description
WebformExporterBase::getArchiveFileExtension public function Get archive file extension for a webform. Overrides WebformExporterInterface::getArchiveFileExtension
WebformExporterBase::getArchiveFileName public function Get archive file name for a webform. Overrides WebformExporterInterface::getArchiveFileName
WebformExporterBase::getArchiveFilePath public function Get archive file name and path for a webform. Overrides WebformExporterInterface::getArchiveFilePath
WebformExporterBase::getArchiveType public function Get archive file type. Overrides WebformExporterInterface::getArchiveType
WebformExporterBase::getBaseFileName public function Get export base file name without an extension. Overrides WebformExporterInterface::getBaseFileName
WebformExporterBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
WebformExporterBase::getExportFileName public function Get export file name. Overrides WebformExporterInterface::getExportFileName
WebformExporterBase::getExportFilePath public function Get export file path. Overrides WebformExporterInterface::getExportFilePath
WebformExporterBase::getFileExtension public function Get export file extension. Overrides WebformExporterInterface::getFileExtension 3
WebformExporterBase::getFileTempDirectory public function Get export file temp directory. Overrides WebformExporterInterface::getFileTempDirectory
WebformExporterBase::getSourceEntity protected function Get the webform source entity whose submissions are being exported.
WebformExporterBase::getStatus public function Returns the results exporter status. Overrides WebformExporterInterface::getStatus
WebformExporterBase::getSubmissionBaseName public function Get webform submission base file name. Overrides WebformExporterInterface::getSubmissionBaseName
WebformExporterBase::getWebform protected function Get the webform whose submissions are being exported.
WebformExporterBase::hasFiles public function Determine if exporter can include uploaded files (in a zipped archive). Overrides WebformExporterInterface::hasFiles
WebformExporterBase::hasOptions public function Determine if exporter has options. Overrides WebformExporterInterface::hasOptions
WebformExporterBase::isArchive public function Determine if exporter generates an archive. Overrides WebformExporterInterface::isArchive
WebformExporterBase::isExcluded public function Checks if the exporter is excluded via webform.settings. Overrides WebformExporterInterface::isExcluded
WebformExporterBase::label public function Returns the results exporter label. Overrides WebformExporterInterface::label
WebformExporterBase::openExport public function Open export. Overrides WebformExporterInterface::openExport
WebformExporterBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration 1
WebformExporterBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
WebformExporterBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
WebformExporterBase::writeFooter public function Write footer to export. Overrides WebformExporterInterface::writeFooter 1
WebformExporterBase::writeHeader public function Write header to export. Overrides WebformExporterInterface::writeHeader 3
WebformExporterInterface::ARCHIVE_TAR constant Tar archive.
WebformExporterInterface::ARCHIVE_ZIP constant ZIP file.