class LanguageFieldSerializer in Acquia Content Hub 8.2
Subscribes to entity field serialization to handle language reference.
Hierarchy
- class \Drupal\acquia_contenthub\EventSubscriber\SerializeContentField\LanguageFieldSerializer implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses ContentFieldMetadataTrait
Expanded class hierarchy of LanguageFieldSerializer
1 string reference to 'LanguageFieldSerializer'
1 service uses LanguageFieldSerializer
File
- src/
EventSubscriber/ SerializeContentField/ LanguageFieldSerializer.php, line 12
Namespace
Drupal\acquia_contenthub\EventSubscriber\SerializeContentFieldView source
class LanguageFieldSerializer 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 as the language.
*
* @param \Drupal\acquia_contenthub\Event\SerializeCdfEntityFieldEvent $event
* The content entity field serialization event.
*/
public function onSerializeContentField(SerializeCdfEntityFieldEvent $event) {
if ('language' !== $event
->getField()
->getFieldDefinition()
->getType()) {
return;
}
$this
->setFieldMetaData($event);
$data = [];
/** @var \Drupal\Core\Entity\TranslatableInterface $entity */
$entity = $event
->getEntity();
foreach ($entity
->getTranslationLanguages() as $langcode => $language) {
$field = $event
->getFieldTranslation($langcode);
foreach ($field as $item) {
$data['value'][$langcode] = $item
->getValue()['value'];
}
}
$event
->setFieldData($data);
$event
->stopPropagation();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ContentFieldMetadataTrait:: |
protected | function | Sets field metadata. | |
LanguageFieldSerializer:: |
public static | function | Returns an array of event names this subscriber wants to listen to. | |
LanguageFieldSerializer:: |
public | function | Directly reference the field's value as the language. |