Skip to content

Commit

Permalink
File exports
Browse files Browse the repository at this point in the history
  • Loading branch information
taylor-steve committed Jun 23, 2023
1 parent 8ec9db2 commit 167df67
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 25 deletions.
12 changes: 0 additions & 12 deletions modules/mukurtu_export/src/Entity/CsvExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
* "uid",
* "description",
* "site_wide",
* "include_files",
* "entity_fields_export_list",
* "multivalue_delimiter",
* "field_file",
Expand Down Expand Up @@ -56,8 +55,6 @@ class CsvExporter extends ConfigEntityBase implements EntityOwnerInterface
{
protected $uid;

protected $include_files;

protected $description;
protected $site_wide;

Expand Down Expand Up @@ -122,15 +119,6 @@ public function setOwnerId($uid) {
return $this;
}

public function getIncludeFiles() {
return $this->include_files;
}

public function setIncludeFiles(bool $include_files) {
$this->include_files = $include_files;
return $this;
}

public function getFileFieldSetting()
{
return $this->field_file;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ public function exportField(EntityFieldExportEvent $event)
$field = $entity->get($field_name);
$fieldType = $field->getFieldDefinition()->getType() ?? NULL;

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

if ($fieldType == 'image') {
return $this->exportImage($event, $field, $config);
}
Expand All @@ -46,20 +50,50 @@ public function exportField(EntityFieldExportEvent $event)
$event->setValue($exportValue);
}


protected function exportFile(EntityFieldExportEvent $event, $field, CsvExporter $config)
{
$setting = $config->getFileFieldSetting();
$export = [];

foreach ($field->getValue() as $value) {
if ($fid = ($value['target_id'] ?? NULL)) {
// Export path and package binary file.
if ($setting == 'path_with_binary') {
$export[] = $this->packageFile($event, $fid);
continue;
}

// Export whole file entity.
if ($setting == 'file_entity') {
if ($this->exportEntityById($event, 'file', $fid)) {
$export[] = $fid;
$this->packageFile($event, $fid);
continue;
}
}

// Default.
$export[] = $fid;
}
}
$event->setValue($export);
}

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

foreach ($field->getValue() as $value) {
if ($fid = ($value['target_id'] ?? NULL)) {
// Export path and package binary file.
if ($idSetting == 'path_with_binary') {
if ($setting == 'path_with_binary') {
$export[] = $this->packageFile($event, $fid);
continue;
}

// Export whole file entity.
if ($idSetting == 'file_entity') {
if ($setting == 'file_entity') {
if($this->exportEntityById($event, 'file', $fid)) {
$export[] = $fid;
$this->packageFile($event, $fid);
Expand Down
15 changes: 5 additions & 10 deletions modules/mukurtu_export/src/Form/CsvExporterFormBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,14 @@ public function buildForm(array $form, FormStateInterface $form_state)
'#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.')
'id' => $this->t('Export the identifier (file ID or UUID) only'),
'path_with_binary' => $this->t('Package the binary file and export the relative path.'),
'file_entity' => $this->t('Package the binary file and export the referenced file entity'),
],
];

Expand All @@ -137,8 +132,8 @@ public function buildForm(array $form, FormStateInterface $form_state)
'#default_value' => $entity->getImageFieldSetting(),
'#options' => [
'id' => $this->t('Export the identifier (file ID or UUID) only'),
'path_with_binary' => $this->t('Package the binary file and export the relative path to the file.'),
'file_entity' => $this->t('Package the binary file and export the referenced file entity'),
'path_with_binary' => $this->t('Package the binary image file and export the relative path.'),
'file_entity' => $this->t('Package the binary image file and export the referenced image file entity'),
],
];

Expand Down

0 comments on commit 167df67

Please sign in to comment.