Problem/Motivation
After the upgrade from Drupal Core 11.2.4 to 11.2.5, the Twig function "render" started to return an object instead of the string.
Steps to reproduce
Try this code in any Twig template to reproduce:
{{ dump('Hello one'|t) }}
{{ dump('Hello two'|t|render) }}
{{ dump('Hello three'|t|render ~ '') }}
With Drupal Core 11.2.4 , the "Hello two" will be rendered as string, with 11.2.5 and later - as the TranslatableMarkup instead of the string:
Drupal\Core\StringTranslation\TranslatableMarkup {
#string: "Hello two"
#arguments: []
#translatedMarkup: null
#options: []
#stringTranslation: null
}
But if we force converting it to a string using a Twig construction, the conversion works well in the "Hello three" string.
Proposed resolution
This change was merged in the scope of the issue #2334319: {% trans %} does not support render array and MarkupInterface valued expressions:
https://git.drupalcode.org/project/drupal/-/merge_requests/2968/diffs#64...
if ($arg instanceof RenderableInterface) {
$arg = $arg->toRenderable();
}
+ elseif ($arg instanceof MarkupInterface) {
+ return $arg;
+ }
elseif (method_exists($arg, '__toString')) {
return (string) $arg;
}
If this change is intended, it is a pretty breaking change, so it's better to revert it if there are no real reasons to keep it as is.
And if we need to keep it, we need to create a change record, describing this change.
Comments
Comment #2
pavel_spn commentedHi there!
I faced with this issue too.
Comment #3
quietone commentedHi, in Drupal core changes are made on on 11.x (our main development branch) first, and are then back ported as needed according to the Core change policies. Thanks.
Comment #4
thomas.frobieterJust noticed broken elements on a customer page because of this. Could someone provide a patch please?
Comment #5
plopescWe also had the same issue.
Pointed out in #2334319-114: {% trans %} does not support render array and MarkupInterface valued expressions how we made a workaround for Views custom fields.
Comment #6
grevil commentedYep, this is a major regression IMO and should be fixed ASAP!