My HTML, or what I thought was HTML, was actually generating an object of type Twig_Markup rather than a string, and therefore wasn't passing json validation. Hmm. raw didn't work. But Twig_Markup has a magic method, __toString() which is called, magically, when Twig_Markup is printed. So, I just had to invoke that directly. Which was as easy as: {% set buttonHtml = buttonHtml.__toString(buttonHtml) %) Happy coding! References: Twig_Markup API docs…
Read more about Casting Twig_Markup to string in TwigWell, that's because you can't. For whatever reason. I can't modify the variables in search-result.html.twig like so: function mytheme_preprocess_search_result(&$variables) { $variables['result']['testvar'] = 'beeeg test!'; $variables['result']['title'] = 'AMAAAAZING NEW TITLE!!!'; } Hmm. Okay. But I can access testvar like so in the twig file: {{ result.testvar }} And if I really want to change the title of my search results, I can do this: function mytheme_preprocess_item_list(&$vars) { foreach ($vars['items'] as &$item) { $res =& $item['value']['#result']; $res['title'] = 'bleaaargh!' ; } } And now all titles are set to 'bleaaargh!' as they should be…
Read more about Can't modify title in search-result.html.twig with template_preprocess_search_result()