class TextItemFieldSerializer in Acquia Content Hub 8.2
Subscribes to entity field serialization to extract the filter format uuid.
Hierarchy
- class \Drupal\acquia_contenthub\EventSubscriber\SerializeContentField\TextItemFieldSerializer implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses ContentFieldMetadataTrait
Expanded class hierarchy of TextItemFieldSerializer
1 string reference to 'TextItemFieldSerializer'
1 service uses TextItemFieldSerializer
File
- src/
EventSubscriber/ SerializeContentField/ TextItemFieldSerializer.php, line 13
Namespace
Drupal\acquia_contenthub\EventSubscriber\SerializeContentFieldView source
class TextItemFieldSerializer implements EventSubscriberInterface {
use ContentFieldMetadataTrait;
/**
* Field types to serialize.
*
* @var array
*/
protected $fieldTypes = [
'text_with_summary',
'text',
'text_long',
];
/**
* The filter format entity storage handler.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $storage;
/**
* TextItemFieldSerializer constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*
* @throws \Exception
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->storage = $entity_type_manager
->getStorage('filter_format');
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[AcquiaContentHubEvents::SERIALIZE_CONTENT_ENTITY_FIELD][] = [
'onSerializeContentField',
100,
];
return $events;
}
/**
* Extract filter format UUID and place it into the serialized field values.
*
* @param \Drupal\acquia_contenthub\Event\SerializeCdfEntityFieldEvent $event
* The content entity field serialization event.
*/
public function onSerializeContentField(SerializeCdfEntityFieldEvent $event) {
if (!in_array($event
->getField()
->getFieldDefinition()
->getType(), $this->fieldTypes)) {
return;
}
$this
->setFieldMetaData($event);
$data = [];
$data['field_type'] = $event
->getField()
->getFieldDefinition()
->getType();
/** @var \Drupal\Core\Entity\TranslatableInterface $entity */
$entity = $event
->getEntity();
foreach ($entity
->getTranslationLanguages() as $langcode => $language) {
$field = $event
->getFieldTranslation($langcode);
foreach ($field as $item) {
$values = $item
->getValue();
if (!empty($values['format']) && ($format = $this->storage
->load($values['format']))) {
$values['format'] = $format
->uuid();
}
$data['value'][$langcode][] = $values;
}
}
$event
->setFieldData($data);
$event
->stopPropagation();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContentFieldMetadataTrait:: |
protected | function | Sets field metadata. | |
TextItemFieldSerializer:: |
protected | property | Field types to serialize. | |
TextItemFieldSerializer:: |
protected | property | The filter format entity storage handler. | |
TextItemFieldSerializer:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
TextItemFieldSerializer:: |
public | function | Extract filter format UUID and place it into the serialized field values. | |
TextItemFieldSerializer:: |
public | function | TextItemFieldSerializer constructor. |