Skip to content

Commit

Permalink
Add option to export refs by UUID
Browse files Browse the repository at this point in the history
  • Loading branch information
taylor-steve committed Jun 26, 2023
1 parent df98784 commit 6d17f5e
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 30 deletions.
4 changes: 3 additions & 1 deletion modules/mukurtu_export/mukurtu_export.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ services:
parent: default_plugin_manager
mukurtu_export.csv_field_export_event_subscriber:
class: Drupal\mukurtu_export\EventSubscriber\CsvEntityFieldExportEventSubscriber
arguments: ['@messenger']
arguments:
- '@messenger'
- '@entity_type.manager'
tags:
- { name: event_subscriber }
18 changes: 18 additions & 0 deletions modules/mukurtu_export/src/Entity/CsvExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* "escape",
* "eol",
* "multivalue_delimiter",
* "field_id",
* "field_file",
* "field_image",
* "entity_reference_node",
Expand Down Expand Up @@ -74,6 +75,7 @@ class CsvExporter extends ConfigEntityBase implements EntityOwnerInterface
protected $escape;
protected $eol;
protected $multivalue_delimiter;
protected $field_id;
protected $field_file;
protected $field_image;
protected $entity_reference_node;
Expand Down Expand Up @@ -113,6 +115,10 @@ public function __construct(array $values, $entity_type) {
$this->setMultivalueDelimiter('||');
}

if (!$this->getIdFieldSetting()) {
$this->setIdFieldSetting('id');
}

if (!$this->getFileFieldSetting()) {
$this->setFileFieldSetting('id');
}
Expand Down Expand Up @@ -158,6 +164,18 @@ public function setOwnerId($uid) {
return $this;
}


public function getIdFieldSetting()
{
return $this->field_id;
}

public function setIdFieldSetting(string $id_field_option)
{
$this->field_id = $id_field_option;
return $this;
}

public function getFileFieldSetting()
{
return $this->field_file;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Drupal\mukurtu_export\Event\EntityFieldExportEvent;
use Drupal\mukurtu_export\Entity\CsvExporter;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Messenger\MessengerInterface;

class CsvEntityFieldExportEventSubscriber implements EventSubscriberInterface
{

protected $messenger;
protected $entityTypeManager;

public function __construct(MessengerInterface $messenger, EntityTypeManagerInterface $entity_type_manager) {
$this->messenger = $messenger;
$this->entityTypeManager = $entity_type_manager;
}

/**
* {@inheritdoc}
*/
Expand All @@ -28,7 +38,7 @@ 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']);
$config = $this->entityTypeManager->getStorage('csv_exporter')->load($event->context['results']['config_id']);

$field = $entity->get($field_name);
$fieldType = $field->getFieldDefinition()->getType() ?? NULL;
Expand Down Expand Up @@ -58,34 +68,41 @@ public function exportField(EntityFieldExportEvent $event)
$event->setValue($exportValue);
}

protected function getUUID($entity_type_id, $id) {
if($entity = $this->entityTypeManager->getStorage($entity_type_id)->load($id)) {
return $entity->uuid();
}
return "{$entity_type_id}:{$id}";
}
protected function exportEntityReference(EntityFieldExportEvent $event, $field, CsvExporter $config) {
$export = [];
$target_type = $field->getFieldDefinition()->getSettings()['target_type'] ?? NULL;
$option = $config->getEntityReferenceSetting($target_type);
$id_format = $config->getIdFieldSetting();

foreach ($field->getValue() as $value) {
if ($id = ($value['target_id'] ?? NULL)) {
if ($option && $target_type) {
if ($option == 'id') {
$export[] = $id;
$export[] = $id_format === 'uuid' ? $this->getUUID($target_type, $id) : $id;
continue;
}

if ($option == 'entity') {
$this->exportEntityById($event, $target_type, $id);
$export[] = $id;
$export[] = $id_format === 'uuid' ? $this->getUUID($target_type, $id) : $id;
continue;
}

if ($target_type == 'user' && $option == 'username') {
if ($user = \Drupal::entityTypeManager()->getStorage($target_type)->load($id)) {
if ($user = $this->entityTypeManager->getStorage($target_type)->load($id)) {
/** @var \Drupal\user\UserInterface $user */
$export[] = $user->getAccountName();
}
}

if ($target_type == 'taxonomy_term' && $option == 'name') {
if ($term = \Drupal::entityTypeManager()->getStorage($target_type)->load($id)) {
if ($term = $this->entityTypeManager->getStorage($target_type)->load($id)) {
/** @var \Drupal\taxonomy\TermInterface $term */
$export[] = $term->getName();
}
Expand All @@ -100,9 +117,15 @@ protected function exportEntityReference(EntityFieldExportEvent $event, $field,

protected function exportCulturalProtocol(EntityFieldExportEvent $event, $field, CsvExporter $config) {
$export = [];
$id_format = $config->getIdFieldSetting();

foreach ($field->getValue() as $value) {
$protocols = str_replace('|', '', $value['protocols']);
if ($id_format === 'uuid') {
$ids = explode(',', $protocols);
$uuids = array_map(fn($p) => $this->getUUID('protocol', $p), $ids);
$protocols = implode(',', $uuids);
}
$export[] = "{$value['sharing_setting']}({$protocols})";
}
$event->setValue($export);
Expand Down Expand Up @@ -132,7 +155,7 @@ protected function exportFile(EntityFieldExportEvent $event, $field, CsvExporter
}

// Default.
$export[] = $fid;
$export[] = $config->getIdFieldSetting() === 'uuid' ? $this->getUUID('file', $fid) : $fid;
}
}
$event->setValue($export);
Expand Down Expand Up @@ -160,22 +183,22 @@ protected function exportImage(EntityFieldExportEvent $event, $field, CsvExporte
}

// Default.
$export[] = $fid;
$export[] = $config->getIdFieldSetting() === 'uuid' ? $this->getUUID('file', $fid) : $fid;
}
}
$event->setValue($export);
}

protected function exportEntityById(EntityFieldExportEvent $event, $entity_type_id, $id): EntityInterface|null {
if ($entity = \Drupal::entityTypeManager()->getStorage($entity_type_id)->load($id)) {
if ($entity = $this->entityTypeManager->getStorage($entity_type_id)->load($id)) {
$event->exportAdditionalEntity($entity);
return $entity;
}
return NULL;
}

protected function packageFile(EntityFieldExportEvent $event, $fid): string|null {
if ($file = \Drupal::entityTypeManager()->getStorage('file')->load($fid)) {
if ($file = $this->entityTypeManager->getStorage('file')->load($fid)) {
$packagedFilePath = sprintf("%s/%s/%s", "files", $fid, $file->getFilename());
$event->packageFile($file->getFileUri(), $packagedFilePath);
return $packagedFilePath;
Expand Down
51 changes: 31 additions & 20 deletions modules/mukurtu_export/src/Form/CsvExporterFormBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,46 +88,57 @@ public function buildForm(array $form, FormStateInterface $form_state)
'#title' => $this->t("Field Type Settings")
];

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

$form['field_type_specific']['file']['field_file'] = [
$form['field_type_specific']['entity_reference']['field_id'] = [
'#type' => 'radios',
'#title' => $this->t('File export handling'),
'#default_value' => $entity->getFileFieldSetting(),
'#title' => $this->t('Select the identifier format to export'),
'#default_value' => $entity->getIdFieldSetting(),
'#options' => [
'id' => $this->t('Export the identifier (file ID or UUID)'),
'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'),
'id' => $this->t('Export the ID'),
'uuid' => $this->t('Export the UUID'),
],
'id' => ['#description' => $this->t("Exporting identifiers as IDs is suitable for importing the data back into the same site.")],
'uuid' => ['#description' => $this->t("Exporting identifiers as UUIDs is preferable for sharing data for use on other sites. ")],
];

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

$form['field_type_specific']['image']['field_image'] = [
$form['field_type_specific']['entity_reference']['field_image'] = [
'#type' => 'radios',
'#title' => $this->t('Image export handling'),
'#title' => $this->t('Images'),
'#default_value' => $entity->getImageFieldSetting(),
'#options' => [
'id' => $this->t('Export the identifier (image ID or UUID)'),
'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'),
],
];

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

$form['field_type_specific']['entity_reference']['field_file'] = [
'#type' => 'radios',
'#title' => $this->t('Files'),
'#default_value' => $entity->getFileFieldSetting(),
'#options' => [
'id' => $this->t('Export the identifier (file ID or UUID)'),
'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'),
],
];

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

$form['field_type_specific']['entity_reference']['entity_reference_node'] = [
'#type' => 'radios',
'#title' => $this->t('Content'),
Expand Down Expand Up @@ -183,7 +194,7 @@ public function buildForm(array $form, FormStateInterface $form_state)
$form['csv'] = [
'#type' => 'details',
'#open' => FALSE,
'#title' => $this->t("CSV File Settings")
'#title' => $this->t("CSV File Format Settings")
];

$form['csv']['separator'] = [
Expand Down

0 comments on commit 6d17f5e

Please sign in to comment.