You are here

protected function FieldEntityDependencyTrait::getEntity in Acquia Content Hub 8.2

Get an entity from the dependency stack.

Parameters

string $uuid: The uuid of the entity to retrieve.

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

Return value

\Drupal\Core\Entity\EntityInterface The retrieved entity.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

3 calls to FieldEntityDependencyTrait::getEntity()
EntityReferenceField::onUnserializeContentField in src/EventSubscriber/UnserializeContentField/EntityReferenceField.php
Extracts the target storage and retrieves the referenced entity.
EntityReferenceImageField::onUnserializeContentField in src/EventSubscriber/UnserializeContentField/EntityReferenceImageField.php
Extracts the target storage and retrieves the referenced entity.
TextItemField::onUnserializeContentField in src/EventSubscriber/UnserializeContentField/TextItemField.php
Extract the stored filter_format uuid and retrieve the entity id.

File

src/EventSubscriber/UnserializeContentField/FieldEntityDependencyTrait.php, line 28

Class

FieldEntityDependencyTrait
Trait for consistency of field entity dependencies.

Namespace

Drupal\acquia_contenthub\EventSubscriber\UnserializeContentField

Code

protected function getEntity($uuid, UnserializeCdfEntityFieldEvent $event) {

  // Check stack first because uuids are referenced from origin, not local.
  if ($event
    ->getStack()
    ->hasDependency($uuid)) {
    return $event
      ->getStack()
      ->getDependency($uuid)
      ->getEntity();
  }

  // Only fall back to local uuids as an absolute last resort.
  if (empty($event
    ->getFieldMetadata()['target'])) {
    throw new \Exception(sprintf("The %s field does not specify a metadata target. This is likely due to an unresolved dependency export process. Please check your relationships.", $event
      ->getFieldName()));
  }
  $storage = \Drupal::entityTypeManager()
    ->getStorage($event
    ->getFieldMetadata()['target']);
  $entities = $storage
    ->loadByProperties([
    'uuid' => $uuid,
  ]);
  return reset($entities);
}