Skip to content

Commit

Permalink
Image field export
Browse files Browse the repository at this point in the history
  • Loading branch information
taylor-steve committed Jun 22, 2023
1 parent 0a63662 commit e78a2f3
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,10 @@ mukurtu_export.csv_exporter.*:
multivalue_delimiter:
type: string
label: 'Multivalue delimiter'
field_file:
type: string
label: 'File field export handling'
field_image:
type: string
label: 'Image field export handling'

6 changes: 3 additions & 3 deletions modules/mukurtu_export/src/BatchExportExecutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ public static function package(&$context)
$context['sandbox']['deliverables'] = array_slice($context['sandbox']['deliverables'], $batchSize);

// Zip the files.
foreach ($filesBatch as $fileUri) {
$filePath = $fs->realpath($fileUri);
foreach ($filesBatch as $fileToZip) {
$filePath = $fs->realpath($fileToZip['uri']);
if ($filePath) {
$zip->addFile($filePath, basename($filePath));
$zip->addFile($filePath, $fileToZip['entryname'] ?? basename($filePath));
}
$context['sandbox']['packaged']++;
}
Expand Down
34 changes: 34 additions & 0 deletions modules/mukurtu_export/src/Entity/CsvExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
* "include_files",
* "entity_fields_export_list",
* "multivalue_delimiter",
* "field_file",
* "field_image",
* },
* handlers = {
* "access" = "Drupal\mukurtu_export\CsvExporterAccessController",
Expand Down Expand Up @@ -62,6 +64,8 @@ class CsvExporter extends ConfigEntityBase implements EntityOwnerInterface
protected $entity_fields_export_list;

protected $multivalue_delimiter;
protected $field_file;
protected $field_image;


/**
Expand All @@ -77,6 +81,14 @@ public function __construct(array $values, $entity_type) {
$this->setMultivalueDelimiter('||');
}

if (!$this->getFileFieldSetting()) {
$this->setFileFieldSetting('id');
}

if (!$this->getImageFieldSetting()) {
$this->setImageFieldSetting('id');
}


}

Expand Down Expand Up @@ -119,6 +131,28 @@ public function setIncludeFiles(bool $include_files) {
return $this;
}

public function getFileFieldSetting()
{
return $this->field_file;
}

public function setFileFieldSetting(string $file_field_option)
{
$this->field_file = $file_field_option;
return $this;
}

public function getImageFieldSetting()
{
return $this->field_image;
}

public function setImageFieldSetting(string $image_field_option)
{
$this->field_image = $image_field_option;
return $this;
}

public function getDescription() {
return $this->description;
}
Expand Down
4 changes: 4 additions & 0 deletions modules/mukurtu_export/src/Event/EntityFieldExportEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,8 @@ public function setValue($value)
return $this;
}

public function packageFile($uri, $entryname) {
$this->context['results']['deliverables']['files'][] = ['uri' => $uri, 'entryname' => $entryname];
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\mukurtu_export\Event\EntityFieldExportEvent;
use Drupal\mukurtu_export\Entity\CsvExporter;

class CsvEntityFieldExportEventSubscriber implements EventSubscriberInterface
{
Expand All @@ -25,10 +26,17 @@ public function exportField(EntityFieldExportEvent $event)
}
$entity = $event->entity;
$field_name = $event->field_name;
/** @var \Drupal\mukurtu_export\Entity\CsvExporter $config */
$config = \Drupal::entityTypeManager()->getStorage('csv_exporter')->load($event->context['results']['config_id']);

$field = $entity->get($field_name);
$fieldType = $field->getFieldDefinition()->getType() ?? NULL;

if ($fieldType == 'image') {
return $this->exportImage($event, $field, $config);
}

// Default handling.
$values = $entity->get($field_name)->getValue();
$exportValue = [];
foreach ($values as $value) {
Expand All @@ -37,4 +45,31 @@ public function exportField(EntityFieldExportEvent $event)
$event->setValue($exportValue);
}

protected function exportImage(EntityFieldExportEvent $event, $field, CsvExporter $config) {
$idSetting = $config->getImageFieldSetting();
$export = [];

foreach ($field->getValue() as $value) {
if ($fid = ($value['target_id'] ?? NULL)) {
if ($idSetting == 'id') {
$export[] = $fid;
continue;
}

// Export path and package binary file.
$export[] = $this->packageFile($event, $fid);
}
}
$event->setValue($export);
}

protected function packageFile(EntityFieldExportEvent $event, $fid): string|null {
if ($file = \Drupal::entityTypeManager()->getStorage('file')->load($fid)) {
$packagedFilePath = sprintf("%s/%s/%s", "files", $fid, $file->getFilename());
$event->packageFile($file->getFileUri(), $packagedFilePath);
return $packagedFilePath;
}
return NULL;
}

}
55 changes: 47 additions & 8 deletions modules/mukurtu_export/src/Form/CsvExporterFormBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,6 @@ public function buildForm(array $form, FormStateInterface $form_state)
'#default_value' => $entity->getDescription(),
];

$form['include_files'] = [
'#type' => 'checkbox',
'#title' => $this->t('Include files in export package'),
'#description' => $this->t('If enabled, the binary files referenced by file fields will be included in the export package.'),
'#default_value' => $entity->getIncludeFiles(),
];

$form['csv'] = [
'#type' => 'details',
'#open' => TRUE,
Expand All @@ -104,6 +97,50 @@ public function buildForm(array $form, FormStateInterface $form_state)

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

$form['field_type_specific'] = [
'#type' => 'details',
'#open' => TRUE,
'#title' => $this->t("Field Type Settings")
];

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

$form['field_type_specific']['file']['include_files'] = [
'#type' => 'checkbox',
'#title' => $this->t('Include files in export package'),
'#description' => $this->t('If enabled, the binary files referenced by file fields will be included in the export package.'),
'#default_value' => $entity->getIncludeFiles(),
];
$form['field_type_specific']['file']['field_file'] = [
'#type' => 'radios',
'#title' => $this->t('File export handling'),
'#default_value' => $entity->getFileFieldSetting(),
'#options' => [
'id' => $this->t('Export identifier (file ID or UUID) only'),
'path_with_binary' => $this->t('Include the binary file in the export package and export the relative path to the file.')
],
];

$form['field_type_specific']['image'] = [
'#type' => 'details',
'#open' => TRUE,
'#title' => $this->t("Image")
];

$form['field_type_specific']['image']['field_image'] = [
'#type' => 'radios',
'#title' => $this->t('Image export handling'),
'#default_value' => $entity->getImageFieldSetting(),
'#options' => [
'id' => $this->t('Export identifier (file ID or UUID) only'),
'path_with_binary' => $this->t('Include the binary file in the export package and export the relative path to the file.')
],
];

return $form;
}

Expand All @@ -112,7 +149,8 @@ protected function buildEntityFieldMapping() {
$entity = $this->entity;

$build = [
'#type' => 'fieldset',
'#type' => 'details',
'#open' => TRUE,
'#title' => $this->t("Field Mappings")
];

Expand Down Expand Up @@ -260,6 +298,7 @@ public function save(array $form, FormStateInterface $form_state)
}

$entity->setMultivalueDelimiter($form_state->getValue('multivalue_delimiter'));
$entity->setImageFieldSetting($form_state->getValue('field_image'));
$entity->set('entity_fields_export_list', $field_list);
$status = $entity->save();
$form_state->setRedirect('mukurtu_export.export_settings');
Expand Down
2 changes: 1 addition & 1 deletion modules/mukurtu_export/src/Plugin/MukurtuExporter/CSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ public static function getOutputFile($entity_type_id, $bundle, &$context) {
$context['sandbox']['batch']['output_files'][$entity_type_id][$bundle] = $output;

// Add file as deliverable.
$context['results']['deliverables']['metadata'][] = $filepath;
$context['results']['deliverables']['metadata'][] = ['uri' => $filepath, 'entryname' => basename($filepath)];

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

0 comments on commit e78a2f3

Please sign in to comment.