Score:0

Why is language prefix not included in Url::fromUri if the used path is not defined as a route?

cn flag

The issue is with the H5P module when Drupal is set to allow language overrides. Specifically this method.

If the language is set to English and user configuration form has Estonian as the language choice, then this will serve the newly loaded H5P libraries in Estonian, because the call is being made to h5peditor/... instead of en/h5peditor/..., which does not set the correct current language.

It seems that for paths that are not defined in the system, and this one is not among the defined routes, CacheableMetadata for current language is not included. I would like to know if that is a bug (and should be reported) or a feature (this is how it functions). My guess would be that any internal path should include a language prefix, even when it is not defined in the system, but it's hard to be sure.

Update.

I suppose that the explanation for that behavior could be found in the documentation of Url::fromUserInput method, which clearly states:

If the path matches a Drupal route, then the URL generation will include Drupal's path processors (e.g., language-prefixing and aliasing). Otherwise, the URL generation will just append the passed-in path to Drupal's base path.

I only see one way to deal with that in a generally usable and meaningful way. Use the route for portal root, add a slash if it has a prefix instead of just a slash.

$baseUrl = Url::fromUri('internal:/')->toString();
$path = "some/path/that/is/not/a/route";

return $baseUrl === '/' ? "/{$path}"
  : "{$baseUrl}/${$path}";

It might not be the best possible solution, but it should still get the job done and work in all possible cases. One could even create a function/method for that which would allow passing the options and maybe something else.

Kevin avatar
in flag
Just going off the code example and docs, it looks like you have to pass the language as an option to fromUri: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Url.php/function/Url%3A%3AfromUri/9.4.x
cn flag
I'll try to give it a go. I did try to provide the value, but that was not the language object. Will report back how it goes.
cn flag
Checked the suggestion out and it seems that both language object and language identifier are acceptable, yet that has no effect whatsoever. It [Url::fromUserInput](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Url.php/function/Url%3A%3AfromUserInput/9.4.x) has an explanation of how things work and it seems that **path processors** will only be applied in case of matched routes. I guess that it is by design and can't be changed in any way.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.