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. Create failing test demonstrating issue
2. Create patch to fix issue

User interface changes

n/a

API changes

n/a

Data model changes

n/a

Release notes snippet

TBD

Comments

WidgetsBurritos created an issue. See original summary.

WidgetsBurritos’s picture

Issue summary: View changes
WidgetsBurritos’s picture

Issue summary: View changes
WidgetsBurritos’s picture

I've attached a failing test and a hopefully working patch.

WidgetsBurritos’s picture

StatusFileSize
new4.41 KB
new2.55 KB

Just a minor patch cleanup

WidgetsBurritos’s picture

Issue summary: View changes
pobster’s picture

At a guess ... I would say that the current language should only be used when the locale is empty (it does start out that way);

  /**
   * {@inheritdoc}
   */
  public function getInfo() {
    $class = get_class($this);
    return [
      '#text' => '',
      '#format' => NULL,
      '#filter_types_to_skip' => [],
      '#langcode' => '',
      '#pre_render' => [
        [$class, 'preRenderText'],
      ],
    ];
  }

I feel like the langcode itself should be injected via the format (as it is now $langcode = $element['#langcode']), I mean ... there is a chance that someone might want to render processed text in a language other than the one the site is currently displaying in ~ so the token content should match the text content being created for whatever locale.

To give an example, the patch in this issue case when given the scenario where the site is being viewed in locale A but requests to display the processed text in using locale B ... we'd be showing the text in the different "B" language but the token content would display content in the currently viewed "A" locale ... if I'm understanding it correctly?

Anyways I feel like we need to put a little more thought in here in how the language is passed up the "chain"... (i.e. ... I don't have any answers here, and this is maybe covering an edge case ...) but it does feel like it'd be a more intuitive solution to affect $element['#langcode'] with the required langcode for display.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

anybody’s picture

Agreed with #8:

At a guess ... I would say that the current language should only be used when the locale is empty

as it seems most helpful and flexible to me in cases which we can't generally predict, as modules may also set the value by custom logic for some cases.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

darvanen’s picture

Flagging for subsystem maintainer review for direction.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

Status: Needs review » Postponed (maintainer needs more info)

Moving to PNMI based on #13 from the subsystem maintainer.

anybody’s picture

@smustgrave I don't really get that change to PNMI - what information is needed and who will have a look here in that status? Sorry.

smustgrave’s picture

Status: Postponed (maintainer needs more info) » Needs review

I know core has certain people for certain modules. Hopefully by changing the status this moves it to the front. After 2 years it feels forgotten

Putting back though so hopefully someone sees it

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

needs-review-queue-bot’s picture

Status: Needs review » Needs work
StatusFileSize
new2.51 KB

The Needs Review Queue Bot tested this issue. It either no longer applies to Drupal core, or fails the Drupal core commit checks. Therefore, this issue status is now "Needs work".

Apart from a re-roll or rebase, this issue may need more work to address feedback in the issue or MR comments. To progress an issue, incorporate this feedback as part of the process of updating the issue. This helps other contributors to know what is outstanding.

Consult the Drupal Contributor Guide to find step-by-step guides for working with issues.

anybody’s picture

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 11.x-dev » main

Drupal core is now using the main branch as the primary development branch. New developments and disruptive changes should now be targeted to the main branch.

Read more in the announcement.