ProcessedText filter doesn't pass in correct language when using language fallback

Created on 15 July 2019, over 5 years ago
Updated 31 January 2023, almost 2 years ago

Problem/Motivation

Drupal\filter\Element\ProcessedText::preRenderText() presently grabs the langcode from $element['#langcode'] and then passes it into respective text filters.

  public static function preRenderText($element) {
    // ...
    $langcode = $element['#langcode'];
    // ...

    // Get a complete list of filters, ordered properly.
    /** @var \Drupal\filter\Plugin\FilterInterface[] $filters **/
    $filters = $format->filters();

    // Give filters a chance to escape HTML-like data such as code or formulas.
    foreach ($filters as $filter) {
      if ($filter_must_be_applied($filter)) {
        $text = $filter->prepare($text, $langcode);
      }
    }

    // Perform filtering.
    $metadata = BubbleableMetadata::createFromRenderArray($element);
    foreach ($filters as $filter) {
      if ($filter_must_be_applied($filter)) {
        $result = $filter->process($text, $langcode);
        $metadata = $metadata->merge($result);
        $text = $result->getProcessedText();
      }
    }
    // ...

However, as identified in #3063657: Token always output in wrong / default language β†’ , if a node is referencing a token that has a translation for a particular language it will only load those translations if the node itself is translated into that same language:

1. You use a module like language hierarchy to provide language fallback functionality for various regions. For example:
English:
-> /en-us
-> /en-gb
2. You create a node in the base English language that contains a token.
3. You visit that node in /en-gb. Because the node was created using the primary english translation, if you visit /en-gb, the filter module will pass in `en` as the langcode, despite the fact you're visiting the page in en-gb.

Proposed resolution

Use the current langcode (\Drupal::languageManager()->getCurrentLanguage()->getId()) instead of $element['#langcode']

Remaining tasks

1.
2.

User interface changes

n/a

API changes

n/a

Data model changes

n/a

Release notes snippet

TBD

πŸ› Bug report
Status

Needs work

Version

10.1 ✨

Component
FilterΒ  β†’

Last updated 4 days ago

No maintainer
Created by

πŸ‡ΊπŸ‡ΈUnited States WidgetsBurritos San Antonio, TX

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

  • Needs subsystem maintainer review

    It is used to alert the maintainer(s) of a particular core subsystem that an issue significantly impacts their subsystem, and their signoff is needed (see the governance policy draft for more information). Also, if you use this tag, make sure the issue component is set to the correct subsystem. If an issue significantly impacts more than one subsystem, use needs framework manager review instead.

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.

Production build 0.71.5 2024