When getAliasByPath is called, allow it to add the language path prefix for unaliased paths

Created on 26 July 2016, over 8 years ago
Updated 10 July 2024, 6 months ago

In AliasManager.php on lines 198, 231 and 248 (Drupal 8.1.1 src -- line numbers might differ in other versions), there's return $path.

This happens in certain cases, for example, if the path has no alias, or if the path is not in the whitelist. While this is OK, the path returned doesn't contain the current language, or the language passed in by parameter to the method.

As a result, calling:

\Drupal::service('path.alias_manager')->getAliasByPath('/taxonomy/term/1', 'fr');

results in an URL like:

/taxonomy/term/1

while the correct path should be:

/fr/taxonomy/term/1

(assuming there is no alias created for that path)

πŸ› Bug report
Status

Postponed: needs info

Version

11.0 πŸ”₯

Component
RoutingΒ  β†’

Last updated 2 days ago

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡¬πŸ‡§United Kingdom catch

    Retitling however I'm not sure this is the right place to do this.

    Does the Url class not handle this already for example using ::fromUri() and then ::toString()?

  • πŸ‡ΊπŸ‡ΈUnited States itmaybejj

    This stumped me on Editoria11y. I'm currently doing this wildly inelegant thing to get the true alias:

      $page_path = \Drupal::service('path_alias.manager')->getAliasByPath('/node/' . $nid, $language);
      $lang_check = Url::fromRoute('<current>')->toString();
      if ($language &&
          str_starts_with($lang_check, '/' . $language . '/') &&
          !str_starts_with($page_path, '/' . $language . '/')) {
                $page_path = '/' . $language . $page_path;
           }
    

    The alias manager will get me the "nice" alias...but not reliably language prefix. Url::fromRoute will get me the prefix, but not the nice alias. So I have been running both queries and prefixing as needed.

Production build 0.71.5 2024