You are here

class IsPathAliasForUnpublishedContent in Acquia Content Hub 8.2

Subscribes to entity eligibility to prevent enqueueing path alias.

Hierarchy

  • class \Drupal\acquia_contenthub_publisher\EventSubscriber\EnqueueEligibility\IsPathAliasForUnpublishedContent implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of IsPathAliasForUnpublishedContent

1 file declares its use of IsPathAliasForUnpublishedContent
IsPathAliasForUnpublishedContentTest.php in modules/acquia_contenthub_publisher/tests/src/Unit/EventSubscriber/EntityEligibility/IsPathAliasForUnpublishedContentTest.php
1 string reference to 'IsPathAliasForUnpublishedContent'
acquia_contenthub_publisher.services.yml in modules/acquia_contenthub_publisher/acquia_contenthub_publisher.services.yml
modules/acquia_contenthub_publisher/acquia_contenthub_publisher.services.yml
1 service uses IsPathAliasForUnpublishedContent
pathalias_for_unpublished_content.enqueue in modules/acquia_contenthub_publisher/acquia_contenthub_publisher.services.yml
Drupal\acquia_contenthub_publisher\EventSubscriber\EnqueueEligibility\IsPathAliasForUnpublishedContent

File

modules/acquia_contenthub_publisher/src/EventSubscriber/EnqueueEligibility/IsPathAliasForUnpublishedContent.php, line 17

Namespace

Drupal\acquia_contenthub_publisher\EventSubscriber\EnqueueEligibility
View source
class IsPathAliasForUnpublishedContent implements EventSubscriberInterface {

  /**
   * The url matcher.
   *
   * @var \Symfony\Component\Routing\Matcher\UrlMatcherInterface
   */
  protected $matcher;

  /**
   * The Entity Moderated Revision Service.
   *
   * @var \Drupal\acquia_contenthub_publisher\EntityModeratedRevision
   */
  protected $entityModeratedRevision;

  /**
   * The acquia_contenthub_publisher logger channel.
   *
   * @var \Drupal\Core\Logger\LoggerChannelInterface
   */
  protected $achLoggerChannel;

  /**
   * IsPathAliasForUnpublishedContent constructor.
   *
   * @param \Symfony\Component\Routing\Matcher\UrlMatcherInterface $matcher
   *   URL Matcher service.
   * @param \Drupal\acquia_contenthub_publisher\EntityModeratedRevision $entity_moderated_revision
   *   Entity Moderated Revision Service.
   * @param \Drupal\Core\Logger\LoggerChannelInterface $logger_channel
   *   The acquia_contenthub_publisher logger channel.
   */
  public function __construct(UrlMatcherInterface $matcher, EntityModeratedRevision $entity_moderated_revision, LoggerChannelInterface $logger_channel) {
    $this->matcher = $matcher;
    $this->entityModeratedRevision = $entity_moderated_revision;
    $this->achLoggerChannel = $logger_channel;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[ContentHubPublisherEvents::ENQUEUE_CANDIDATE_ENTITY][] = [
      'onEnqueueCandidateEntity',
      50,
    ];
    return $events;
  }

  /**
   * Prevent path aliases from enqueueing unpublished content.
   *
   * @param \Drupal\acquia_contenthub_publisher\Event\ContentHubEntityEligibilityEvent $event
   *   The event to determine entity eligibility.
   *
   * @throws \Exception
   */
  public function onEnqueueCandidateEntity(ContentHubEntityEligibilityEvent $event) {

    // Never export path alias dependencies that are not "published" .
    $entity = $event
      ->getEntity();
    if (!$entity instanceof PathAliasInterface) {
      return;
    }
    try {
      $params = $this->matcher
        ->match($entity
        ->getPath());
      foreach ($params['_raw_variables']
        ->keys() as $parameter) {
        if (!empty($params[$parameter])) {
          $entity_dependency = $params[$parameter];
          if ($entity_dependency instanceof EntityInterface && !$this->entityModeratedRevision
            ->isPublishedRevision($entity_dependency)) {
            $event
              ->setEligibility(FALSE);
            $event
              ->stopPropagation();
          }
        }
      }
    } catch (\Exception $e) {
      $this->achLoggerChannel
        ->error('Following error occurred while trying to get the matching entity for path alias with uuid: @uuid. Error: @error', [
        '@uuid' => $entity
          ->uuid(),
        '@error' => $e
          ->getMessage(),
      ]);

      // Set the eligibility false as this path alias is a problematic entity.
      $event
        ->setEligibility(FALSE);
      $event
        ->stopPropagation();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
IsPathAliasForUnpublishedContent::$achLoggerChannel protected property The acquia_contenthub_publisher logger channel.
IsPathAliasForUnpublishedContent::$entityModeratedRevision protected property The Entity Moderated Revision Service.
IsPathAliasForUnpublishedContent::$matcher protected property The url matcher.
IsPathAliasForUnpublishedContent::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
IsPathAliasForUnpublishedContent::onEnqueueCandidateEntity public function Prevent path aliases from enqueueing unpublished content.
IsPathAliasForUnpublishedContent::__construct public function IsPathAliasForUnpublishedContent constructor.