Suppose I have an alias like http://www.example.com/en/the-new-webpage.html.

I want to get the the-new-webpage.html part in hook_preprocess_html(). The language code might change into 'ja' or some other code due to the language availability. So I can't get the URL and pass it to str_replace() to replace the language part. Is there a function or a way to get the alias part without the language code?


Since you just need the last part of the path, you can use the following code in a hook_preprocess_html() implementation.

$current_path = \Drupal::service('path.current')->getPath();
$alias = \Drupal::service('path.alias_manager')->getAliasByPath($current_path);
if (basename($alias) == 'the-new-webpage.html') {
  // Your code here
| improve this answer | |
  • 1
    It works on some situations. But when it comes to a URL like http://www.example.com/en/my-page/the-new-webpage.html, this only gives the-new-webpage.html. But I want to get the whole my-page/the-new-webpage.html part :) – i am batman Mar 14 '18 at 8:43
  • @iambatman why would you have a .html file to begin with. Drupal does not use .html files – No Sssweat Mar 15 '18 at 1:07
  • @NoSssweat That is a url alias I have created using path auto – i am batman Mar 15 '18 at 3:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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