It would be awesome to be able to show images in this block. I am working on a project for a large e-commerce site with Apache SOLR powering the product browsing. The more like this works really well, but the client really wants to show images in the block. I have been trying to get my imagefields indexed in SOLR with some limited success (see #627448: Display Images in Results).

Any help would be appreciated!

Sean

Comments

jpmckinney’s picture

Status: Active » Fixed

Can't you just write a custom implementation of the theme function: theme_apachesolr_mlt_recommendation_block?

pyrello’s picture

Haha... yeah. That is what I was trying to do. I spent a lot of time trying to figure out what all was stored in the $result variable, but a lot of it is serialized information. When I tried to unserialize it, it didn't seem to work.

I'll try to recreate the code I was working up. Maybe I was doing something obviously wrong and a second set of eyes could spot it.

Thanks for the response,
Sean

jpmckinney’s picture

Status: Fixed » Postponed (maintainer needs more info)

Here is the default implementation of the theme function:

http://drupalcontrib.org/api/function/theme_apachesolr_mlt_recommendatio...

pyrello’s picture

So, using this:

<?php
function theme_apachesolr_mlt_recommendation_block($docs) {
  $links = array();
  foreach ($docs as $result) {
    // Suitable for single-site mode.
    $links[] = l($result->title, $result->path, array('html' => TRUE));
  }
  return theme('item_list', $links);
}
?>

and presuming that I have a field getting indexed as $document->ss_image_path, how would I add the image?

Sean

jpmckinney’s picture

Try:

function MYTHEME_apachesolr_mlt_recommendation_block($docs) {
  $links = array();
  foreach ($docs as $result) {
    // Suitable for single-site mode.
    $links[] = l(theme('image', $result->ss_image_path, $result->title, $result->title), $result->path, array('html' => TRUE));
  }
  return theme('item_list', $links);
}

Not sure what your ss_image_path values looks like.

pyrello’s picture

Hello,

I really appreciate all of your help and patience. I implemented your code and am not getting it to work, but I am pretty sure it is because of #627448: Display Images in Results not working either. I have written a detailed post in that issue queue, since I believe it is the source of the issue and once I get that to work, I will have this working as well.

Sean

pyrello’s picture

I have since gotten this to work by implementing the final missing bit of code over at #627448: Display Images in Results which actually loads the indexed field into the $document object for output into search results, I think. Either way, it is working.

Thanks for all of your help, I really appreciate it. I plan on eventually releasing the custom code that was generated as an add-on module to make the integration between apache solr and ubercart much smoother.

Sean

pyrello’s picture

Status: Postponed (maintainer needs more info) » Fixed

And updating status...

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

guruslot’s picture

Status: Closed (fixed) » Active

Anyone did successful implementation of imagecache images into MLT block?

Thanks

pyrello’s picture

@Guruslot - here is the snippet that we are using to override the output via template.php in our custom theme:

function YOUR_THEME_apachesolr_mlt_recommendation_block($docs) {
  $links = array();
	
	$num = 0;

  foreach ($docs as $result) {
		$node = node_load($result->nid);
		$left = ($num % 3);
		if ($left === 0) {
			$output .= '<div class="row grid-12 alpha omega">';
		}
		if ($left === 0) {
			$output .= '<div class="grid-4 alpha">';
		} else if ($left == 2) { 
			$output .= '<div class="grid-4 omega">'; 
		}  else {
			$output .= '<div class="grid-4">';
		}
		$output .= node_view($node, TRUE, FALSE, FALSE);
		$output .= '</div>';
    $num++;
		if ($left == 2) { 
			$output .= '</div>'; 
		}
  }
	
  return $output;
}

The relevant part is that we are using node_load() and node_view() to output these as teasers. The rest of it is just styling it to work with the grid we are using for our custom theme.

guruslot’s picture

Thank you very much, Pyrello.

Your snippet really works. But it outputs a complete node teasers. My goal is to have a small MLT's block with thumbnails (using imagecache) and node titles.

And I tried to use a snippet #4. It works. But it outputs only titles. I'd like add only thumbnails to it.

So, I did the following code:

function sky_apachesolr_mlt_recommendation_block($docs) {
    $links = array();
    foreach ($docs as $result) {
if (isset($result->sm_field_image[0])) {
    $links[] = l(theme('imagecache', 'thumb', $result->sm_field_image[0], $result->title), $result->path, array('html' => TRUE));
        $links[] = l($result->title, $result->path, array('html' => TRUE));
        }
    }
    return theme('item_list', $links);
}

It works, but there is one issue - it separates as list with <li> </li> thumbnails and titles. The first $links[] statement does not print title...
Could you advice on it, please?

pyrello’s picture

@Guruslot - this is because you are adding to the $links array 2 times every time the foreach loop cycles:

if (isset($result->sm_field_image[0])) {
  $links[] = l(theme('imagecache', 'thumb', $result->sm_field_image[0], $result->title), $result->path, array('html' => TRUE));
  $links[] = l($result->title, $result->path, array('html' => TRUE));
}

When you theme the $links array using 'list_item', it takes each item in the links array and turns that into an <li></li> element. I haven't tested it, but I think the following code should work:

if (isset($result->sm_field_image[0])) {
  $links[] = l(theme('imagecache', 'thumb', $result->sm_field_image[0], $result->title), $result->path, array('html' => TRUE)) . l($result->title, $result->path, array('html' => TRUE));
}
guruslot’s picture

It works, confirm! Thank you so much, Pyrello!

nick_vh’s picture

Status: Active » Fixed
nick_vh’s picture

Status: Fixed » Closed (fixed)
brephraim’s picture

This doesn't work for me; I assume this is because the code is written for 6.x whereas I am running 7.x.

Does anybody have a successful implementation for 7.x?

pyrello’s picture

@brephraim - You are correct that it doesn't work because the code has changed for 7.x. But the approach is still the same. You'll need to override the same theme function in your template.php file. The 7.x version of this theme function is:

function MYTHEME_apachesolr_search_mlt_recommendation_block($vars) {
  $docs = $vars['docs'];
  $links = array();
  foreach ($docs as $result) {
    // Suitable for single-site mode. Label is already safe.
    $links[] = l($result->label, $result->path, array('html' => TRUE));
  }
  $links = array(
    '#theme' => 'item_list',
    '#items' => $links,
  );
  return render($links);
}

I would start with that in your theme. I am not as familiar with how the render functions work and how they are different from the theme functions that were used before (ex. theme('item_list', $links)), so I am not sure exactly how to update it, but that is the route I would take.