You are here

public function PushIntent::addReference in CMS Content Sync 2.1.x

Same name and namespace in other branches
  1. 8 src/PushIntent.php \Drupal\cms_content_sync\PushIntent::addReference()
  2. 2.0.x src/PushIntent.php \Drupal\cms_content_sync\PushIntent::addReference()

Push the provided entity as a simple reference. There is no guarantee the referenced entity will be available on the remote site as well, but if it is, it will be de-referenced. If you need the referenced entity to be available, use {

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The referenced entity to push as well

array $details: {@see SyncIntent::getEmbedEntityDefinition}

Return value

array|object the definition you can store via {@see SyncIntent::setField} and on the other end receive via {@see SyncIntent::getField}

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

\Drupal\Core\Entity\EntityStorageException

\Drupal\cms_content_sync\Exception\SyncException

\GuzzleHttp\Exception\GuzzleException

See also

PushIntent::addDependency} instead.

File

src/PushIntent.php, line 918

Class

PushIntent
Class PushIntent.

Namespace

Drupal\cms_content_sync

Code

public function addReference($entity, $details = null) {

  // Check if the Pool has been selected manually. In this case, we need to embed the entity despite the AUTO PUSH not being set.
  $statuses = EntityStatus::getInfosForEntity($entity
    ->getEntityTypeId(), $entity
    ->uuid(), [
    'flow' => $this->flow
      ->id(),
  ]);
  if (in_array($entity
    ->getEntityTypeId(), ContentSyncSettings::getInstance()
    ->getEmbedEntities())) {
    $result = null;
    foreach ($statuses as $status) {
      if ($status
        ->isManualPushEnabled()) {
        $result = $this
          ->embedForFlowAndPool($entity, $details, $status
          ->getFlow(), $status
          ->getPool());
      }
    }
    if ($result) {
      return $result;
    }

    // Not pushed? Just using our current pool then to de-reference it at the remote site if the entity exists.
    return $this->operation
      ->addReference($entity
      ->getEntityTypeId(), $entity
      ->bundle(), $entity
      ->uuid(), EntityHandlerPluginManager::isEntityTypeConfiguration($entity
      ->getEntityType()) ? $entity
      ->id() : null, Flow::getEntityTypeVersion($entity
      ->getEntityTypeId(), $entity
      ->bundle()), [
      $this->pool->id,
    ], $entity
      ->language()
      ->getId(), $entity
      ->label(), $details);
  }
  foreach ($statuses as $status) {
    if ($status
      ->isManualPushEnabled()) {

      // This is only relevant for dependencies.
      // If the mode is "all" or "manual" we must not add them as a dependency as otherwise this can result in pushing referenced entities endlessly.
      if ($this->flow
        ->getController()
        ->canPushEntity($entity, PushIntent::PUSH_AS_DEPENDENCY)) {
        return $this
          ->addDependency($entity, $details, false);
      }
    }
  }
  $pools = $this
    ->pushReference($entity, false);

  // Not pushed? Just using our current pool then to de-reference it at the remote site if the entity exists.
  if (empty($pools)) {
    return $this->operation
      ->addReference($entity
      ->getEntityTypeId(), $entity
      ->bundle(), $entity
      ->uuid(), EntityHandlerPluginManager::isEntityTypeConfiguration($entity
      ->getEntityType()) ? $entity
      ->id() : null, Flow::getEntityTypeVersion($entity
      ->getEntityTypeId(), $entity
      ->bundle()), [
      $this->pool->id,
    ], $entity
      ->language()
      ->getId(), $entity
      ->label(), $details);
  }
  $result = null;
  foreach ($pools as $pool_id) {
    $result = $this->operation
      ->addReference($entity
      ->getEntityTypeId(), $entity
      ->bundle(), $entity
      ->uuid(), EntityHandlerPluginManager::isEntityTypeConfiguration($entity
      ->getEntityType()) ? $entity
      ->id() : null, Flow::getEntityTypeVersion($entity
      ->getEntityTypeId(), $entity
      ->bundle()), [
      $pool_id,
    ], $entity
      ->language()
      ->getId(), $entity
      ->label(), $details);
  }
  return $result;
}