You are here

public function PullIntent::loadEmbeddedEntity in CMS Content Sync 8

Same name and namespace in other branches
  1. 2.1.x src/PullIntent.php \Drupal\cms_content_sync\PullIntent::loadEmbeddedEntity()
  2. 2.0.x src/PullIntent.php \Drupal\cms_content_sync\PullIntent::loadEmbeddedEntity()

Restore an entity that was added via {{

Parameters

array $definition: The definition you saved in a field and gotten back when calling one of the mentioned functions above

Return value

\Drupal\Core\Entity\EntityInterface the restored entity

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

\Drupal\cms_content_sync\Exception\SyncException

See also

SyncIntent::embedEntityDefinition} or

SyncIntent::embedEntity}.

File

src/PullIntent.php, line 381

Class

PullIntent

Namespace

Drupal\cms_content_sync

Code

public function loadEmbeddedEntity($definition) {
  $reference = $this->operation
    ->loadReference($definition);
  $expected_version = Flow::getEntityTypeVersion($reference
    ->getType(), $reference
    ->getBundle());
  if ($expected_version != $reference
    ->getVersion()) {
    \Drupal::logger('cms_content_sync')
      ->warning('Outdated reference to @entity_type:@bundle: Remote version @remote_version doesn\'t match local version @local_version<br>Flow: @flow_id | Pool: @pool_id', [
      '@entity_type' => $reference
        ->getType(),
      '@bundle' => $reference
        ->getBundle(),
      '@remote_version' => $reference
        ->getVersion(),
      '@local_version' => $expected_version,
      '@flow_id' => $this
        ->getFlow()
        ->id(),
      '@pool_id' => $this
        ->getPool()
        ->id(),
    ]);
  }
  if ($reference
    ->isEmbedded()) {
    $embedded_entity = $reference
      ->getEmbeddedEntity();
    if (empty($embedded_entity)) {
      return null;
    }
    $pool_id = $reference
      ->getPoolId();
    $pool = Pool::getAll()[$pool_id];
    if (empty($pool)) {
      return null;
    }
    $entity_type_name = $reference
      ->getType();
    $entity_bundle = $reference
      ->getBundle();
    $flow = Flow::getFlowForApiAndEntityType($pool, $entity_type_name, $entity_bundle, PullIntent::PULL_AS_DEPENDENCY, SyncIntent::ACTION_CREATE);
    if (!$flow) {
      return null;
    }
    $intent = new PullIntent($flow, $pool, PullIntent::PULL_AS_DEPENDENCY, SyncIntent::ACTION_CREATE, $entity_type_name, $entity_bundle, $embedded_entity, $this->entityType, $this->uuid);
    $status = $intent
      ->execute();
    if (!$status) {
      return null;
    }
    return $intent
      ->getEntity();
  }
  if (empty($reference
    ->getId())) {
    $entity = \Drupal::service('entity.repository')
      ->loadEntityByUuid($reference
      ->getType(), $reference
      ->getUuid());
  }
  else {
    $entity = \Drupal::entityTypeManager()
      ->getStorage($reference
      ->getType())
      ->load($reference
      ->getId());
  }

  // Taxonomy terms can be mapped by their name.
  if (!$entity && !empty($reference
    ->getName())) {
    $config = $this->flow
      ->getEntityTypeConfig($reference
      ->getType(), $reference
      ->getBundle());
    if (!empty($config) && !empty($config['handler_settings'][DefaultTaxonomyHandler::MAP_BY_LABEL_SETTING])) {
      $entity_type = \Drupal::entityTypeManager()
        ->getDefinition($reference
        ->getType());
      $label_property = $entity_type
        ->getKey('label');
      $existing = \Drupal::entityTypeManager()
        ->getStorage($reference
        ->getType())
        ->loadByProperties([
        $label_property => $reference
          ->getName(),
      ]);
      $entity = reset($existing);
    }
  }
  return $entity;
}