PublishOnlyRendered.php in Acquia Lift Connector 8.4
File
modules/acquia_lift_publisher/src/EventSubscriber/Publish/PublishOnlyRendered.php
View source
<?php
namespace Drupal\acquia_lift_publisher\EventSubscriber\Publish;
use Acquia\ContentHubClient\CDF\CDFObject;
use Drupal\acquia_contenthub\AcquiaContentHubEvents;
use Drupal\acquia_contenthub\ContentHubCommonActions;
use Drupal\acquia_contenthub\Event\PrunePublishCdfEntitiesEvent;
use Drupal\acquia_contenthub_publisher\ContentHubPublisherEvents;
use Drupal\acquia_contenthub_publisher\Event\ContentHubEntityEligibilityEvent;
use Drupal\acquia_lift_publisher\Form\ContentPublishingForm;
use Drupal\acquia_lift_publisher\Form\ContentPublishingSettingsTrait;
use Drupal\Core\Config\ImmutableConfig;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class PublishOnlyRendered implements EventSubscriberInterface {
use ContentPublishingSettingsTrait;
private $commonActions;
public function __construct(ContentHubCommonActions $common_actions, ImmutableConfig $config) {
$this->commonActions = $common_actions;
$this->publisherSettings = $config;
}
public static function getSubscribedEvents() {
return [
ContentHubPublisherEvents::ENQUEUE_CANDIDATE_ENTITY => [
'onEnqueueCandidateEntity',
99,
],
AcquiaContentHubEvents::PRUNE_PUBLISH_CDF_ENTITIES => [
'onPrunePublishCdfEntities',
1000,
],
];
}
public function onEnqueueCandidateEntity(ContentHubEntityEligibilityEvent $event) : void {
if (!$this
->personalizedContentPushIsActive()) {
return;
}
$entity = $event
->getEntity();
if (empty($this
->getEntityViewModesSettingValue($entity))) {
$event
->setEligibility(FALSE);
$event
->stopPropagation();
return;
}
$entities = [];
$cdfs = $this->commonActions
->getEntityCdf($entity, $entities, TRUE, FALSE);
foreach ($cdfs as $cdf) {
if ($cdf
->getType() !== 'rendered_entity') {
continue;
}
$source_uuid = $this
->getCdfEntityAttributeValue($cdf, 'source_entity');
if ($source_uuid === $entity
->uuid()) {
$event
->setEligibility(TRUE);
$event
->setCalculateDependencies(FALSE);
return;
}
}
$event
->setEligibility(FALSE);
$event
->stopPropagation();
}
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]);
}
}
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);
}
}
private function getCdfEntityAttributeValue(CDFObject $cdf, string $attribute) {
$attribute_obj = $cdf
->getAttribute($attribute);
if (!$attribute_obj) {
return NULL;
}
$attr_val = $attribute_obj
->getValue();
return $attr_val[CDFObject::LANGUAGE_UNDETERMINED] ?? NULL;
}
private function personalizedContentPushIsActive() : bool {
return (bool) $this->publisherSettings
->get(ContentPublishingForm::$pushSettingField);
}
}