UrlLinkEnhancer should use current language negotiated from content

Created on 9 February 2024, 10 months ago
Updated 13 February 2024, 9 months ago

Problem/Motivation

Within the UrlLinkEnhancer, the current language is obtained by $this->languageManager->getCurrentLanguage() and used to help build the public-facing URL for a link. Drupal's default behaviour is that this language is the one negotiated for user interface text and not necessarily the same as negotiated for content being returned.

This came to light on our decoupled project where the CMS user interface uses a fixed language, but content uses language negotiation over URL prefix. We found that all link field data returned by JSON:API had a path prefix of /en/ (our default language).

Steps to reproduce

  1. Multilingual site with "Interface text language detection" of "Selected language" and "Content language detection" or "URL" (with configured prefixes)
  2. Create a node/taxonomy/etc entity type with a link field
  3. Expose this entity type over JSON:API using JSON:API Extras
  4. Ensure the link field's JSON:API Extras field enhancer is set to "URL for link (link field only)"
  5. After creating, and translations some content, make JSON:API requests for the content in various languages (/en/jsonapi/..., /fr/jsonapi/... etc) and observe the URL value for the link field

Proposed resolution

The current content negotiated language can be obtained by passing a type parameter as per:

$url = Url::fromUri($data['uri'], ['language' => $this->languageManager->getCurrentLanguage(LanguageInterface::TYPE_CONTENT)]);

Patch to follow.

🐛 Bug report
Status

Closed: duplicate

Version

3.0

Component

Code

Created by

🇬🇧United Kingdom Paul_Gregory

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

Comments & Activities

Production build 0.71.5 2024