You are here

class EntityReferenceImageFieldSerializer in Acquia Content Hub 8.2

Subscribes to entity field serialization to handle entity references.

Hierarchy

  • class \Drupal\acquia_contenthub\EventSubscriber\SerializeContentField\EntityReferenceFieldSerializer implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
    • class \Drupal\acquia_contenthub\EventSubscriber\SerializeContentField\EntityReferenceImageFieldSerializer implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of EntityReferenceImageFieldSerializer

1 string reference to 'EntityReferenceImageFieldSerializer'
acquia_contenthub.services.yml in ./acquia_contenthub.services.yml
acquia_contenthub.services.yml
1 service uses EntityReferenceImageFieldSerializer
entity_reference_image.field.cdf.serializer in ./acquia_contenthub.services.yml
Drupal\acquia_contenthub\EventSubscriber\SerializeContentField\EntityReferenceImageFieldSerializer

File

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

Namespace

Drupal\acquia_contenthub\EventSubscriber\SerializeContentField
View source
class EntityReferenceImageFieldSerializer extends EntityReferenceFieldSerializer implements EventSubscriberInterface {

  /**
   * Use the image field type.
   *
   * @var array
   */
  protected $fieldTypes = [
    'image',
  ];

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[AcquiaContentHubEvents::SERIALIZE_CONTENT_ENTITY_FIELD][] = [
      'onSerializeContentField',
      98,
    ];
    return $events;
  }

  /**
   * Extract entity uuids as field values.
   *
   * @param \Drupal\acquia_contenthub\Event\SerializeCdfEntityFieldEvent $event
   *   The content entity field serialization event.
   *
   * @throws \Exception
   */
  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);
    }
  }

}

Members