Skip to content

Commit

Permalink
Use events to handle field exports
Browse files Browse the repository at this point in the history
  • Loading branch information
taylor-steve committed Jun 15, 2023
1 parent 198edb3 commit 8fe2243
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 2 deletions.
7 changes: 6 additions & 1 deletion modules/mukurtu_export/mukurtu_export.services.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
services:
plugin.manager.mukurtu_exporter:
class: Drupal\mukurtu_export\MukurtuExporterPluginManager
parent: default_plugin_manager
parent: default_plugin_manager
mukurtu_export.csv_field_export_event_subscriber:
class: Drupal\mukurtu_export\EventSubscriber\CsvEntityFieldExportEventSubscriber
arguments: ['@messenger']
tags:
- { name: event_subscriber }
62 changes: 62 additions & 0 deletions modules/mukurtu_export/src/Event/EntityFieldExportEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace Drupal\mukurtu_export\Event;

use Drupal\Component\EventDispatcher\Event;
use Drupal\Core\Entity\EntityInterface;

/**
* Event when an entity field is being exported.
*/
class EntityFieldExportEvent extends Event
{
const EVENT_NAME = 'mukurtu_export_entity_field_export';

/**
* The exporter plugin ID.
*
* @var string
*/
public $exporter_id;

/**
* The entity.
*
* @var \Drupal\Core\Entity\EntityInterface
*/
public $entity;

/**
* The field name.
*
* @var string
*/
public $field_name;

/**
* The field value to export.
*
* @var mixed
*/
protected $value;

public function __construct($exporter_id, EntityInterface $entity, $field_name)
{
$this->exporter_id = $exporter_id;
$this->entity = $entity;
$this->field_name = $field_name;
$this->value = [];
}

public function getValue()
{
return $this->value;
}

public function setValue($value)
{
$this->value = $value;
return $this;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Drupal\mukurtu_export\EventSubscriber;

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

class CsvEntityFieldExportEventSubscriber implements EventSubscriberInterface
{

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents()
{
return [
EntityFieldExportEvent::EVENT_NAME => ['exportField', -100],
];
}

public function exportField(EntityFieldExportEvent $event)
{
if ($event->exporter_id != 'csv') {
return;
}
$entity = $event->entity;
$field_name = $event->field_name;

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

$values = $entity->get($field_name)->getValue();
$exportValue = [];
foreach ($values as $value) {
$exportValue[] = is_array($value) ? reset($value) : $value;
}

$event->setValue($exportValue);
}

}
13 changes: 12 additions & 1 deletion modules/mukurtu_export/src/Plugin/MukurtuExporter/CSV.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\File\FileSystemInterface;
use Drupal\mukurtu_export\Event\EntityFieldExportEvent;
use Exception;

/**
Expand All @@ -32,6 +33,7 @@ public function defaultConfiguration()
'digital_heritage' => [
'title' => 'Title',
'field_description' => "Description",
'field_media_assets' => "Media Assets",
'nid' => 'ID',
'uuid' => 'UUID',
],
Expand Down Expand Up @@ -272,11 +274,20 @@ public static function batchCompleted(&$context)
*/
public static function export(EntityInterface $entity, &$context)
{
//$moduleHandler = \Drupal::moduleHandler();
$event_dispatcher = \Drupal::service('event_dispatcher');

// 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) {
$export[] = $entity->get($field_name)->getValue()[0]['value'] ?? "";
$event = new EntityFieldExportEvent('csv', $entity, $field_name);
$event_dispatcher->dispatch($event, EntityFieldExportEvent::EVENT_NAME);
$exportValue = $event->getValue();

//$moduleHandler->alter("exporter_csv_field_{$fieldType}", $exportValue, $entity, $field_name);
$export[] = implode($multiValueDelimiter, $exportValue);
}
return $export;
}
Expand Down

0 comments on commit 8fe2243

Please sign in to comment.