Skip to content

Commit

Permalink
Export the configured fields
Browse files Browse the repository at this point in the history
  • Loading branch information
taylor-steve committed Jun 21, 2023
1 parent cf80e50 commit 451c029
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
28 changes: 28 additions & 0 deletions modules/mukurtu_export/src/Entity/CsvExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,34 @@ public function getMappedFields($entity_type_id, $bundle) {
return $result;
}

public function getHeaders($entity_type_id, $bundle) {
$map = $this->get('entity_fields_export_list');
$key = "{$entity_type_id}__{$bundle}";
$headers = [];

// Mapped fields are in the order we want them already.
if (isset($map[$key]) && !empty($map[$key])) {
foreach ($map[$key] as $mapped_field_name => $mapped_field_label) {
$headers[$mapped_field_name] = $mapped_field_label;
}
}
return $headers;
}

public function getExportFields($entity_type_id, $bundle) {
$map = $this->get('entity_fields_export_list');
$key = "{$entity_type_id}__{$bundle}";
$fields = [];

// Mapped fields are in the order we want them already.
if (isset($map[$key]) && !empty($map[$key])) {
foreach ($map[$key] as $mapped_field_name => $mapped_field_label) {
$fields[] = $mapped_field_name;
}
}
return $fields;
}

public function getSupportedEntityTypes() {
return ['node', 'media', 'community', 'protocol', 'paragraph'];
}
Expand Down
20 changes: 11 additions & 9 deletions modules/mukurtu_export/src/Plugin/MukurtuExporter/CSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,8 @@ public static function exportSetup($entities, $options, &$context)
if (!$id) {
throw new Exception('Failed to load CSV exporter settings.');
}
/** @var \Drupal\mukurtu_export\Entity\CsvExporter $config */
$config = \Drupal::entityTypeManager()->getStorage('csv_exporter')->load($id);


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

// List of entities to export. We will "consume" these as we export. Empty means done.
Expand All @@ -175,9 +173,6 @@ public static function exportSetup($entities, $options, &$context)

// Track if we've written headers for a given file yet.
$context['results']['headers_written'] = [];
//$context['results']['headers'] = [];
$context['results']['headers'] = $context['results']['settings']['field_mapping'];
//$context['results']['headers']['media'] = $context['results']['settings']['field_mapping']['media'];

// Files paths for metadata to include in the package.
$context['results']['deliverables']['metadata'] = [];
Expand Down Expand Up @@ -226,6 +221,10 @@ public static function batchSetup(&$context)
{
$size = 10;

// Load the config entity.
/** @var \Drupal\mukurtu_export\Entity\CsvExporter $config */
$context['sandbox']['config'] = \Drupal::entityTypeManager()->getStorage('csv_exporter')->load($context['results']['config_id']);

// Identify the next batch of entities to export. We only get entities of a single type per batch.
$entity_types = array_keys($context['results']['entities']);
$entity_type_id = reset($entity_types);
Expand Down Expand Up @@ -255,7 +254,10 @@ public static function getOutputFile($entity_type_id, $bundle, &$context) {

// Check if we've written headers for this file.
if ($output && !isset($context['results']['headers_written'][$entity_type_id][$bundle])) {
fputcsv($output, $context['results']['headers'][$entity_type_id][$bundle]);

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

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

Expand Down Expand Up @@ -352,9 +354,9 @@ public static function export(EntityInterface $entity, &$context)

// Get mapping to determine what fields to export.
$multiValueDelimiter = '||';
$field_mapping = $context['results']['settings']['field_mapping'];

$export = [];
foreach ($field_mapping[$entity->getEntityTypeId()][$entity->bundle()] as $field_name => $label) {
foreach($context['sandbox']['config']->getExportFields($entity->getEntityTypeId(), $entity->bundle()) as $field_name) {
$event = new EntityFieldExportEvent('csv', $entity, $field_name, $context);
$event_dispatcher->dispatch($event, EntityFieldExportEvent::EVENT_NAME);
$exportValue = $event->getValue();
Expand Down

0 comments on commit 451c029

Please sign in to comment.