Url access / PathValidator not language/options aware

Created on 23 August 2018, over 6 years ago
Updated 2 August 2023, over 1 year ago

For this scenario (tests realized in a hook_page_attachments_alter() context):

foreach ($languages as $lang) {

  $url = Url::fromRoute('<current>', [], ['language' => $lang]);
	
  \Drupal::service('path.validator')->isValid($url->toString());
  $url->access();

}
  • The PathValidator will return TRUE only if the specified language match the current language
  • $url->access() doesn't seems to work consistently and is not language/options aware at all

Functional hierarchy for both:

  • PathValidator >> AccessAwareRouter >> AccessManager
  • Url >> AccessManager

Globally, is there a way currently to know if a url/route/path is accessible or not for a specified language whatever the current language context? Something simple similar to drupal_valid_path() in D7 (i would suppose that the answer will be PathValidator :D).

Could some kind of swap logical, a way for some potential solution? As it exists for the ConfigOverride currently, example:

// preserve the original language
$original_language = \Drupal::languageManager()->getConfigOverrideLanguage();

foreach ($languages as $lang) {

  // force $lang for ConfigOverride
  \Drupal::languageManager()->setConfigOverrideLanguage($lang);

  // Do config stuff for $lang here
  // ...

}

// reset to the original language
\Drupal::languageManager()->setConfigOverrideLanguage($original_language);

I supposed that the issue #2940036: Path processors are fixated to current request and not the processed request โ†’ is related?

I guess, some solution here should permit to generate more easily, relevant and "access aware" stuff like: language switcher links, alternate hreflang links, ... Or whatever the kind of scenario needing to generate a set of translated links with access awareness for a given route object.

๐Ÿ› Bug report
Status

Active

Version

11.0 ๐Ÿ”ฅ

Component
Pathย  โ†’

Last updated about 19 hours ago

  • Maintained by
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom @catch
Created by

๐Ÿ‡ซ๐Ÿ‡ทFrance raphaeltbm

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 States recrit

    I've found this to be a bigger issue when creating views with mixed translation links. Any views handler that extends Drupal\views\Plugin\views\field\LinkBase has this issue - entity view link, entity edit link, etc. They all use the LinkBase::checkUrlAccess() which checks the URL access with $this->accessManager->checkNamedRoute($url->getRouteName(), $url->getRouteParameters(), $this->currentUser(), TRUE); which ignores the language of the URL object and consequently checks the URL access using the current interface language.
    Example: When on an admin view where the admin language is English and the views row entity URL is Spanish, the access check will check access for the English version of the route. This can cause very odd issues when the English translation has a published version, but the Spanish version is only in Draft.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States mlncn Minneapolis, MN, USA

    Glad this is already marked major, not so glad there doesn't seem to be a clear path to fixingโ€” because we spent a couple hours debugging custom code which was working fine in Drupal 9 (!?) and blowing up because of this issue in Drupal 10.

    The code in question was in a custom theme in a hook_preprocess_menu():

      $route_name = \Drupal::getContainer()->get('path.matcher')->isFrontPage() ? '<front>' : '<current>';
      $links = \Drupal::languageManager()->getLanguageSwitchLinks(LanguageInterface::TYPE_INTERFACE, Url::fromRoute($route_name));
    

    And kind of contrary to getLanguageSwitchLinks() documentation, we were getting an object with an empty links array, leading subsequent code to blow up completely.

    Againโ€” those exact same lines were working in Drupal 9, and not working, due precisely to the reasons discussed in this issue, in Drupal 10. So something has made this issue a higher priority somehow.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States MegaKeegMan

    Working with mlncn, our working code looks like:

      // $url contains <current> - but we need real route for accessManager.
      $url = Url::fromRoute($route_name);
      // Create path and request.
      $path = $url->toString();
      $request = Request::create($path);
      $urlFull = Url::createFromRequest($request);
    
      $links = \Drupal::languageManager()->getLanguageSwitchLinks(LanguageInterface::TYPE_INTERFACE, $urlFull);
    

    To clarify, the old code worked up until 9.5.4, and broke in Drupal 10. The code here in this comment fixed the issue. Thanks for the example!

  • ๐Ÿ‡ฎ๐Ÿ‡ณIndia sumit_saini

    Facing a similar issue described in #13. Patch suggested in #11 fixes the issue for us.

Production build 0.71.5 2024