You are here

public function PruneImportedEntitiesFromExport::onPrunePublishCdfEntitiesIfSiteHasDualConfiguration in Acquia Content Hub 8.2

OnPrunePublishCdfEntities event handler.

Parameters

\Drupal\acquia_contenthub\Event\PrunePublishCdfEntitiesEvent $event: Prune published cdf entities event.

Throws

\ReflectionException

\Drupal\Core\Entity\EntityStorageException

File

modules/acquia_contenthub_subscriber/src/EventSubscriber/PrunePublishEntities/PruneImportedEntitiesFromExport.php, line 77

Class

PruneImportedEntitiesFromExport
Prunes imported entities so they are not exported.

Namespace

Drupal\acquia_contenthub_subscriber\EventSubscriber\PrunePublishEntities

Code

public function onPrunePublishCdfEntitiesIfSiteHasDualConfiguration(PrunePublishCdfEntitiesEvent $event) : void {
  if (!$this->checker
    ->siteHasDualConfiguration()) {
    return;
  }

  /** @var \Acquia\ContentHubClient\CDFDocument $document */
  $document = $event
    ->getDocument();
  $doc_uuids = array_keys($document
    ->getEntities());
  $untracked_uuids = $this->tracker
    ->getUntracked($doc_uuids);
  $tracked_uuids = array_diff($doc_uuids, $untracked_uuids);

  // Leave untracked uuids by removing all tracked uuids from the document.
  foreach ($tracked_uuids as $remove_uuid) {
    $document
      ->removeCdfEntity($remove_uuid);
  }

  // Fetch all entities from Plexus from the list of untracked uuids.
  $remote_entities = $event
    ->getClient()
    ->getEntities($untracked_uuids);
  $site_origin = $event
    ->getOrigin();
  foreach ($remote_entities
    ->getEntities() as $entity) {
    $uuid = $entity
      ->getUuid();

    // It's eligible to be exported iff owned by self.
    if ($entity
      ->getOrigin() === $site_origin) {
      continue;
    }

    // Remove from the document to prevent it from being exported.
    $cdf = $document
      ->getCdfEntity($uuid);
    $document
      ->removeCdfEntity($uuid);

    // Add to the tracking table so it never ends up in the export queue.
    $local_entity = $this->entityRepository
      ->loadEntityByUuid($cdf
      ->getAttribute('entity_type')
      ->getValue()['und'], $uuid);
    if ($local_entity) {
      $this->tracker
        ->track($local_entity, $entity
        ->getAttribute('hash')
        ->getValue()['und']);
    }
  }
}