background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount

Embedding the Fivestar widget in a view

Eric.London's picture

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.php

I 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.

Thanks!

Just came across this now and it was super helpful! Much appreciated Eric!

Just use this on an individual field

This helped me so much. However I wanted to use the Views default table plugin so I just applied your two lines of custom code to a tpl.php file targeting an individual field in my view. I used a very specific template file: views-view-field--my-view-name--field-my-field-name-value.tpl.php Then put the code inside looked like this:

<?php
/**
  * This template is used to print a single field in a view. It is not
  * actually used in default Views, as this is registered as a theme
  * function which has better performance. For single overrides, the
  * template is perfectly okay.
  *
  * Variables available:
  * - $view: The view object
  * - $field: The field handler object that can process the input
  * - $row: The raw SQL result that can be used
  * - $output: The processed output that will normally be used.
  *
  * When fetching output from the $row, this construct should be used:
  * $data = $row->{$field->field_alias}
  *
  * The above will guarantee that you'll always get the correct data,
  * regardless of any changes in the aliasing that might happen if
  * the view is modified.
  */
  // custom code

$node = node_load($row->nid);
?>

<?php
print $output;
?>

<?php
// custom code
if (function_exists('fivestar_widget_form')) print fivestar_widget_form($node);
?>

Thanks

Its working fine...Thanks A Lot - Eric

Hello, I did what you explain

Hello,

I did what you explain above and it works perfectly, but I want to show the fivestar in every row but not in the first one (the first row is an attachment view). How can I do it?

Thanks.

Five star

Hi,

I'm using the Fivestar module and I'm trying to set the "Display" mode to be Inline for the "Label", and this does not seem to work. Meaning, I get the label on one line, and the five stars on the next. can anyone help

Five star

I cant get the user to rate the content by fiveStar... you the user edit the content he himself can rate it,but no other user can. not even the admin. i have given all the permissions required but still no use...

any idea what im doing wrong?

There is a...

Hi there.

There is a cheaper way to do this, I've changed it and works OK.

Change

$node = node_load($row->nid);

for

$node->nid = $row->nid;

Not sure if this would work ok for all php versions, or have any other issues, since I am not an expert.

Thank you for this! And for the blog, in general.

Cheers!
Droope

Eric.London's picture

putting it all together

I should have included this in my original post (thanks for the heads up Col!). Here's all the code put together. Keep in mind that you'll need to improve the layout and CSS.

<?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
*/

// custom code
$node = node_load($row->nid);

?>

<?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; ?>

<?php
// custom code
if (function_exists('fivestar_widget_form')) print fivestar_widget_form($node);
?>

vote not saved

Unfortunatly on my site the vote is not saved. After I refress the site, appear ""no votes yet"
Lazar

Problem

First, thank you for this tutorial.

I done what you suggest, but i have problems with voting. Idea is to show only voting summary on this view, not ability to post vote for public or register user.

I used this code:

$node = node_load($row->nid);
if (function_exists('fivestar_widget_form')) print fivestar_widget_form($node);

Can you tell how can i show just voting summary for this view?

Thank you.

Tutorial from Lullabot

I've followed this tutorial from the great folks at Lullabot for using fivestar/votingAPI with Views: http://www.lullabot.com/node/425/play - I found this really helpful.

easy as pie

Thanks - that worked great. Seems like there should be an easier way.. but..

Selwyn