EntityReferenceImageField.php in Acquia Content Hub 8.2
File
src/EventSubscriber/UnserializeContentField/EntityReferenceImageField.php
View source
<?php
namespace Drupal\acquia_contenthub\EventSubscriber\UnserializeContentField;
use Drupal\acquia_contenthub\AcquiaContentHubEvents;
use Drupal\acquia_contenthub\Event\UnserializeCdfEntityFieldEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class EntityReferenceImageField implements EventSubscriberInterface {
use FieldEntityDependencyTrait;
protected $fieldTypes = [
'image',
];
public static function getSubscribedEvents() {
$events[AcquiaContentHubEvents::UNSERIALIZE_CONTENT_ENTITY_FIELD] = [
'onUnserializeContentField',
8,
];
return $events;
}
public function onUnserializeContentField(UnserializeCdfEntityFieldEvent $event) {
$field = $event
->getField();
if (!in_array($event
->getFieldMetadata()['type'], $this->fieldTypes)) {
return;
}
$values = [];
if (!empty($field['value'])) {
foreach ($field['value'] as $langcode => $value) {
if (empty($value)) {
continue;
}
if (!is_array(reset($value))) {
$entity = $this
->getEntity($value['target_id'], $event);
$value['target_id'] = $entity
->id();
$values[$langcode][$event
->getFieldName()] = $value;
}
else {
foreach ($value as $delta => $item) {
$entity = $this
->getEntity($item['target_id'], $event);
$item['target_id'] = $entity
->id();
$values[$langcode][$event
->getFieldName()][] = $item;
}
}
}
}
$event
->setValue($values);
$event
->stopPropagation();
}
}