I have used the following code in a Drupal 8 theme file.

$current_path = \Drupal::request()->getRequestUri();
$path_alias = Unicode::strtolower(\Drupal::service('path.alias_manager')->getAliasByPath($current_path));
$visiblity = \Drupal::service('path.matcher')->matchPath($path_alias, $pages);

$pages contains values like '<front>' or '/blog'.

$visiblity is set to FALSE for /, but to TRUE for /node; Both are the front page.

What is the wrong with my code?

1 Answer 1


Your code is assuming that the request URI for / will retrieve /node on the getAliasByPath() method call. It won't. Setting the Drupal front page isn't the same as setting a path alias in the Path module.

Use the following code, which will return Drupal's internal /node path even on / requests.

$current_path = \Drupal::service('path.current')->getPath();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.