0

I'm trying to display an image on the search api page result. I tried the following way: Displaying Images in Search Results, but I'm getting a "Notice: Undefined index: node...template.php" error. Off course after changing the preprocess function to theme_preprocess_search_api_page_result. Am I getting this error because the following preprocessor works only for the search module? Is there another way to add an image to the search api result?

I'm using the following modules version:

  • Search API 7.x-1.17
  • Solr search 7.x-1.10
  • Search Api Page 7.x-1.2

2 Answers 2

1

You first have to add the image to the index. Then preferably reindex. Usually for some reason the image fields are hidden on the page, where you add the fields to the index. The page in Drupal 7 would be under Search and metadata > Search API > Your index, edit > Fields /admin/config/search/search_api/index/NAME_OF_INDEX/fields

And your image field you may add from the bottom - the section is called "Add related fields" and then you should choose your field and click "Add field". Afterwards you may click the checkbox to the field - in order to add it to the index.

The approach with node_load is a very bad one because that would load stuff from the database, where the idea of Search Api and in this case the connection with Apache Solr - is to get everything directly from the Solr.

2
  • Yes your right did not think about it that way. I did what you suggested, but the images are not appearing on the result page. How can I print the image in the search_api_page_results.tpl.php? This is how i was displaying it: <img class="img-responsive" src="<?php print file_create_url($entity->field_image_biblio['und']['0']['uri']); ?>" /> I appreciate your help
    – Hicham
    Commented Aug 23, 2016 at 12:04
  • You have to add afterwards the image from the view mode. If you want to do it for a Content Type - Article for example - you would go under Structure > Content Types > Article > Manage display . Then if you did not activated the "Search result smth." viewmode - you may activate it form that page under "Custom display settings" and then you may go to that View Mode and add the image field that you want. Commented Aug 24, 2016 at 13:33
0

Found my mistake, such a newbie. Instead of 'node' index in the $variables['result']['node']; replace it with 'entity'.

But I ended up using a different approach by getting the image URL to be able to style the image as desired.

<img class="img-responsive" src="<?php print file_create_url($entity->field_image['und']['0']['uri']); ?>" />

And changed the function template file to:

$node = node_load($variables['result']['entity']->nid);
$node && ($variables['entity'] = $node);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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