Override URL in Drupal Search Results

By peterm, 7 December, 2010

Tags

I needed to come up with a solution for overriding the links in the search results for the campus map project. I've created a view that uses OpenLayers presets for getting a zoom level. In the view we use the standard node/%/map_detail path to get the detail view preset applied for the appropriate node nid.

I wanted to have the search results use the same pattern for getting the detail level information.  In my Blocks this was easy using the "output this field as link" setting within the view. I googled around, but didn't find much info that fit this particular need. Here's what I came up with that's working for my requirements. In template.php I've placed the following hook override.

 

/*

 * Implementation of hook preprocess_search_result()

 * 

 * This override allows us to insert 'content/%nid/map_detail 

 * into the path so that a search result can use the map_detail

 * view and associated openlayers preset for detail view.

 * 

 * $result is an array than contains the object $node. We need to get

 * the object into an array format so that we can loop over it for the nids

 */

function ucscv2_preprocess_search_result(&$variables) {

  $result = $variables['result'];

  // grab the node object

  $node_object = $result['node'];

  // create the $node array from the object

  $node = (array)$node_object;

  // we use the $base_url to give us the right URL

  global $base_url;

  // loop over the search results

  foreach ($variables['result'] as $result) {

// loop over the node array to get the correct nid for this iteration

foreach ($node as $key => $value) {

$variables['url'] = $base_url .'/content/' . $node['nid'] .  '/m

ap_detail';

}

}

}