Hello, I would like to show a search result "snippet" and highlight searched terms using an exposed filter, similar to the core search results. Has anyone done this?

Comments

rickmanelius’s picture

I'm not sure exactly. But here's how one could approach it.

  • Set the format to 'fields'
  • Add node title and node body to the fields
  • Create a template override for views-view-field--your-view--body.tpl.php
  • In that template file, you'll have to perform a regular expression to find the exposed keyword and then replaced it with bold or emphasis tags.

There are probably some other caveats (I'm not sure how easy it would be to do other fields, you have to grab the search term from the view object, etc). But that would be a start.

dawehner’s picture

Status: Active » Fixed

#1 sounds like a good plan.

In general this needs custom code at the moment, so good luck with it.
Perhaps you could create some kind of contrib module which everyone can use.

cwyant’s picture

I was hoping there was an easier way. Basically, I want it to appear similar to the core search results. Can I borrow some code from the search template?

dawehner’s picture

Noone prevent you from doing it.

cwyant’s picture

This is somewhat beyond me. It's not a big deal, but I would appreciate some help if anyone has the time.

rickmanelius’s picture

Sorry cwyant. I have no use for such a use case and it's way more work than I'm willing to donate. If it's beyond your skills, you can always try to hire it out.

Status: Fixed » Closed (fixed)

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

quickly’s picture

this is a really useful feature... I wonder why it is not implemented in views... This is such a common requirement, someone should provide a solution to it...

jasonawant’s picture

Hello,

I've put together a rough solution by modifying the following blog post suggestion.

http://www.bigcitydev.com/node/18

1) Create the node template file using your view's row style template name suggestion. For example, if your view name is 'blog_search', create a node template like 'node--view--blog_search.tpl.php'
2) Add a snippet at top of the new node template file to 1) grab the keys from the url query string and 2) send which ever fields you want through the search_excerpt() function using the keys.

Here's an example sending the body through the function.

$keys = $_GET['keys'];
$content['body'][0]['#markup'] = search_excerpt($keys, $content['body'][0]['#markup']);

You could also use this in a view field template like comment#1 suggested, but use the search_exerpt() instead of a regular expression.

Hope that helps someone. I'm sure a more elegant solution can be made.
Jason.

chefnelone’s picture

I also think this is very useful feature which would be nice to get it working in a simpler way.

nicolas bouteille’s picture

Version: 7.x-3.0-rc1 » 7.x-3.5
Component: exposed filters » search data
Category: support » feature
Status: Closed (fixed) » Active

Definitely a must according to me too, and not fixed at all as far as I know even though I'm impatient to try solution #9.
Updating the version to current one, switching it back to active, to feature request, to search data.

nicolas bouteille’s picture

I'd like to add that solutions #1 and #9 were what worked for me for now. Still think this should be a field available on the UI though.

dawehner’s picture

Does someone know whether core search provide that kind of feature?

gChen07’s picture

Not fixed at all.. This feature should really be in all search result pages.. I'm thinking about the "Keyword Highlighter" module where you can select text to be highlight.. I think it is possible to just make selected text equal to the search text for the search result to be highlighten also.. Just can't find the right file code to do so.. please help

wOOge’s picture

Relating to post #9 — here is some updated code to go into your views-view-field--VIEWNAME--VIEWDISPLAY--field-body.tpl:

<?php 
$keys = $_GET['keys'];
print search_excerpt($keys, $row->field_field_body[0]['rendered']['#markup']);
?>
demonde’s picture

I suggest to use these variables working for search filter & argument:

<?php
if (isset($view->filter['keys']->value)):
  $keys = $view->filter['keys']->value;
elseif (isset($view->argument['keys']->argument)):
  $keys = $view->argument['keys']->argument;
endif;

if(isset($row->field_field_text_source[0]['raw']['value'])):
  $text = $row->field_field_text_source[0]['raw']['value'];
endif;
if (isset($keys) && isset($text)):
  print search_excerpt($keys, $text);
endif;
//dpm($view);
?>