diff --git a/src/Plugin/Derivative/SerializationWebformExporter.php b/src/Plugin/Derivative/SerializationWebformExporter.php new file mode 100644 index 00000000..26a18452 --- /dev/null +++ b/src/Plugin/Derivative/SerializationWebformExporter.php @@ -0,0 +1,77 @@ +getParameter('serializer.formats') + ); + } + + /** + * SerializationWebformExporter constructor. + * + * @param string $base_plugin_id + * Base plugin ID + * @param array $serializer_formats + * The available serialization formats. + */ + public function __construct($base_plugin_id, array $serializer_formats) { + $this->basePluginId = $base_plugin_id; + $this->serializerFormats = $serializer_formats; + } + + /** + * {@inheritdoc} + */ + public function getDerivativeDefinitions($base_plugin_definition) { + $this->derivatives = []; + + foreach ($this->serializerFormats as $format) { + $derivative_id = $this->basePluginId . '_' . $format; + $this->derivatives[$derivative_id] = [ + 'label' => $this->t('Serialize to @format', [ + '@format' => $format, + ]), + 'description' => $this->t('Serialize data into @format format.', [ + '@format' => $format, + ]), + 'serialization_format' => $format, + ] + $base_plugin_definition; + } + + return $this->derivatives; + } + +} diff --git a/src/Plugin/WebformExporter/SerializationWebformExporter.php b/src/Plugin/WebformExporter/SerializationWebformExporter.php new file mode 100644 index 00000000..ca56c736 --- /dev/null +++ b/src/Plugin/WebformExporter/SerializationWebformExporter.php @@ -0,0 +1,121 @@ +get('logger.factory')->get('webform'), + $container->get('entity_type.manager'), + $container->get('plugin.manager.webform.element'), + $container->get('serializer') + ); + } + + /** + * SerializationWebformExporter constructor. + * + * @param array $configuration + * @param string $plugin_id + * @param mixed $plugin_definition + * @param \Psr\Log\LoggerInterface $logger + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * @param \Drupal\webform\WebformElementManagerInterface $element_manager + * @param \Symfony\Component\Serializer\SerializerInterface $serializer + */ + public function __construct(array $configuration, $plugin_id, $plugin_definition, \Psr\Log\LoggerInterface $logger, \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager, \Drupal\webform\WebformElementManagerInterface $element_manager, SerializerInterface $serializer) { + parent::__construct($configuration, $plugin_id, $plugin_definition, $logger, $entity_type_manager, $element_manager); + + $this->serializer = $serializer; + } + + /** + * {@inheritdoc} + */ + public function writeSubmission(WebformSubmissionInterface $webform_submission) { + parent::writeSubmission($webform_submission); + + // We individually write each record as a CSV and then at the end convert it + // to the actually requested format since some formats require full dataset + // for serialization. + fputcsv($this->fileHandle, $this->buildRecord($webform_submission)); + } + + /** + * {@inheritdoc} + */ + public function writeFooter() { + $read_handler = fopen($this->getExportFilePath(), 'r'); + $data = []; + while ($row = fgetcsv($read_handler)) { + $data[] = array_combine($this->buildHeader(), $row); + } + fclose($read_handler); + + // Recreate the export file since we are now writing the data serialized in + // the requested format. + $this->createExport(); + + fwrite($this->fileHandle, $this->serializer->serialize($data, $this->getSerializationFormat())); + parent::writeFooter(); + } + + /** + * {@inheritdoc} + */ + public function getFileExtension() { + // TODO: is there any smarter way about determining the extension? + return $this->getSerializationFormat(); + } + + /** + * Retrieve serialization format to use in this exporter. + * + * @return string + * Serialization format to use in this exporter + */ + protected function getSerializationFormat() { + return $this->getPluginDefinition()['serialization_format']; + } + +}