class GeneralFieldSerializer in Acquia Content Hub 8.2
Subscribes to entity field serialization to handle basic field values.
Hierarchy
- class \Drupal\acquia_contenthub\EventSubscriber\SerializeContentField\GeneralFieldSerializer implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses ContentFieldMetadataTrait
Expanded class hierarchy of GeneralFieldSerializer
1 string reference to 'GeneralFieldSerializer'
1 service uses GeneralFieldSerializer
File
- src/
EventSubscriber/ SerializeContentField/ GeneralFieldSerializer.php, line 13
Namespace
Drupal\acquia_contenthub\EventSubscriber\SerializeContentFieldView source
class GeneralFieldSerializer implements EventSubscriberInterface {
use ContentFieldMetadataTrait;
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[AcquiaContentHubEvents::SERIALIZE_CONTENT_ENTITY_FIELD][] = [
'onSerializeContentField',
10,
];
return $events;
}
/**
* Directly reference the field's value property.
*
* @param \Drupal\acquia_contenthub\Event\SerializeCdfEntityFieldEvent $event
* The content entity field serialization event.
*/
public function onSerializeContentField(SerializeCdfEntityFieldEvent $event) {
$acceptable_types = [
'integer',
'boolean',
'string',
];
if (in_array($event
->getField()
->getFieldDefinition()
->getType(), $acceptable_types)) {
$this
->setFieldMetaData($event);
$data = [];
/** @var \Drupal\Core\Entity\TranslatableInterface $entity */
$entity = $event
->getEntity();
foreach ($entity
->getTranslationLanguages() as $langcode => $language) {
$field = $event
->getFieldTranslation($langcode);
// Set the translation value to represent null field data.
if (empty(count($field))) {
$data['value'][$langcode] = [];
continue;
}
foreach ($field as $item) {
if ($field
->getFieldDefinition()
->getFieldStorageDefinition()
->getCardinality() == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED) {
$data['value'][$langcode][] = $item
->getValue()['value'];
}
else {
$data['value'][$langcode] = $item
->getValue()['value'];
}
}
}
$event
->setFieldData($data);
$event
->stopPropagation();
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContentFieldMetadataTrait:: |
protected | function | Sets field metadata. | |
GeneralFieldSerializer:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
GeneralFieldSerializer:: |
public | function | Directly reference the field's value property. |