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.

Remaining tasks

User interface changes

Introduced terminology

API changes

Data model changes

Release notes snippet

Comments

murz created an issue. See original summary.

pavel_spn’s picture

Hi there!
I faced with this issue too.

quietone’s picture

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

Hi, 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.

thomas.frobieter’s picture

Just noticed broken elements on a customer page because of this. Could someone provide a patch please?

plopesc’s picture

We 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.

grevil’s picture

Yep, this is a major regression IMO and should be fixed ASAP!

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.