You are here

class DelimitedYamlFormExporter in YAML Form 8

Defines a delimited text exporter.

Plugin annotation


@YamlFormExporter(
  id = "delimited",
  label = @Translation("Delimited text"),
  description = @Translation("Exports results as delimited text file."),
)

Hierarchy

Expanded class hierarchy of DelimitedYamlFormExporter

File

src/Plugin/YamlFormExporter/DelimitedYamlFormExporter.php, line 17

Namespace

Drupal\yamlform\Plugin\YamlFormExporter
View source
class DelimitedYamlFormExporter extends TabularBaseYamlFormExporter {

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

  /**
   * {@inheritdoc}
   */
  public function setConfiguration(array $configuration) {
    parent::setConfiguration($configuration);
    if ($this->configuration['delimiter'] == '\\t') {
      $this->configuration['delimiter'] = "\t";
    }
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    if (isset($form['delimiter'])) {
      return $form;
    }
    $states = [
      'visible' => [
        [
          ':input.js-yamlform-exporter' => [
            'value' => 'delimited',
          ],
        ],
      ],
    ];
    $form['warning'] = [
      '#type' => 'yamlform_message',
      '#message_type' => 'warning',
      '#message_message' => $this
        ->t('<strong>Warning:</strong> Opening delimited text files with spreadsheet applications may expose you to <a href=":href">formula injection</a> or other security vulnerabilities. When the submissions contain data from untrusted users and the downloaded file will be used with spreadsheets, use Microsoft Excel format.', [
        ':href' => 'https://www.google.com/search?q=spreadsheet+formula+injection',
      ]),
      '#states' => $states,
    ];
    $form['delimiter'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Delimiter text format'),
      '#description' => $this
        ->t('This is the delimiter used in the CSV/TSV file when downloading form results. Using tabs in the export is the most reliable method for preserving non-latin characters. You may want to change this to another character depending on the program with which you anticipate importing results.'),
      '#required' => TRUE,
      '#options' => [
        ',' => $this
          ->t('Comma (,)'),
        '\\t' => $this
          ->t('Tab (\\t)'),
        ';' => $this
          ->t('Semicolon (;)'),
        ':' => $this
          ->t('Colon (:)'),
        '|' => $this
          ->t('Pipe (|)'),
        '.' => $this
          ->t('Period (.)'),
        ' ' => $this
          ->t('Space ( )'),
      ],
      '#states' => $states,
      '#default_value' => $this->configuration['delimiter'],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function getFileExtension() {
    switch ($this->configuration['delimiter']) {
      case "\t":
        return 'tsv';
      default:
        return 'csv';
    }
  }

  /**
   * {@inheritdoc}
   */
  public function writeHeader() {
    $header = $this
      ->buildHeader();
    fputcsv($this->fileHandle, $header, $this->configuration['delimiter']);
  }

  /**
   * {@inheritdoc}
   */
  public function writeSubmission(YamlFormSubmissionInterface $yamlform_submission) {
    $record = $this
      ->buildRecord($yamlform_submission);
    fputcsv($this->fileHandle, $record, $this->configuration['delimiter']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DelimitedYamlFormExporter::buildConfigurationForm public function Form constructor. Overrides YamlFormExporterBase::buildConfigurationForm
DelimitedYamlFormExporter::defaultConfiguration public function Gets default configuration for this plugin. Overrides YamlFormExporterBase::defaultConfiguration
DelimitedYamlFormExporter::getFileExtension public function Get export file extension. Overrides YamlFormExporterBase::getFileExtension
DelimitedYamlFormExporter::setConfiguration public function Sets the configuration for this plugin instance. Overrides YamlFormExporterBase::setConfiguration
DelimitedYamlFormExporter::writeHeader public function Write header to export. Overrides YamlFormExporterBase::writeHeader
DelimitedYamlFormExporter::writeSubmission public function Write submission to export. Overrides YamlFormExporterBase::writeSubmission
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
FileHandleTraitYamlFormExporter::$fileHandle protected property A file handler resource.
FileHandleTraitYamlFormExporter::closeExport public function
FileHandleTraitYamlFormExporter::createExport public function
FileHandleTraitYamlFormExporter::openExport public function
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.
TabularBaseYamlFormExporter::$elements protected property An associative array containing form elements keyed by name.
TabularBaseYamlFormExporter::$fieldDefinitions protected property An associative array containing a form's field definitions.
TabularBaseYamlFormExporter::buildHeader protected function Build export header using form submission field definitions and form element columns.
TabularBaseYamlFormExporter::buildRecord protected function Build export record using a form submission.
TabularBaseYamlFormExporter::formatRecordFieldDefinitionValue protected function Get the field definition value from a form submission entity.
TabularBaseYamlFormExporter::getElements protected function Get form elements.
TabularBaseYamlFormExporter::getFieldDefinitions protected function Get a form's field definitions.
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::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
YamlFormExporterBase::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
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::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::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::__construct public function Constructs a Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct