You are here

abstract class YamlFormExporterBase in YAML Form 8

Provides a base class for a results exporter.

Hierarchy

Expanded class hierarchy of YamlFormExporterBase

See also

\Drupal\yamlform\YamlFormExporterInterface

\Drupal\yamlform\YamlFormExporterManager

\Drupal\yamlform\YamlFormExporterManagerInterface

Plugin API

2 files declare their use of YamlFormExporterBase
DocumentBaseYamlFormExporter.php in src/Plugin/YamlFormExporter/DocumentBaseYamlFormExporter.php
TabularBaseYamlFormExporter.php in src/Plugin/YamlFormExporter/TabularBaseYamlFormExporter.php

File

src/YamlFormExporterBase.php, line 19

Namespace

Drupal\yamlform
View source
abstract class YamlFormExporterBase extends PluginBase implements YamlFormExporterInterface {

  /**
   * A logger instance.
   *
   * @var \Psr\Log\LoggerInterface
   */
  protected $logger;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Form submission storage.
   *
   * @var \Drupal\yamlform\YamlFormSubmissionStorageInterface
   */
  protected $entityStorage;

  /**
   * The form element manager.
   *
   * @var \Drupal\yamlform\YamlFormElementManagerInterface
   */
  protected $elementManager;

  /**
   * Constructs a Drupal\Component\Plugin\PluginBase object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Psr\Log\LoggerInterface $logger
   *   A logger instance.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\yamlform\YamlFormElementManagerInterface $element_manager
   *   The form element manager.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerInterface $logger, EntityTypeManagerInterface $entity_type_manager, YamlFormElementManagerInterface $element_manager) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this
      ->setConfiguration($configuration);
    $this->logger = $logger;
    $this->entityTypeManager = $entity_type_manager;
    $this->entityStorage = $entity_type_manager
      ->getStorage('yamlform_submission');
    $this->elementManager = $element_manager;
  }

  /**
   * {@inheritdoc}
   */
  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('yamlform'), $container
      ->get('entity_type.manager'), $container
      ->get('plugin.manager.yamlform.element'));
  }

  /**
   * {@inheritdoc}
   */
  public function label() {
    return $this->pluginDefinition['label'];
  }

  /**
   * {@inheritdoc}
   */
  public function description() {
    return $this->pluginDefinition['description'];
  }

  /**
   * {@inheritdoc}
   */
  public function getStatus() {
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function isArchive() {
    return $this->pluginDefinition['archive'];
  }

  /**
   * {@inheritdoc}
   */
  public function hasOptions() {
    return $this->pluginDefinition['options'];
  }

  /**
   * {@inheritdoc}
   */
  public function getConfiguration() {
    return $this->configuration;
  }

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    $this->configuration = $configuration + $this
      ->defaultConfiguration();
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function defaultConfiguration() {
    return [
      'yamlform' => NULL,
      'source_entity' => NULL,
    ];
  }

  /**
   * {@inheritdoc}
   */
  public function calculateDependencies() {
    return [];
  }

  /**
   * Get the form whose submissions are being exported.
   *
   * @return \Drupal\yamlform\YamlFormInterface
   *   A form.
   */
  protected function getYamlForm() {
    return $this->configuration['yamlform'];
  }

  /**
   * Get the form source entity whose submissions are being exported.
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *   A form's source entity.
   */
  protected function getSourceEntity() {
    return $this->configuration['source_entity'];
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
  }

  /**
   * {@inheritdoc}
   */
  public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
  }

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

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

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

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

  /**
   * {@inheritdoc}
   */
  public function writeSubmission(YamlFormSubmissionInterface $yamlform_submission) {
  }

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

  /**
   * {@inheritdoc}
   */
  public function getFileTempDirectory() {
    return file_directory_temp();
  }

  /**
   * {@inheritdoc}
   */
  public function getSubmissionBaseName(YamlFormSubmissionInterface $yamlform_submission) {
    $export_options = $this
      ->getConfiguration();
    $file_name = $export_options['file_name'];
    $token_data = [
      'yamlform' => $yamlform_submission
        ->getYamlForm(),
      'yamlform_submission' => $yamlform_submission,
    ];
    $token_options = [
      'clear' => TRUE,
    ];
    $file_name = \Drupal::token()
      ->replace($file_name, $token_data, $token_options);

    // Sanitize file name.
    // @see http://stackoverflow.com/questions/2021624/string-sanitizer-for-filename
    $file_name = preg_replace('([^\\w\\s\\d\\-_~,;:\\[\\]\\(\\].]|[\\.]{2,})', '', $file_name);
    $file_name = preg_replace('/\\s+/', '-', $file_name);
    return $file_name;
  }

  /**
   * {@inheritdoc}
   */
  public function getFileExtension() {
    return 'txt';
  }

  /**
   * {@inheritdoc}
   */
  public function getBaseFileName() {
    $yamlform = $this
      ->getYamlForm();
    $source_entity = $this
      ->getSourceEntity();
    if ($source_entity) {
      return $yamlform
        ->id() . '.' . $source_entity
        ->getEntityTypeId() . '.' . $source_entity
        ->id();
    }
    else {
      return $yamlform
        ->id();
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getExportFileName() {
    return $this
      ->getBaseFileName() . '.' . $this
      ->getFileExtension();
  }

  /**
   * {@inheritdoc}
   */
  public function getExportFilePath() {
    return $this
      ->getFileTempDirectory() . '/' . $this
      ->getExportFileName();
  }

  /**
   * {@inheritdoc}
   */
  public function getArchiveFilePath() {
    return $this
      ->getFileTempDirectory() . '/' . $this
      ->getArchiveFileName();
  }

  /**
   * {@inheritdoc}
   */
  public function getArchiveFileName() {
    return $this
      ->getBaseFileName() . '.tar.gz';
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
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 3
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.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.
YamlFormExporterBase::$elementManager protected property The form element manager.
YamlFormExporterBase::$entityStorage protected property Form submission storage.
YamlFormExporterBase::$entityTypeManager protected property The entity type manager.
YamlFormExporterBase::$logger protected property A logger instance.
YamlFormExporterBase::buildConfigurationForm public function Form constructor. Overrides PluginFormInterface::buildConfigurationForm 3
YamlFormExporterBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
YamlFormExporterBase::closeExport public function Close export. Overrides YamlFormExporterInterface::closeExport
YamlFormExporterBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
YamlFormExporterBase::createExport public function Create export. Overrides YamlFormExporterInterface::createExport
YamlFormExporterBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurablePluginInterface::defaultConfiguration 3
YamlFormExporterBase::description public function Returns the results exporter description. Overrides YamlFormExporterInterface::description
YamlFormExporterBase::getArchiveFileName public function Get archive file name for a form. Overrides YamlFormExporterInterface::getArchiveFileName
YamlFormExporterBase::getArchiveFilePath public function Get archive file name and path for a form. Overrides YamlFormExporterInterface::getArchiveFilePath
YamlFormExporterBase::getBaseFileName public function Get export base file name without an extension. Overrides YamlFormExporterInterface::getBaseFileName
YamlFormExporterBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurablePluginInterface::getConfiguration
YamlFormExporterBase::getExportFileName public function Get export file name. Overrides YamlFormExporterInterface::getExportFileName
YamlFormExporterBase::getExportFilePath public function Get export file path. Overrides YamlFormExporterInterface::getExportFilePath
YamlFormExporterBase::getFileExtension public function Get export file extension. Overrides YamlFormExporterInterface::getFileExtension 2
YamlFormExporterBase::getFileTempDirectory public function Get export file temp directory. Overrides YamlFormExporterInterface::getFileTempDirectory
YamlFormExporterBase::getSourceEntity protected function Get the form source entity whose submissions are being exported.
YamlFormExporterBase::getStatus public function Returns the results exporter status. Overrides YamlFormExporterInterface::getStatus
YamlFormExporterBase::getSubmissionBaseName public function Get form submission base file name. Overrides YamlFormExporterInterface::getSubmissionBaseName
YamlFormExporterBase::getYamlForm protected function Get the form whose submissions are being exported.
YamlFormExporterBase::hasOptions public function Determine if exporter has options. Overrides YamlFormExporterInterface::hasOptions
YamlFormExporterBase::isArchive public function Determine if exporter generates an archive. Overrides YamlFormExporterInterface::isArchive
YamlFormExporterBase::label public function Returns the results exporter label. Overrides YamlFormExporterInterface::label
YamlFormExporterBase::openExport public function Open export. Overrides YamlFormExporterInterface::openExport
YamlFormExporterBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurablePluginInterface::setConfiguration 1
YamlFormExporterBase::submitConfigurationForm public function Form submission handler. Overrides PluginFormInterface::submitConfigurationForm
YamlFormExporterBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
YamlFormExporterBase::writeFooter public function Write footer to export. Overrides YamlFormExporterInterface::writeFooter 1
YamlFormExporterBase::writeHeader public function Write header to export. Overrides YamlFormExporterInterface::writeHeader 2
YamlFormExporterBase::writeSubmission public function Write submission to export. Overrides YamlFormExporterInterface::writeSubmission 4
YamlFormExporterBase::__construct public function Constructs a Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct