I need to customize the search results of a drupal 8 (8.7.0 currently) page a bit and need the node_type for this. According to search-result.html.twig, this should be stored in info_split.type, but the whole variable is empty.

How can I get the node type inside the search-result.html.twig file?


Alright, so I'm still not sure why the fields are empty, but they the keys are certainly there. Either it is a bug in the core, or some weird behaviour of my installations.

I fixed it now manually by implementing the preprocess_search_result hook in mytheme.theme:

function mytheme_preprocess_search_result(&$variables) {

    $variables['type'] = $variables['result']['node']->get('type')->getString();
    $variables['nid'] = $variables['result']['node']->get('nid')->getString();

    switch ($variables['type']) {
        // some other adjustments based on the node tpye 
  • maybe as a sidenote - I was a bit weary about a performance drop by this, but it is not noticable
    – Andreas
    May 9 '19 at 0:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.