headless_cms-1.0.3/modules/headless_cms_preview/src/ConsumerHeadlessPreviewManager.php
modules/headless_cms_preview/src/ConsumerHeadlessPreviewManager.php
<?php
declare(strict_types=1);
namespace Drupal\headless_cms_preview;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\consumers\Entity\ConsumerInterface;
use Drupal\consumers\Negotiator;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
/**
* Manages headless previews for consumer entities.
*/
class ConsumerHeadlessPreviewManager {
public function __construct(
protected readonly EntityTypeManagerInterface $entityTypeManager,
#[Autowire('@consumer.negotiator')]
protected readonly Negotiator $negotiator,
protected readonly ConfigFactoryInterface $configFactory,
protected readonly AccountProxyInterface $currentUser,
) {}
/**
* Get preview urls for all consumers.
*/
public function getPreviewUrlsForAllConsumers(string $entityTypeId, string $entityBundle, string $entityUuid, string $token): array {
$previewUrls = [];
foreach ($this->entityTypeManager->getStorage('consumer')->loadMultiple() as $consumer) {
$previewUrl = $this->getPreviewUrl($entityTypeId, $entityBundle, $entityUuid, $token, $consumer);
if ($previewUrl) {
$previewUrls[$consumer->label()] = $previewUrl;
}
}
return $previewUrls;
}
/**
* Get entity preview url for a consumer.
*
* If no explicit consumer is provided, the consumer is
* negotiated from the request.
*/
public function getPreviewUrl(string $entityTypeId, string $entityBundle, string $entityUuid, string $token, ?ConsumerInterface $consumer = NULL): ?string {
$consumer = $consumer ?? $this->negotiator->negotiateFromRequest();
$previewUrl = $consumer->get('headless_cms_preview_url')->getString();
if (!$previewUrl) {
return NULL;
}
$previewUrl = str_replace('[preview:entity_type_id]', $entityTypeId, $previewUrl);
$previewUrl = str_replace('[preview:entity_bundle]', $entityBundle, $previewUrl);
$previewUrl = str_replace('[preview:entity_uuid]', $entityUuid, $previewUrl);
$previewUrl = str_replace('[preview:owner_id]', (string) $this->currentUser->id(), $previewUrl);
$previewUrl = str_replace('[preview:token]', $token, $previewUrl);
return $previewUrl;
}
/**
* Get revision urls for all consumers.
*/
public function getRevisionUrlsForAllConsumers(string $entityTypeId, string $entityBundle, string $entityUuid, string $revisionId, string $token): array {
$revisionUrls = [];
foreach ($this->entityTypeManager->getStorage('consumer')->loadMultiple() as $consumer) {
$revisionUrl = $this->getRevisionUrl($entityTypeId, $entityBundle, $entityUuid, $revisionId, $token, $consumer);
if ($revisionUrl) {
$revisionUrls[$consumer->label()] = $revisionUrl;
}
}
return $revisionUrls;
}
/**
* Get entity revision url for a consumer.
*
* If no explicit consumer is provided, the consumer is
* negotiated from the request.
*/
public function getRevisionUrl(string $entityTypeId, string $entityBundle, string $entityUuid, string $revisionId, string $token, ?ConsumerInterface $consumer = NULL): ?string {
$consumer = $consumer ?? $this->negotiator->negotiateFromRequest();
$previewUrl = $consumer->get('headless_cms_revision_url')->getString();
if (!$previewUrl) {
return NULL;
}
$previewUrl = str_replace('[preview:entity_type_id]', $entityTypeId, $previewUrl);
$previewUrl = str_replace('[preview:entity_bundle]', $entityBundle, $previewUrl);
$previewUrl = str_replace('[preview:entity_uuid]', $entityUuid, $previewUrl);
$previewUrl = str_replace('[preview:revision_id]', $revisionId, $previewUrl);
$previewUrl = str_replace('[preview:owner_id]', (string) $this->currentUser->id(), $previewUrl);
$previewUrl = str_replace('[preview:token]', $token, $previewUrl);
return $previewUrl;
}
}
