Provide a TextLongFieldItemNormalizer with an option to return processed text.
This commit is contained in:
parent
9995efd994
commit
6ccf2cda0d
|
@ -4,6 +4,10 @@ services:
|
|||
tags:
|
||||
- { name: normalizer, priority: 10 }
|
||||
arguments: ['@entity_type.manager', '@entity_type.repository', '@entity_field.manager']
|
||||
farm_csv.normalizer.text_long_field_item:
|
||||
class: Drupal\farm_csv\Normalizer\TextLongFieldItemNormalizer
|
||||
tags:
|
||||
- { name: normalizer, priority: 10 }
|
||||
farm_csv.normalizer.entity_reference_field_item:
|
||||
class: Drupal\farm_csv\Normalizer\EntityReferenceFieldItemNormalizer
|
||||
tags:
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\farm_csv\Normalizer;
|
||||
|
||||
use Drupal\serialization\Normalizer\FieldItemNormalizer;
|
||||
use Drupal\text\Plugin\Field\FieldType\TextLongItem;
|
||||
|
||||
/**
|
||||
* Normalizes long text fields for farmOS CSV exports.
|
||||
*/
|
||||
class TextLongFieldItemNormalizer extends FieldItemNormalizer {
|
||||
|
||||
/**
|
||||
* The supported format.
|
||||
*/
|
||||
const FORMAT = 'csv';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function normalize($field_item, $format = NULL, array $context = []): array|string|int|float|bool|\ArrayObject|NULL {
|
||||
/** @var \Drupal\text\Plugin\Field\FieldType\TextLongItem $field_item */
|
||||
|
||||
// Return processed text, if desired.
|
||||
if (!empty($context['processed_text'])) {
|
||||
return $field_item->get('processed')->getValue();
|
||||
}
|
||||
|
||||
// Delegate to the parent method.
|
||||
return parent::normalize($field_item, $format, $context);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsNormalization($data, string $format = NULL, array $context = []): bool {
|
||||
return $data instanceof TextLongItem && $format == static::FORMAT;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue