User login

Preprocess from a module: removing the content author from Drupal 7 search results

All, not just most, but all the content on a particular site was set to be posted without showing the by-line or submitted by text. Given this requirement, it makes no sense to have search results show the user that submitted the content, every time. This can be themed away in search-result.tpl.php or it can be removed with a pre-process function as shown below.

You can do this from a module or a theme, where 'example' is the system name of that module or theme:

<?php
/**
 * Override template_search_preprocess_search_result() to not show username.
 */
function example_preprocess_search_result(&$variables) {
  global
$language;

 

$result = $variables['result'];
 
$variables['url'] = check_url($result['link']);
 
$variables['title'] = check_plain($result['title']);
  if (isset(
$result['language']) && $result['language'] != $language->language && $result['language'] != LANGUAGE_NONE) {
   
$variables['title_attributes_array']['xml:lang'] = $result['language'];
   
$variables['content_attributes_array']['xml:lang'] = $result['language'];
  }

 

$info = array();
  if (!empty(
$result['module'])) {
   
$info['module'] = check_plain($result['module']);
  }
  if (!empty(
$result['date'])) {
   
$info['date'] = format_date($result['date'], 'short');
  }
  if (isset(
$result['extra']) && is_array($result['extra'])) {
   
$info = array_merge($info, $result['extra']);
  }
 
// Check for existence. User search does not include snippets.
 
$variables['snippet'] = isset($result['snippet']) ? $result['snippet'] : '';
 
// Provide separated and grouped meta information..
 
$variables['info_split'] = $info;
 
$variables['info'] = implode(' - ', $info);
 
$variables['theme_hook_suggestions'][] = 'search_result__' . $variables['module'];
}
?>

See also, for an example of using both preprocess and the template:
http://www.midwesternmac.com/blogs/jeff-geerling/adding-images-search-results

Searched words: 
drupal 7 take over a preprocess function preprocess from a module drupal 7 remove user-submitted byline from search results drupal search result build mode has no effect prepprocess

Comments

Better solution with a 1 line function

I found this page, then a simpler way to achieve the same result, which I published here:
http://drupal.org/node/1101554#comment-5345554

That is not the same

That is not the same result.

It completely removes the post info in search results, while the function above merely removes the author's name, while still showing the post date and content type. A different result, which was very helpful to me!

hook_preprocess_search_result

hook_preprocess_search_result happens as well as template_preprocess_search_result, not instead of - it's not a theme override, it's more of a hook_alter type of a thing. That means the $variables array already includes info_split with all the values calculated here. Rather than starting from first principles you can just do this:

<?php
/**
* Implements hook_preprocess_search_result().
*/
function example_preprocess_search_result(&$variables) {
  unset($variables['info_split']['user']);
  $variables['info'] = implode(' - ', $variables['info_split']);
}
?>

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.