I have a theme function that transforms a part of a form into table.

The rows look like this:

    $row[] = drupal_render($items[$key]['thumbnail']);
    $row[] = drupal_render($items[$key]['label']) . drupal_render($items[$key]['note']);
    $row[] = drupal_render($items[$key]['value']);
    $row[] = drupal_render($items[$key]['operations']) . drupal_render($items[$key]);

Note the second and last rows - they combine multiple fields into one row.
These cells will get double escaped by Twig autoescape.
The rows with only one item gets rendered just fine.

This is due to the fact that each render is marked as safe separately. But when they are put together they make up a new and unique string which is not present in SafeMarkup's static cache and therefore it's not treated as safe.

Simple solution for tables is to put the data into separate cells and use colspan.

Comments

Anonymous’s picture

Issue summary: View changes
Anonymous’s picture

Issue summary: View changes
tim.plunkett’s picture

Title: Combining drupal_render() in teheme function causes double escaping » Combining drupal_render() in theme function causes double escaping
Version: 8.x-dev » 8.0.x-dev
Priority: Major » Normal

Seems like user error. Can you paste the rest of the relevant code?

Anonymous’s picture

Status: Active » Closed (works as designed)

It's not a user error. That's the way the theme functions(mostly aimed at transforming forms into tables) were always done.

But I see now(theme_book_admin_table) that SafeMarkup::set(drupal_render($items[$key]['label']) . drupal_render($items[$key]['note'])) is the new way.

I guess there isn't a better way to do this right now but to duplicate the code in SafeMarkup's static cache.

chx’s picture

Absolutely not. The way is to use #type inline_template. Change notice https://www.drupal.org/node/2311123 here.

jibran’s picture

Issue tags: +SafeMarkup