Make a module use a custom node template
On my list of 2010 New Year's todo's, not necessarily resolutions, is to start a blog here. So let's jump right in.
I wanted to have better search for a new project I'm working on, but I'm not quite sure I need apache solr yet. So I found a Drupal 6 version of the Fuzzy Search module and started playing around.
One nice thing that any search should have is results that show why the item is a result. Fuzzy Search needed this, so I started tinkering.
After getting the output the way I wanted, it became clear I'd need a node template so the search results could be themed independently of other nodes.
First I added to hook_theme().
'fuzzysearch_result_view' => array(
'template' => 'fuzzysearch-result-view',
'arguments' => array('node' => NULL, 'teaser' => FALSE, 'page' => FALSE),
),Then I needed a preprocess_node function. It took me a while to figure out that it had to have the name of the template file appended to it. I pasted the default preprocess_node function into mine, removing only the template file assignment (at the bottom of the original function).
function fuzzysearch_preprocess_fuzzysearch_result_view(&$variables) {
$node = $variables['node'];
if (module_exists('taxonomy')) {
$variables['taxonomy'] = taxonomy_link('taxonomy terms', $node);
}
else {
$variables['taxonomy'] = array();
}
if ($variables['teaser'] && $node->teaser) {
$variables['content'] = $node->teaser;
}
elseif (isset($node->body)) {
$variables['content'] = $node->body;
}
else {
$variables['content'] = '';
}
$variables['date'] = format_date($node->created);
$variables['links'] = !empty($node->links) ? theme('links', $node->links, array('class' => 'links inline')) : '';
$variables['name'] = theme('username', $node);
$variables['node_url'] = url('node/'. $node->nid);
$variables['terms'] = theme('links', $variables['taxonomy'], array('class' => 'links inline'));
$variables['title'] = check_plain($node->title);
// Flatten the node object's member fields.
$variables = array_merge((array)$node, $variables);
// Display info only on certain node types.
if (theme_get_setting('toggle_node_info_'. $node->type)) {
$variables['submitted'] = theme('node_submitted', $node);
$variables['picture'] = theme_get_setting('toggle_node_user_picture') ? theme('user_picture', $node) : '';
}
else {
$variables['submitted'] = '';
$variables['picture'] = '';
}
}Here is how I call the search result node theme function. Note that $result is a node object.
theme('fuzzysearch_result_view', $result);Finally, I copied node.tpl.php to the fuzzysearch module directory and renamed it fuzzysearch-result-view.tpl.php. Now anyone who uses this module can theme search results as desired by copying that template again to the theme directory.
The module is not on the project page as of this post, but I hope to make it available soon.


Post new comment