class AliasPathMatcher

Extends the default path matcher to check aliases.

Hierarchy

Expanded class hierarchy of AliasPathMatcher

File

core/modules/path_alias/src/AliasPathMatcher.php, line 14

Namespace

Drupal\path_alias
View source
class AliasPathMatcher implements PathMatcherInterface {
  
  /**
   * Whether the current page is the front page.
   */
  protected ?bool $isCurrentFrontPage = NULL;
  public function __construct(#[AutowireDecorated] protected PathMatcherInterface $decorated, protected RouteMatchInterface $routeMatch, protected AliasManagerInterface $aliasManager, protected ConfigFactoryInterface $configFactory) {
  }
  
  /**
   * {@inheritdoc}
   */
  public function matchPath($path, $patterns) {
    return $this->decorated
      ->matchPath($path, $patterns);
  }
  
  /**
   * {@inheritdoc}
   */
  public function isFrontPage() {
    // Cache the result as this is called often.
    $this->isCurrentFrontPage ??= $this->decorated
      ->isFrontPage() || $this->isAliasFrontPage();
    return $this->isCurrentFrontPage;
  }
  
  /**
   * Checks if the current page is the front page by comparing aliases.
   */
  protected function isAliasFrontPage() : bool {
    // Ensure that the code can also be executed when there is no active
    // route match, like on exception responses.
    if (!$this->routeMatch
      ->getRouteName()) {
      return FALSE;
    }
    $url = Url::fromRouteMatch($this->routeMatch);
    $path = '/' . $url->getInternalPath();
    $frontPagePath = $this->configFactory
      ->get('system.site')
      ->get('page.front');
    return $this->aliasManager
      ->getAliasByPath($path) === $frontPagePath;
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
AliasPathMatcher::$isCurrentFrontPage protected property Whether the current page is the front page.
AliasPathMatcher::isAliasFrontPage protected function Checks if the current page is the front page by comparing aliases.
AliasPathMatcher::isFrontPage public function Checks if the current page is the front page. Overrides PathMatcherInterface::isFrontPage
AliasPathMatcher::matchPath public function Checks if a path matches any pattern in a set of patterns. Overrides PathMatcherInterface::matchPath
AliasPathMatcher::__construct public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.