You are here

trait FieldEntityDependencyTrait in Acquia Content Hub 8.2

Trait for consistency of field entity dependencies.

@package Drupal\acquia_contenthub\EventSubscriber\UnserializeContentField

Hierarchy

File

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

Namespace

Drupal\acquia_contenthub\EventSubscriber\UnserializeContentField
View source
trait FieldEntityDependencyTrait {

  /**
   * Get an entity from the dependency stack.
   *
   * @param string $uuid
   *   The uuid of the entity to retrieve.
   * @param \Drupal\acquia_contenthub\Event\UnserializeCdfEntityFieldEvent $event
   *   The subscribed event.
   *
   * @return \Drupal\Core\Entity\EntityInterface
   *   The retrieved entity.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FieldEntityDependencyTrait::getEntity protected function Get an entity from the dependency stack.