I need to display a multi-axis review system using Fivestar in node.tpl.php in Drupal 7.

I created the content type according to this tutorial, which is for Drupal 6, and I need to know how I can call votingapi_select_results() and fivestar_theme() in Drupal 7.

Initially, I tried the following code.

$design_rating = votingapi_select_results(array('entity_id' => $node->nid, 'tag' =>'design', 'function' => 'average'));
print_r($design_rating );

It caused a fatal error.

Later, I used fivestar_get_votes() as in the following code.

$design = fivestar_get_votes('node', $node->nid, 'design', NULL);

I got an array.

  Array (
    [average] => Array (
      [vote_cache_id] => 128
      [entity_type] => node
      [entity_id] => 18
      [value] => 80
      [value_type] => percent
      [tag] => design
      [function] => average
      [timestamp] => 1332162918
    [count] => Array(
      [vote_cache_id] => 127
      [entity_type] => node
      [entity_id] => 18
      [value] => 1
      [value_type] => percent
      [tag] => design
      [function] => count
      [timestamp] => 1332162918
    [user] => Array(
      [vote_id] => 8
      [entity_type] => node
      [entity_id] => 18
      [value] => 80
      [value_type] => percent
      [tag] => design
      [uid] => 1
      [timestamp] => 1332162918
      [vote_source] =>
      [function] => user

I don't know how to use it to show the result shown from the Fivestar module. How can I do it?

i tried with below code in node.tpl.php and it displayed result as '3' not a fivestar widget.

echo theme('fivestar_static',$variables); 

Am i missing anything in it? or is there any other procedure to call theme function in node.tpl.php in drupal 7?

  • You normally don't need to call fivestar_theme() as it is a hook implementation; if you really need to call that function, you just call it as any PHP function. – kiamlaluno Mar 19 '12 at 14:30
  • from memory, the only thing different between theme functions in drupal 6 and 7 is that 6 uses individual arguments while 7 uses an array for the arguments, so I'd imagine you would just call theme('fivestar_whatever', $args) instead of theme('fivestar_whatever', $a1, $a2, $a3 etc) – Jimajamma Mar 19 '12 at 16:46

Fivestar module for Drupal 7 implements fivestar as a field. Assuming the field name is field_fivestar_awesomeness you can print it anywhere with:

$fivestar = field_view_field('node', $node, 'field_fivestar_awesomeness');
print render($fivestar);

There are some display and language options, see more in the api docs.

  • i created multiaxis rating system , so i am trying to bring the fivestar rating of another content type[review content type] into the parent content type[product content type].So how can i display it in parent content type node.tpl.php? – Moorthy Mar 21 '12 at 12:21
  • let's say you know the nid of the review node, load it and pass it to field_view_field. there is no secret here. in any case, the views module is your friend here. – Capi Etheriel Mar 22 '12 at 14:32

You can use fivestar_custom_widget() from the Fivestar module

  1. Create rate from node

    $rating = votingapi_select_single_result_value(
        'entity_id' => $node->nid,
        'entity_type' => 'node',
        'tag' => 'vote',
        'function' => 'average',

2 Create form

$vote = drupal_get_form('fivestar_custom_widget', array('average'=>$rating), $settings);


$settings = array( 'stars' => 5, 'tag' => 'staff_vote');

see more in fivestar.module

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.