You are here

public function EntityReferenceImageField::onUnserializeContentField in Acquia Content Hub 8.2

Extracts the target storage and retrieves the referenced entity.

Parameters

\Drupal\acquia_contenthub\Event\UnserializeCdfEntityFieldEvent $event: The unserialize event.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/EventSubscriber/UnserializeContentField/EntityReferenceImageField.php, line 41

Class

EntityReferenceImageField
Entity/image/file field reference handling.

Namespace

Drupal\acquia_contenthub\EventSubscriber\UnserializeContentField

Code

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