By Gerben Zaagsma


2013-05-06 13:51:20 8 Comments

I use the Apache Solr Views module to create a View that displays only items from a specific content type.

I would like to include an image as well on the view. I managed to get the images on my site indexed in solr using a custom module (field is called ss_field_image_uri). They show up in the solr index and I can add the image field in my custom solr view. So far so good.

The problem is that the field outputs as "public://folder/imagename.jpg". My question is how how I could I get the view to output not the link but the image itself?

Should I somehow rewrite the output of the image field in the view? Or use a custom php field and add some code? I cannot figure out how to do it. I am not a programmer but any pointers as to how to proceed would be very much appreciated.

Best, Gerben

1 comments

@Gerben Zaagsma 2013-05-23 10:33:04

We managed to solve this with help from user Gaele Strootman.

There are three steps to solve this: 1) add the image to the solr index using a custom module 2) add the image field to your apache solr view 3) add a template file for the image field that displays the actual image

ad 1) create a custom module to add images to the solr index.

You only need two files, my_module.info and my_module.module. We name the image field ss_field_image_uri.

my.module.info:

name = Apache Solr Image
description = Add image field to the solr index so we can display images in search results
package = Apache Solr
core = 7.x

dependencies[] = apachesolr_search

my_module.module:

<?php
/**
 * Add field_image uri to apache solr index, so that we can later display image in search results or custom apache solr views page.
 * @see apachesolr.api.php
 */

function apachesolr_image_apachesolr_index_document_build($document, $node, $namespace) {
  if (isset($node->field_image['und']['0']['uri'])) $document->addField('ss_field_image_uri', $node->field_image['und']['0']['uri']);
}
?>

ad 2) add the image field to your apache solr view

First re-index solr so the images actually get indexed and show up. Then add the image field to your view.

ad 3) add a template file for the image field that displays the actual image

This is the template we added. Since the field is called ss_field_image_uri the template name is: views-view-field--ss-field-image-uri.tpl.php. This is the code:

<?php

/**
 * @file
 * This template is used to print a single field in a view.
 *
 * It is not actually used in default Views, as this is registered as a theme
 * function which has better performance. For single overrides, the template is
 * perfectly okay.
 *
 * Variables available:
 * - $view: The view object
 * - $field: The field handler object that can process the input
 * - $row: The raw SQL result that can be used
 * - $output: The processed output that will normally be used.
 *
 * When fetching output from the $row, this construct should be used:
 * $data = $row->{$field->field_alias}
 *
 * The above will guarantee that you'll always get the correct data,
 * regardless of any changes in the aliasing that might happen if
 * the view is modified.
 */
?>
<?php 
/**
 * For additional parameters see
 * http://api.drupal.org/api/drupal/modules!image!image.field.inc/function/theme_image_formatter/7 
 */
?>
<?php $variables = array('item' => array('uri' => $row->{$field->field_alias})); ?>
<?php $variables['image_style'] = 'thumbnail'; ?>
<?php print theme_image_formatter($variables); ?>

Now clean your caches and the images should show up in your apache solr view. Note that you can change the image style in the code from 'thumbnail' to whatever image style you like.

Hope this helps anyone!

Related Questions

Sponsored Content

3 Answered Questions

How to modify the search index?

  • 2014-02-27 20:21:09
  • Jack Ryan
  • 2712 View
  • 0 Score
  • 3 Answer
  • Tags:   search

2 Answered Questions

Adding CCK field to solr index

  • 2012-09-19 14:07:55
  • Chris Muench
  • 812 View
  • 0 Score
  • 2 Answer
  • Tags:   6 search

1 Answered Questions

[SOLVED] How can I retrieve indexed facet options for a particular field using Search API?

  • 2013-08-12 18:47:49
  • Alex Kirsten
  • 446 View
  • 1 Score
  • 1 Answer
  • Tags:   search

1 Answered Questions

[SOLVED] Adding custom field to apache solr drupal 7

  • 2015-09-29 07:11:31
  • user15837
  • 1040 View
  • 1 Score
  • 1 Answer
  • Tags:   search

1 Answered Questions

Apache Solr Search Results

  • 2014-02-04 14:06:13
  • Jack Ryan
  • 144 View
  • 0 Score
  • 1 Answer
  • Tags:   search

1 Answered Questions

[SOLVED] drupal could not index site using apache solr

  • 2013-09-16 10:07:01
  • Shirin Abdolahi
  • 453 View
  • 0 Score
  • 1 Answer
  • Tags:   search

Sponsored Content