Skip to content

Commit

Permalink
Add support for geometry fields in CSV exports
Browse files Browse the repository at this point in the history
  • Loading branch information
mstenta committed Oct 10, 2024
1 parent 09e9479 commit 6462ed9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions modules/core/csv/farm_csv.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ services:
class: Drupal\farm_csv\Normalizer\FractionFieldItemNormalizer
tags:
- { name: normalizer, priority: 10 }
farm_csv.normalizer.geofield_item:
class: Drupal\farm_csv\Normalizer\GeofieldItemNormalizer
tags:
- { name: normalizer, priority: 10 }
farm_csv.normalizer.text_long_field_item:
class: Drupal\farm_csv\Normalizer\TextLongFieldItemNormalizer
tags:
Expand Down
39 changes: 39 additions & 0 deletions modules/core/csv/src/Normalizer/GeofieldItemNormalizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Drupal\farm_csv\Normalizer;

use Drupal\geofield\Plugin\Field\FieldType\GeofieldItem;
use Drupal\serialization\Normalizer\FieldItemNormalizer;

/**
* Normalizes Geofields for farmOS CSV exports.
*/
class GeofieldItemNormalizer extends FieldItemNormalizer {

/**
* The supported format.
*/
const FORMAT = 'csv';

/**
* {@inheritdoc}
*/
public function normalize($object, $format = NULL, array $context = []): array|string|int|float|bool|\ArrayObject|NULL {
$data = parent::normalize($object, $format, $context);

// Return the WKT value, if desired.
if (isset($context['wkt']) && $context['wkt'] === TRUE) {
return $data['value'];
}

return $data;
}

/**
* {@inheritdoc}
*/
public function supportsNormalization($data, ?string $format = NULL, array $context = []): bool {
return $data instanceof GeofieldItem && $format == static::FORMAT;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,9 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
// Return RFC3339 dates.
'rfc3339_dates' => TRUE,

// Return WKT geometry.
'wkt' => TRUE,

// CSV encoder settings.
'csv_settings' => [
'sanitize' => $form_state->getValue('sanitize'),
Expand Down Expand Up @@ -406,6 +409,7 @@ protected function getIncludeColumns(?string $bundle = NULL) {
'changed',
'entity_reference',
'fraction',
'geofield',
'list_string',
'state',
'string',
Expand Down

0 comments on commit 6462ed9

Please sign in to comment.