Skip to content

Commit

Permalink
Add CSV format options
Browse files Browse the repository at this point in the history
  • Loading branch information
taylor-steve committed Jun 23, 2023
1 parent 167df67 commit 0652a37
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ mukurtu_export.csv_exporter.*:
export_label:
type: string
label: 'Label'
separator:
type: string
label: 'Field delimiter character'
enclosure:
type: string
label: 'Field enclosure character'
escape:
type: string
label: 'Escape character'
eol:
type: string
label: 'End of line sequence'
multivalue_delimiter:
type: string
label: 'Multivalue delimiter'
Expand Down
57 changes: 57 additions & 0 deletions modules/mukurtu_export/src/Entity/CsvExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
* "description",
* "site_wide",
* "entity_fields_export_list",
* "separator",
* "enclosure",
* "escape",
* "eol",
* "multivalue_delimiter",
* "field_file",
* "field_image",
Expand Down Expand Up @@ -60,6 +64,10 @@ class CsvExporter extends ConfigEntityBase implements EntityOwnerInterface

protected $entity_fields_export_list;

protected $separator;
protected $enclosure;
protected $escape;
protected $eol;
protected $multivalue_delimiter;
protected $field_file;
protected $field_image;
Expand All @@ -74,6 +82,23 @@ public function __construct(array $values, $entity_type) {
$uid = $this->getOwnerId() ?? (\Drupal::currentUser()->id() ?? 1);
$this->setOwnerId($uid);


if (!$this->getSeparator()) {
$this->setSeparator(",");
}

if (!$this->getEnclosure()) {
$this->setEnclosure('"');
}

if (!$this->getEscape()) {
$this->setEscape('\\');
}

if (!$this->getEol()) {
$this->setEol('\n');
}

if (!$this->getMultivalueDelimiter()) {
$this->setMultivalueDelimiter('||');
}
Expand Down Expand Up @@ -154,6 +179,38 @@ public function isSiteWide() {
return $this->site_wide == TRUE;
}

public function getSeparator() {
return $this->separator;
}

public function setSeparator($separator) {
$this->separator = $separator;
}

public function getEnclosure() {
return $this->enclosure;
}

public function setEnclosure($enclosure) {
$this->enclosure = $enclosure;
}

public function getEscape() {
return $this->escape;
}

public function setEscape($escape) {
$this->escape = $escape;
}

public function getEol() {
return $this->eol;
}

public function setEol($eol) {
$this->eol = $eol;
}

public function getMultivalueDelimiter() {
return $this->multivalue_delimiter;
}
Expand Down
67 changes: 52 additions & 15 deletions modules/mukurtu_export/src/Form/CsvExporterFormBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,6 @@ public function buildForm(array $form, FormStateInterface $form_state)
'#default_value' => $entity->getDescription(),
];

$form['csv'] = [
'#type' => 'details',
'#open' => TRUE,
'#title' => $this->t("CSV File Settings")
];

$form['csv']['multivalue_delimiter'] = [
'#type' => 'textfield',
'#title' => $this->t('Multi-value Delimiter'),
'#maxlength' => 255,
'#size' => 5,
'#default_value' => $entity->getMultivalueDelimiter(),
'#required' => TRUE,
];

$form['entity_fields_export_list'] = $this->buildEntityFieldMapping();

$form['field_type_specific'] = [
Expand Down Expand Up @@ -137,6 +122,58 @@ public function buildForm(array $form, FormStateInterface $form_state)
],
];

$form['csv'] = [
'#type' => 'details',
'#open' => FALSE,
'#title' => $this->t("CSV File Settings")
];

$form['csv']['separator'] = [
'#type' => 'textfield',
'#title' => $this->t('Field delimiter'),
'#maxlength' => 2,
'#size' => 2,
'#default_value' => $entity->getSeparator(),
'#required' => TRUE,
];

$form['csv']['enclosure'] = [
'#type' => 'textfield',
'#title' => $this->t('Field enclosure'),
'#maxlength' => 2,
'#size' => 2,
'#default_value' => $entity->getEnclosure(),
'#required' => TRUE,
];

$form['csv']['escape'] = [
'#type' => 'textfield',
'#title' => $this->t('Escape character'),
'#maxlength' => 2,
'#size' => 2,
'#default_value' => $entity->getEscape(),
'#required' => FALSE,
];

/* // Only in PHP 8.1
$form['csv']['eol'] = [
'#type' => 'textfield',
'#title' => $this->t('End of line sequence'),
'#maxlength' => 255,
'#size' => 5,
'#default_value' => $entity->getEol(),
'#required' => TRUE,
]; */

$form['csv']['multivalue_delimiter'] = [
'#type' => 'textfield',
'#title' => $this->t('Multi-value delimiter'),
'#maxlength' => 255,
'#size' => 5,
'#default_value' => $entity->getMultivalueDelimiter(),
'#required' => TRUE,
];

return $form;
}

Expand Down
14 changes: 11 additions & 3 deletions modules/mukurtu_export/src/Plugin/MukurtuExporter/CSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,11 +147,19 @@ public static function exportSetup($entities, $options, &$context)
{
// Load the settings config entity.
$id = $options['settings']['settings_id'];
/** @var \Drupal\mukurtu_export\Entity\CsvExporter $config */
$config = \Drupal::entityTypeManager()->getStorage('csv_exporter')->load($id);
// @todo error handling.
if (!$id) {
if (!$config) {
throw new Exception('Failed to load CSV exporter settings.');
}

// CSV settings. These won't change from batch to batch.
$context['results']['csv']['separator'] = $config->getSeparator();
$context['results']['csv']['enclosure'] = $config->getEnclosure();
$context['results']['csv']['escape'] = $config->getEscape();
$context['results']['csv']['eol'] = $config->getEol();

$context['results']['config_id'] = $options['settings']['settings_id'];
$context['results']['uid'] = \Drupal::currentUser()->id();

Expand Down Expand Up @@ -257,7 +265,7 @@ public static function getOutputFile($entity_type_id, $bundle, &$context) {

$headers = $context['sandbox']['config']->getHeaders($entity_type_id, $bundle);

fputcsv($output, $headers);
fputcsv($output, $headers, $context['results']['csv']['separator'], $context['results']['csv']['enclosure'], $context['results']['csv']['escape']);
$context['results']['headers_written'][$entity_type_id][$bundle] = TRUE;
}

Expand Down Expand Up @@ -293,7 +301,7 @@ public static function batchExport(&$context)
$result = static::class::export($entity, $context);

// Write out.
fputcsv($output, $result);
fputcsv($output, $result, $context['results']['csv']['separator'], $context['results']['csv']['enclosure'], $context['results']['csv']['escape']);

// Record export of this entity.
$context['results']['exported_entities'][$entity_type_id][$id] = $id;
Expand Down

0 comments on commit 0652a37

Please sign in to comment.