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
| Comment | File | Size | Author |
|---|---|---|---|
| #21 | 3067999-nr-bot.txt | 2.51 KB | needs-review-queue-bot |
| #6 | interdiff-4-6.txt | 2.55 KB | WidgetsBurritos |
| #6 | core-3067999-processedtext-language-fallback-6.patch | 4.41 KB | WidgetsBurritos |
| #4 | core-3067999-processedtext-language-fallback-4-test-only.patch | 3.69 KB | WidgetsBurritos |
Comments
Comment #2
WidgetsBurritos commentedComment #3
WidgetsBurritos commentedComment #4
WidgetsBurritos commentedI've attached a failing test and a hopefully working patch.
Comment #6
WidgetsBurritos commentedJust a minor patch cleanup
Comment #7
WidgetsBurritos commentedComment #8
pobster commentedAt a guess ... I would say that the current language should only be used when the locale is empty (it does start out that way);
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.Comment #11
anybodyAgreed with #8:
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.
Comment #13
darvanenFlagging for subsystem maintainer review for direction.
Comment #17
smustgrave commentedMoving to PNMI based on #13 from the subsystem maintainer.
Comment #18
anybody@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.
Comment #19
smustgrave commentedI 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
Comment #21
needs-review-queue-bot commentedThe 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.
Comment #22
anybody