You are here

class EntityReferenceField in Acquia Content Hub 8.2

Entity/image/file field reference handling.

Hierarchy

Expanded class hierarchy of EntityReferenceField

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

File

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

Namespace

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

  /**
   * Field types to use.
   *
   * @var array
   */
  protected $fieldTypes = [
    'file',
    'entity_reference',
    'entity_reference_revisions',
  ];

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

  /**
   * Extracts the target storage and retrieves the referenced entity.
   *
   * @param \Drupal\acquia_contenthub\Event\UnserializeCdfEntityFieldEvent $event
   *   The unserialize event.
   *
   * @throws \Exception
   */
  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 (!$value) {
          $values[$langcode][$event
            ->getFieldName()] = [];
          continue;
        }
        if (!is_array($value)) {
          $entity = $this
            ->getEntity($value, $event);
          $values[$langcode][$event
            ->getFieldName()] = $entity
            ->id();

          // @todo handle single value ERR fields.
        }
        else {
          foreach ($value as $delta => $item) {
            $entity = $this
              ->getEntity($item, $event);
            if ($event
              ->getFieldMetadata()['type'] == 'entity_reference_revisions') {

              /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
              $values[$langcode][$event
                ->getFieldName()][] = [
                'target_id' => $entity
                  ->id(),
                'target_revision_id' => $entity
                  ->getRevisionId(),
              ];
            }
            else {
              $values[$langcode][$event
                ->getFieldName()][]['target_id'] = $entity
                ->id();
            }
          }
        }
      }
    }
    $event
      ->setValue($values);
    $event
      ->stopPropagation();
  }

}

Members

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