You are here

public function EntityReferenceImageFieldSerializer::onSerializeContentField in Acquia Content Hub 8.2

Extract entity uuids as field values.

Parameters

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

Throws

\Exception

Overrides EntityReferenceFieldSerializer::onSerializeContentField

File

src/EventSubscriber/SerializeContentField/EntityReferenceImageFieldSerializer.php, line 38

Class

EntityReferenceImageFieldSerializer
Subscribes to entity field serialization to handle entity references.

Namespace

Drupal\acquia_contenthub\EventSubscriber\SerializeContentField

Code

public function onSerializeContentField(SerializeCdfEntityFieldEvent $event) {
  if (in_array($event
    ->getField()
    ->getFieldDefinition()
    ->getType(), $this->fieldTypes)) {
    parent::onSerializeContentField($event);
    $values = $event
      ->getFieldData();
    if (!empty($values['value'])) {
      $field_name = $event
        ->getField()
        ->getName();
      $entity = $event
        ->getEntity();
      foreach ($values['value'] as $lang => $language_values) {
        $translation = $entity
          ->getTranslation($lang);
        $field_translation = $translation
          ->get($field_name);
        foreach ($language_values as $delta => $value) {
          $values['value'][$lang][$delta] = [
            'target_id' => $value,
          ];
          $values['value'][$lang][$delta] += $field_translation[$delta]
            ->getValue();
        }
      }
    }
    $event
      ->setFieldData($values);
  }
}