You are here

class TableWebformExporter in Webform 6.x

Same name and namespace in other branches
  1. 8.5 src/Plugin/WebformExporter/TableWebformExporter.php \Drupal\webform\Plugin\WebformExporter\TableWebformExporter

Defines a HTML table exporter.

Plugin annotation


@WebformExporter(
  id = "table",
  label = @Translation("HTML Table"),
  description = @Translation("Exports results as an HTML table."),
)

Hierarchy

Expanded class hierarchy of TableWebformExporter

1 file declares its use of TableWebformExporter
TestWebformExporter.php in tests/modules/webform_test_exporter/src/Plugin/WebformExporter/TestWebformExporter.php

File

src/Plugin/WebformExporter/TableWebformExporter.php, line 17

Namespace

Drupal\webform\Plugin\WebformExporter
View source
class TableWebformExporter extends TabularBaseWebformExporter {

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

  /**
   * {@inheritdoc}
   */
  public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildConfigurationForm($form, $form_state);
    $form['excel'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Open HTML table in Excel'),
      '#description' => $this
        ->t('If checked, the download file extension will be change from .html to .xls.'),
      '#return_value' => TRUE,
      '#default_value' => $this->configuration['excel'],
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function writeHeader() {
    $header = $this
      ->buildHeader();
    $file_handle = $this->fileHandle;
    if ($this->configuration['source_entity']) {
      $title = $this->configuration['source_entity']
        ->label();
    }
    elseif ($this->configuration['webform']) {
      $title = $this->configuration['webform']
        ->label();
    }
    else {
      $title = '';
    }
    $thead = [];
    foreach ($header as $item) {
      $thead[] = '<th>' . htmlentities($item) . '</th>';
    }
    fwrite($file_handle, '<!doctype html>');
    fwrite($file_handle, '<html>');
    fwrite($file_handle, '<head>');

    // Force Excel to keep field values containing p- or br-tags within the same
    // cell.
    fwrite($file_handle, '<style>p, br {mso-data-placement:same-cell;}</style>');
    fwrite($file_handle, '<meta charset="utf-8">');
    if ($title) {
      fwrite($file_handle, '<title>' . $title . '</title>');
    }
    fwrite($file_handle, '</head>');
    fwrite($file_handle, '<body>');
    fwrite($file_handle, '<table border="1">');
    fwrite($file_handle, '<thead><tr bgcolor="#cccccc" valign="top">');
    fwrite($file_handle, implode(PHP_EOL, $thead));
    fwrite($file_handle, '</tr></thead>');
    fwrite($file_handle, '<tbody>');
  }

  /**
   * {@inheritdoc}
   */
  public function writeSubmission(WebformSubmissionInterface $webform_submission) {
    $record = $this
      ->buildRecord($webform_submission);
    $file_handle = $this->fileHandle;
    $row = [];
    foreach ($record as $item) {
      $row[] = '<td>' . nl2br(htmlentities($item)) . '</td>';
    }
    fwrite($file_handle, '<tr valign="top">');
    fwrite($file_handle, implode(PHP_EOL, $row));
    fwrite($file_handle, '</tr>');
  }

  /**
   * {@inheritdoc}
   */
  public function writeFooter() {
    $file_handle = $this->fileHandle;
    fwrite($file_handle, '</tbody>');
    fwrite($file_handle, '</table>');
    fwrite($file_handle, '</body>');
    fwrite($file_handle, '</html>');
  }

  /**
   * {@inheritdoc}
   */
  public function getFileExtension() {
    return $this->configuration['excel'] ? 'xls' : 'html';
  }

}

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
FileHandleTraitWebformExporter::$fileHandle protected property A file handler resource.
FileHandleTraitWebformExporter::closeExport public function
FileHandleTraitWebformExporter::createExport public function
FileHandleTraitWebformExporter::openExport public function
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.
TableWebformExporter::buildConfigurationForm public function Form constructor. Overrides WebformExporterBase::buildConfigurationForm 1
TableWebformExporter::defaultConfiguration public function Gets default configuration for this plugin. Overrides WebformExporterBase::defaultConfiguration 1
TableWebformExporter::getFileExtension public function Get export file extension. Overrides WebformExporterBase::getFileExtension
TableWebformExporter::writeFooter public function Write footer to export. Overrides WebformExporterBase::writeFooter
TableWebformExporter::writeHeader public function Write header to export. Overrides WebformExporterBase::writeHeader
TableWebformExporter::writeSubmission public function Write submission to export. Overrides WebformExporterBase::writeSubmission
TabularBaseWebformExporter::$dateFormatter protected property The date formatter service.
TabularBaseWebformExporter::$elements protected property An associative array containing webform elements keyed by name.
TabularBaseWebformExporter::$fieldDefinitions protected property An associative array containing a webform's field definitions.
TabularBaseWebformExporter::buildHeader protected function Build export header using webform submission field definitions and webform element columns. 1
TabularBaseWebformExporter::buildRecord protected function Build export record using a webform submission. 2
TabularBaseWebformExporter::create public static function Creates an instance of the plugin. Overrides WebformExporterBase::create
TabularBaseWebformExporter::formatRecordFieldDefinitionValue protected function Get the field definition value from a webform submission entity.
TabularBaseWebformExporter::getElements protected function Get webform elements.
TabularBaseWebformExporter::getFieldDefinitions protected function Get a webform's field definitions.
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::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::getBatchLimit public function Get the number of submissions to be exported with each batch. Overrides WebformExporterInterface::getBatchLimit 1
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::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::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
WebformExporterInterface::ARCHIVE_TAR constant Tar archive.
WebformExporterInterface::ARCHIVE_ZIP constant ZIP file.