You are here

public function LanguageFieldSerializer::onSerializeContentField in Acquia Content Hub 8.2

Directly reference the field's value as the language.

Parameters

\Drupal\acquia_contenthub\Event\SerializeCdfEntityFieldEvent $event: The content entity field serialization event.

File

src/EventSubscriber/SerializeContentField/LanguageFieldSerializer.php, line 31

Class

LanguageFieldSerializer
Subscribes to entity field serialization to handle language reference.

Namespace

Drupal\acquia_contenthub\EventSubscriber\SerializeContentField

Code

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();
}