Can't modify title in search-result.html.twig with template_preprocess_search_result()


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

About the Author

Hi. My name is Jeremiah John. I'm a sf/f writer and activist.

I just completed a dystopian science fiction novel. I run a website which I created that connects farms with churches, mosques, and synagogues to buy fresh vegetables directly and distribute them on a sliding scale to those in need.

In 2003, I spent six months in prison for civil disobedience while working to close the School of the Americas, converting to Christianity, as one does, while I was in the clink.