You are here

public function PublishOnlyRendered::onPrunePublishCdfEntities in Acquia Lift Connector 8.4

Removes cdfs that are not part of the personalized content scheme.

Parameters

\Drupal\acquia_contenthub\Event\PrunePublishCdfEntitiesEvent $event: The current publishing event.

File

modules/acquia_lift_publisher/src/EventSubscriber/Publish/PublishOnlyRendered.php, line 105

Class

PublishOnlyRendered
Responsible for filtering contents based on publishing configurations.

Namespace

Drupal\acquia_lift_publisher\EventSubscriber\Publish

Code

public function onPrunePublishCdfEntities(PrunePublishCdfEntitiesEvent $event) : void {
  if (!$this
    ->personalizedContentPushIsActive()) {
    return;
  }
  $cdf_document = $event
    ->getDocument();
  $cdfs_to_remove = $cdf_document
    ->getEntities();
  $rendered_entity = NULL;
  foreach ($cdfs_to_remove as $uuid => $cdf_entity) {
    if ($cdf_entity
      ->getType() === 'rendered_entity') {
      $rendered_entity = $cdf_entity;
      unset($cdfs_to_remove[$uuid]);
    }
  }

  // Extra sanity check. The personalized content push was set to true,
  // therefore, if for some reason the rendered entity is missing, stop the
  // execution.
  if (!$rendered_entity) {
    return;
  }
  $se_uuid = $this
    ->getCdfEntityAttributeValue($rendered_entity, 'source_entity');
  if (!$se_uuid) {
    return;
  }
  $cdfs_to_keep = [
    $se_uuid,
  ];
  $source_entity = $cdfs_to_remove[$se_uuid];
  $tags = $this
    ->getCdfEntityAttributeValue($source_entity, 'tags');
  if (is_array($tags)) {
    array_push($cdfs_to_keep, ...$tags);
  }
  $cdfs_to_remove = array_diff(array_keys($cdfs_to_remove), $cdfs_to_keep);
  foreach ($cdfs_to_remove as $uuid) {
    $cdf_document
      ->removeCdfEntity($uuid);
  }
}