You are here

public function EntityManager::enqueueCandidateEntity in Acquia Content Hub 8

Enqueue an entity with an operation to be performed on Content Hub.

Parameters

object $entity: The Drupal Entity object.

bool $do_export: TRUE if the entity action is 'EXPORT'; FALSE, if it is 'UNEXPORT'.

File

src/EntityManager.php, line 154

Class

EntityManager
Provides a service for managing entity actions for Content Hub.

Namespace

Drupal\acquia_contenthub

Code

public function enqueueCandidateEntity($entity, $do_export = TRUE) {

  // Early return, if entity is not eligible.
  if (!$this
    ->isEligibleEntity($entity)) {
    return;
  }

  // Register shutdown function.
  $this
    ->registerShutdownFunction();
  $unexporting_entities = [];
  $exporting_entities = [];

  // Only care about the case where entity is 1) node, and 2) updating.
  if ($entity instanceof NodeInterface && isset($entity->original)) {

    // We also know the $do_export at this point should be TRUE.
    $old_is_published = $entity->original
      ->isPublished();
    $new_is_published = $this
      ->isPublished($entity);
    if (!$new_is_published) {
      $do_export = FALSE;
    }

    // If "published to unpublished", unexport it and its old descendants.
    if ($old_is_published && !$new_is_published) {
      $unexporting_entities += $this
        ->getReferencedEntities($entity->original);
    }
  }

  // Only add references to be exported if the entity is to be exported. In
  // the case of unexporting/deletion, references should not be exported.
  if ($do_export) {
    $exporting_entities += $this
      ->getReferencedEntities($entity);
  }

  // If "to published", we have to move any disassociated entities exporting
  // list to unexporting list.
  if ($do_export && $entity instanceof EntityInterface && isset($entity->original)) {

    // Find all UUIDs of the exported entities.
    $exporting_entities_uuids = [];
    foreach ($exporting_entities as $exporting_entity) {
      $exporting_entities_uuids[$exporting_entity
        ->uuid()] = TRUE;
    }
    $exported_entities = $this
      ->getReferencedEntities($entity->original);

    // For each exported entity, check if it still remains associated.
    foreach ($exported_entities as $key => $exported_entity) {

      // If remain associated, leave it alone.
      if (isset($exporting_entities_uuids[$exported_entity
        ->uuid()])) {
        continue;
      }

      // Otherwise, if disassociated, move it from export to unexport queue.
      $unexporting_entities[] = $exported_entity;
      unset($exported_entities[$key]);
    }
  }

  // Enqueue unexport and export entities separately.
  $this
    ->enqueueQualifiedEntities($unexporting_entities, FALSE);
  $this
    ->enqueueQualifiedEntities($exporting_entities, TRUE);
  $action = $do_export ? self::EXPORT : self::UNEXPORT;
  $this->candidateEntities[$action][$entity
    ->uuid()] = $entity;
}