By chefnelone

2012-11-26 17:18:05 8 Comments

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?


@aleksandr pilipenko 2017-03-23 12:16:49

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.

@Anoop Joseph 2012-12-04 05:06:47

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

@chefnelone 2012-12-05 13:56:49

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 2012-12-05 14:06:06

I see that the search_excerpt function remove all markup. Can this be change in some way?

@chefnelone 2012-12-06 10:53:07

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,..)

@gmclelland 2012-12-06 15:27:20

If you are using views with Apache Solr with and

You can enable highlighting on the word.

@beth 2012-12-05 15:18:58

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.

@chefnelone 2012-12-05 15:46:11

I get it working. Is javascript but it ok. Thanks.

Related Questions

Sponsored Content

2 Answered Questions

Select selection exposed filter using XML Views Backend

  • 2014-02-07 17:05:38
  • Don 01001100
  • 677 View
  • 1 Score
  • 2 Answer
  • Tags:   views

1 Answered Questions

How to create relevant keyword search using drupal 7 views

  • 2017-09-25 12:55:17
  • Sachin Tonte
  • 401 View
  • 0 Score
  • 1 Answer
  • Tags:   views search

1 Answered Questions

[SOLVED] Adding jQuery to a view (exposed filter: search terms)

1 Answered Questions

[SOLVED] Views: Use like operator in exposed search filter (Search: Search Terms)

  • 2013-10-11 15:02:16
  • turzifer
  • 3028 View
  • 3 Score
  • 1 Answer
  • Tags:   views

4 Answered Questions

[SOLVED] Views 'Search terms' exposed filter not working as expected

  • 2014-06-25 19:19:50
  • Vincent
  • 2033 View
  • 1 Score
  • 4 Answer
  • Tags:   views

1 Answered Questions

[SOLVED] Highlight the argument passed in the exposed filters

  • 2011-09-23 03:16:39
  • Hacker
  • 466 View
  • 1 Score
  • 1 Answer
  • Tags:   7 views

Sponsored Content