The Fivestar module offers a great way to allow users to rate your content. Enabling this functionality for a node type was straight forward. My next goal was to show the fivestar widget in a view for each node row. Unfortunately, the row styling for my view was "Fields", so the fivestar widget would not appear. Apparently, the fivestar widget is only available for the teaser & full node display options.
No worries, you can always override the themable output of a view. Using the "Theme information" section on the view edit screen, I decided to create a new template file in my theme directory for "Row style output". For example:
views-view-fields--MY-VIEW-NAME.tpl.phpI then copied the contents of default view template into this file.
<?php
// $Id: views-view-fields.tpl.php,v 1.6 2008/09/24 22:48:21 merlinofchaos Exp $
/**
* @file views-view-fields.tpl.php
* Default simple view template to all the fields as a row.
*
* - $view: The view in use.
* - $fields: an array of $field objects. Each one contains:
* - $field->content: The output of the field.
* - $field->raw: The raw data for the field, if it exists. This is NOT output safe.
* - $field->class: The safe class id to use.
* - $field->handler: The Views field handler object controlling this field. Do not use
* var_export to dump this object, as it can't handle the recursion.
* - $field->inline: Whether or not the field should be inline.
* - $field->inline_html: either div or span based on the above flag.
* - $field->separator: an optional separator that may appear before a field.
* - $row: The raw result object from the query, with all data it fetched.
*
* @ingroup views_templates
*/
?>
<?php foreach ($fields as $id => $field): ?>
<?php if (!empty($field->separator)): ?>
<?php print $field->separator; ?>
<?php endif; ?>
<<?php print $field->inline_html;?> class="views-field-<?php print $field->class; ?>">
<?php if ($field->label): ?>
<label class="views-label-<?php print $field->class; ?>">
<?php print $field->label; ?>:
</label>
<?php endif; ?>
<?php
// $field->element_type is either SPAN or DIV depending upon whether or not
// the field is a 'block' element type or 'inline' element type.
?>
<<?php print $field->element_type; ?> class="field-content"><?php print $field->content; ?></<?php print $field->element_type; ?>>
</<?php print $field->inline_html;?>>
<?php endforeach; ?>By examining the hook_nodeapi() function of the Fivestar module, I decided to use the fivestar_widget_form() function to embed the widget in my view. This function accepts one argument, the $node object. Inside the first php tag in my new template file, I added the following code to load the node object:
<?php
$node = node_load($row->nid);
?>Last, I determined a good insertion point for the fivestar widget in this template file, and added the following code:
<?php
if (function_exists('fivestar_widget_form')) print fivestar_widget_form($node);
?>If done correctly the Fivestar widget should now be embedded in the view for each node row.









