-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
198edb3
commit 8fe2243
Showing
4 changed files
with
121 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
62
modules/mukurtu_export/src/Event/EntityFieldExportEvent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
modules/mukurtu_export/src/EventSubscriber/CsvEntityFieldExportEventSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters