EntityReferenceImageFieldSerializer.php in Acquia Content Hub 8.2
File
src/EventSubscriber/SerializeContentField/EntityReferenceImageFieldSerializer.php
View source
<?php
namespace Drupal\acquia_contenthub\EventSubscriber\SerializeContentField;
use Drupal\acquia_contenthub\AcquiaContentHubEvents;
use Drupal\acquia_contenthub\Event\SerializeCdfEntityFieldEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class EntityReferenceImageFieldSerializer extends EntityReferenceFieldSerializer implements EventSubscriberInterface {
protected $fieldTypes = [
'image',
];
public static function getSubscribedEvents() {
$events[AcquiaContentHubEvents::SERIALIZE_CONTENT_ENTITY_FIELD][] = [
'onSerializeContentField',
98,
];
return $events;
}
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);
}
}
}