View source
<?php
namespace Drupal\webform\Plugin\WebformExporter;
use Drupal\webform\Plugin\WebformExporterBase;
use Drupal\webform\WebformSubmissionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class TabularBaseWebformExporter extends WebformExporterBase {
use FileHandleTraitWebformExporter;
protected $dateFormatter;
protected $elements;
protected $fieldDefinitions;
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$instance = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$instance->dateFormatter = $container
->get('date.formatter');
return $instance;
}
protected function buildHeader() {
$export_options = $this
->getConfiguration();
$this->fieldDefinitions = $this
->getFieldDefinitions();
$elements = $this
->getElements();
$header = [];
foreach ($this->fieldDefinitions as $field_definition) {
$element = [
'#type' => $field_definition['type'] === 'entity_reference' ? 'entity_autocomplete' : 'element',
'#admin_title' => '',
'#title' => (string) $field_definition['title'],
'#webform_key' => (string) $field_definition['name'],
];
$header = array_merge($header, $this->elementManager
->invokeMethod('buildExportHeader', $element, $export_options));
}
foreach ($elements as $element) {
$header = array_merge($header, $this->elementManager
->invokeMethod('buildExportHeader', $element, $export_options));
}
return $header;
}
protected function buildRecord(WebformSubmissionInterface $webform_submission) {
$export_options = $this
->getConfiguration();
$this->fieldDefinitions = $this
->getFieldDefinitions();
$elements = $this
->getElements();
$record = [];
foreach ($this->fieldDefinitions as $field_definition) {
$this
->formatRecordFieldDefinitionValue($record, $webform_submission, $field_definition);
}
foreach ($elements as $column_name => $element) {
$element['#webform_key'] = $column_name;
$record = array_merge($record, $this->elementManager
->invokeMethod('buildExportRecord', $element, $webform_submission, $export_options));
}
return $record;
}
protected function formatRecordFieldDefinitionValue(array &$record, WebformSubmissionInterface $webform_submission, array $field_definition) {
$export_options = $this
->getConfiguration();
$field_name = $field_definition['name'];
$field_type = $field_definition['type'];
switch ($field_type) {
case 'created':
case 'changed':
case 'timestamp':
if (!empty($webform_submission->{$field_name}->value)) {
$record[] = $this->dateFormatter
->format($webform_submission->{$field_name}->value, 'custom', 'Y-m-d H:i:s');
}
else {
$record[] = '';
}
break;
case 'entity_reference':
$element = [
'#type' => 'entity_autocomplete',
'#target_type' => $field_definition['target_type'],
'#value' => $webform_submission
->get($field_name)->target_id,
];
$record = array_merge($record, $this->elementManager
->invokeMethod('buildExportRecord', $element, $webform_submission, $export_options));
break;
case 'entity_url':
case 'entity_title':
$entity = $webform_submission
->getSourceEntity(TRUE);
if ($entity) {
$record[] = $field_type === 'entity_url' && $entity
->hasLinkTemplate('canonical') ? $entity
->toUrl()
->setOption('absolute', TRUE)
->toString() : $entity
->label();
}
else {
$record[] = '';
}
break;
default:
$record[] = $webform_submission
->get($field_name)->value;
break;
}
}
protected function getFieldDefinitions() {
if (isset($this->fieldDefinitions)) {
return $this->fieldDefinitions;
}
$export_options = $this
->getConfiguration();
$this->fieldDefinitions = $this
->getSubmissionStorage()
->getFieldDefinitions();
$this->fieldDefinitions = $this
->getSubmissionStorage()
->checkFieldDefinitionAccess($this
->getWebform(), $this->fieldDefinitions);
if ($export_options['excluded_columns']) {
$this->fieldDefinitions = array_diff_key($this->fieldDefinitions, $export_options['excluded_columns']);
}
if (isset($this->fieldDefinitions['entity_type']) && isset($this->fieldDefinitions['entity_id'])) {
$this->fieldDefinitions['entity_title'] = [
'name' => 'entity_title',
'title' => $this
->t('Submitted to: Entity title'),
'type' => 'entity_title',
];
$this->fieldDefinitions['entity_url'] = [
'name' => 'entity_url',
'title' => $this
->t('Submitted to: Entity URL'),
'type' => 'entity_url',
];
}
return $this->fieldDefinitions;
}
protected function getElements() {
if (isset($this->elements)) {
return $this->elements;
}
$export_options = $this
->getConfiguration();
$this->elements = $this
->getWebform()
->getElementsInitializedFlattenedAndHasValue('view');
$this->elements = $this->tokenManager
->replace($this->elements, $this
->getWebform());
if ($export_options['excluded_columns']) {
$this->elements = array_diff_key($this->elements, $export_options['excluded_columns']);
}
return $this->elements;
}
}