You are here

class ExternalHreflangGetCurrentUrlEventSubscriber in External Hreflang 8

Subscriber for ExternalHreflangGetCurrentUrlEvent.

@package Drupal\external_hreflang\EventSubscriber

Hierarchy

Expanded class hierarchy of ExternalHreflangGetCurrentUrlEventSubscriber

1 string reference to 'ExternalHreflangGetCurrentUrlEventSubscriber'
external_hreflang.services.yml in ./external_hreflang.services.yml
external_hreflang.services.yml
1 service uses ExternalHreflangGetCurrentUrlEventSubscriber
external_hreflang.get_url_event_subscriber in ./external_hreflang.services.yml
Drupal\external_hreflang\EventSubscriber\ExternalHreflangGetCurrentUrlEventSubscriber

File

src/EventSubscriber/ExternalHreflangGetCurrentUrlEventSubscriber.php, line 16

Namespace

Drupal\external_hreflang\EventSubscriber
View source
class ExternalHreflangGetCurrentUrlEventSubscriber implements EventSubscriberInterface {

  /**
   * The path matcher.
   *
   * @var \Drupal\Core\Path\PathMatcherInterface
   */
  private $pathMatcher;

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  private $requestStack;

  /**
   * ExternalHreflangGetCurrentUrlEventSubscriber constructor.
   *
   * @param \Drupal\Core\Path\PathMatcherInterface $path_matcher
   *   The path matcher.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   */
  public function __construct(PathMatcherInterface $path_matcher, RequestStack $request_stack) {
    $this->pathMatcher = $path_matcher;
    $this->requestStack = $request_stack;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[ExternalHreflangGetCurrentUrlEvent::EVENT_NAME][] = [
      'onGetCurrentUrlEvent',
      10,
    ];
    return $events;
  }

  /**
   * Default implementation of getting current page url.
   *
   * @param \Drupal\external_hreflang\Event\ExternalHreflangGetCurrentUrlEvent $event
   *   Event object.
   */
  public function onGetCurrentUrlEvent(ExternalHreflangGetCurrentUrlEvent $event) {
    if ($this->pathMatcher
      ->isFrontPage()) {
      $url = Url::fromRoute('<front>');
    }
    else {
      $url = Url::createFromRequest($this->requestStack
        ->getCurrentRequest());
    }
    $event
      ->setCurrentUrl($url);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ExternalHreflangGetCurrentUrlEventSubscriber::$pathMatcher private property The path matcher.
ExternalHreflangGetCurrentUrlEventSubscriber::$requestStack private property The request stack.
ExternalHreflangGetCurrentUrlEventSubscriber::getSubscribedEvents public static function
ExternalHreflangGetCurrentUrlEventSubscriber::onGetCurrentUrlEvent public function Default implementation of getting current page url.
ExternalHreflangGetCurrentUrlEventSubscriber::__construct public function ExternalHreflangGetCurrentUrlEventSubscriber constructor.