You are here

class EntityReferenceImageField in Acquia Content Hub 8.2

Entity/image/file field reference handling.

Hierarchy

Expanded class hierarchy of EntityReferenceImageField

1 string reference to 'EntityReferenceImageField'
acquia_contenthub.services.yml in ./acquia_contenthub.services.yml
acquia_contenthub.services.yml
1 service uses EntityReferenceImageField
entity_reference_image.field.cdf.unserializer in ./acquia_contenthub.services.yml
Drupal\acquia_contenthub\EventSubscriber\UnserializeContentField\EntityReferenceImageField

File

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

Namespace

Drupal\acquia_contenthub\EventSubscriber\UnserializeContentField
View source
class EntityReferenceImageField implements EventSubscriberInterface {
  use FieldEntityDependencyTrait;

  /**
   * Image field type declaration.
   *
   * @var array
   *   Array of field types,
   */
  protected $fieldTypes = [
    'image',
  ];

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[AcquiaContentHubEvents::UNSERIALIZE_CONTENT_ENTITY_FIELD] = [
      'onUnserializeContentField',
      8,
    ];
    return $events;
  }

  /**
   * Extracts the target storage and retrieves the referenced entity.
   *
   * @param \Drupal\acquia_contenthub\Event\UnserializeCdfEntityFieldEvent $event
   *   The unserialize event.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  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();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityReferenceImageField::$fieldTypes protected property Image field type declaration.
EntityReferenceImageField::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
EntityReferenceImageField::onUnserializeContentField public function Extracts the target storage and retrieves the referenced entity.
FieldEntityDependencyTrait::getEntity protected function Get an entity from the dependency stack.