12

In a view and I added an exposed filter: Search: Search Terms filter

It works fine, but I'd like to highlight the searched keyword in the results.

How can I do it?

4
  • You can use tpl file for this.. Dec 3, 2012 at 9:42
  • yes, I guess that I should use a function to replace the searched keyword. But how do I fetch the searched keyword, and what if there are more than one keywords?
    – chefnelone
    Dec 3, 2012 at 13:24
  • If you can use jQuery there is a small tricky way to do it.
    – niksmac
    Dec 3, 2012 at 16:52
  • yeah think about it how to get.. you can use jquery as nikhil said.. :D Dec 4, 2012 at 4:04

5 Answers 5

7
+50

You could use something like highlight that finds and adds a class to all instances of a word on the page, and instead of feeding it a static word, you would use the contents of the search box.

You can find more information and links to other versions in the StackOverflow question, Highlight a word with jQuery.

1
  • I get it working. Is javascript but it ok. Thanks.
    – chefnelone
    Dec 5, 2012 at 15:46
11

Using search_excerpt function you can highlight the searched keyword. It show the keyword in bold.

Returns snippets from a piece of text, with certain keywords highlighted. Used for formatting search results.

An example is shown below,

for eg: //a body field is exposed here

in your views tpl file add the following code

 <?php foreach ($rows as $id => $row): 

      $keys = $_GET['body_value'];
      $row = search_excerpt($keys, $row);
 ?>

it will highlight the searched word, see the screenshot below

enter image description here

3
  • It does the job but it remove the structure of the view row. The view is showing content (not fields) as teaser. Once the search is done the result doesn't show the teaser but just the raw data. Can this be solved.
    – chefnelone
    Dec 5, 2012 at 13:56
  • I see that the search_excerpt function remove all markup. Can this be change in some way?
    – chefnelone
    Dec 5, 2012 at 14:06
  • I think this is the best option if the views is styled to show fields, but is not useful if the view is showing content (full, teaser,..)
    – chefnelone
    Dec 6, 2012 at 10:53
1

If you are using views with Apache Solr with http://drupal.org/project/search_api and http://drupal.org/project/search_api_solr

You can enable highlighting on the word.

0
function custom_views_pre_render(&$view) {
  if ($view->name == 'article') {
    $result = $view->result;
    foreach ($result as $i => $row) {
      $body = $view->result[$i]->field_body[0]['rendered']['#markup'];
      $strong = $view->exposed_data['body_value'];
      $view->result[$i]->field_body[0]['rendered']['#markup'] = str_replace($strong, '<strong>' . $strong . '</strong>', $body);
    }
  }
}

Example for body field. Into your custom module.

0

You could use a javascript plugin like mark.js.

Here's an example with jQuery :

$('.view-content').mark($('input').val());

Your Answer

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

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