Problem/Motivation

Language negotiation in core changed from Drupal 9 to 10, breaking things.

I have a piece of code in an openid_connect module to generate an internal URL, like this:

    $language_none = \Drupal::languageManager()
      ->getLanguage(LanguageInterface::LANGCODE_NOT_APPLICABLE);

    $redirect_uri = Url::fromRoute(
      'openid_connect.redirect_controller_redirect',
      [
        'openid_connect_client' => $this->getPluginId(),
      ],
      [
        'absolute' => TRUE,
        'language' => $language_none,
      ]
    )->toString(TRUE);

Which in Drupal 9 generated something as "https://mysite.com/openid_connect/acm-idm". I have language negotiation active, but the LanguageInterface::LANGCODE_NOT_APPLICABLE killed the language prefix.

I updated to Drupal 10 and then the behaviour changes: I get as output "https://mysite.com/nl/openid_connect/acm-idm" (note the "nl" prefix).

Looking at the code I end up in "Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl.php" in function "processOutbound":

In Drupal 10 there's code in there like this:

    if (!isset($options['language']) || ($options['language'] instanceof LanguageInterface && in_array($options['language']->getId(), [
      LanguageInterface::LANGCODE_NOT_SPECIFIED,
      LanguageInterface::LANGCODE_NOT_APPLICABLE,
    ]))) {
      $language_url = $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_URL);
      $options['language'] = $language_url;
    }

In Drupal 9 the in_array() part above is not present.

If the language option I set is "LanguageInterface::LANGCODE_NOT_APPLICABLE" I do not want the language prefix to be added. If the LanguageInterface::LANGCODE_NOT_APPLICABLE is removed from the array above all is ok again.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3385550

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

sboden created an issue. See original summary.

cilefen’s picture

Issue tags: -LANGCODE_NOT_APPLICABLE
longwave’s picture

longwave’s picture

I get why LANGUAGE_NOT_SPECIFIED might need a default language prefix adding, but I do wonder if LANGCODE_NOT_APPLICABLE should have one at all.

sboden’s picture

LANGCODE_NOT_APPLICABLE should in my mind not have a language prefix: I have language negotation active on my site, but openid_connect that is used for authentication is very "touchy" about the URLs that you can use.

So in Drupal 9 my custom code generated "https://mysite.com/openid_connect/acm-idm" and after upgrading to Drupal 10 that is magically changed to "https://mysite.com/nl/openid_connect/acm-idm", which breaks openid_connect.

sboden’s picture

StatusFileSize
new1.06 KB

Patch that fixes the LANGCODE_NOT_APPLICABLE issue.

cilefen’s picture

@sboden Please do not test on every platform configuration for a small bug fix that is unrelated to database. Test bots have a cost.

longwave’s picture

Title: Language negotation breaks updating Drupal 9 to 10 » Language negotiation breaks updating Drupal 9 to 10
Status: Active » Needs work

The in_array() can be removed now there is only one value to check.

gauravvvv’s picture

Status: Needs work » Needs review
StatusFileSize
new1.16 KB
new1.16 KB

Removed in_array and fixed CC failure in #6. please review

sboden’s picture

Patch #9 fails: when you remove in_array() the line should be changed to a '=='.

Something as:

if (!isset($options['language']) || ($options['language'] instanceof LanguageInterface && $options['language']->getId() ==
      LanguageInterface::LANGCODE_NOT_SPECIFIED)) {
smustgrave’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Per #10 and will need a test case to show the problem.

rpayanm made their first commit to this issue’s fork.

lauriii’s picture

I now see that there is a valid use case for generating language neutral URLs. Since we don't know the intent in \Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl::processOutbound at least without adding an API, I'm wondering if the right way to fix this would be to revert #2883450: Missing url prefix on language neutral content , and fix the original bug in \Drupal\Core\Entity\EntityBase::toUrl and other instances that are generating URLs? It feels strange that \Drupal\Core\Entity\EntityBase::toUrl would have to care about the interface language but at the same time, it seems like that it needs to care about it because that's where we know which behavior we want.

longwave’s picture

@lauriii What do you think about just dropping the LANGCODE_NOT_APPLICABLE check? The original issue only mentions "language not specified", which to me means we could try to select a default, but "not applicable" means there doesn't need to be a language at all.

lauriii’s picture

I think you're right. We could just drop LANGCODE_NOT_APPLICABLE from there. However, if an entity has LANGCODE_NOT_APPLICABLE we may still want to generate the link with the current language to retain the UI language. I'm fine if we move that to a follow-up to address this first.

Shabbir made their first commit to this issue’s fork.

shabbir’s picture

Have fixed the tests and now i guess its in a mergeable format.

lauriii’s picture

Status: Needs work » Needs review
tuutti’s picture

Status: Needs review » Reviewed & tested by the community

We've been using this for a couple of months now and it seems to work.

quietone’s picture

Issue summary: View changes
Issue tags: +Needs followup

I'm triaging RTBC issues. I read the IS and the comments. The issue summary is incomplete, particularly there isn't a proposed resolution. I have added the standard template but it should at least have an updated 'proposed resolution'.

#16 mentions moving some work to a follow up. I am not sure if that is still needed but I have added the tag and adding it to the remaining tasks so that gets considered.

Leaving at RTBC.

longwave’s picture

Issue tags: -Needs tests, -Needs followup

Opened #3414318: Decide what to do with interface language for LANGCODE_NOT_APPLICABLE entities as followup. Plus, this already has tests, so removing the tag.

longwave’s picture

Version: 10.1.x-dev » 10.2.x-dev
Status: Reviewed & tested by the community » Patch (to be ported)

Committed f028872 and pushed to 11.x. Thanks!

Unsure whether this is eligible for backport. One on hand it seems like a serious bug for the URLs that it affects, but on the other hand some sites may already be used to the new buggy behaviour. Will discuss with the other committers.

  • longwave committed f0288721 on 11.x
    Issue #3385550 by lauriii, Gauravvvv, sboden, Shabbir, longwave:...
quietone’s picture

Issue summary: View changes

The follow up was created.

quietone’s picture

Issue tags: +10.3.0 release notes

I talked with longwave about this and he suggesting tagging for release notes.

longwave’s picture

Version: 10.2.x-dev » 11.x-dev
Status: Patch (to be ported) » Fixed

Let's just mark this as fixed in 10.3.0, as there is a minor behaviour change let's not introduce it in a patch release. Anyone who is affected by this can patch, but with only 13 followers it seems this issue is not that widespread a problem.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.