First of all, I'm quite new in Drupal 7 and I've almost Zero knowledge in coding.

I've create a custom content type (Review - Machine Name: review) with 4 Fivestar Field :

  1. Support - Machine Name: field_support
  2. Performance - Machine Name: field_performance
  3. Value - Machine Name: field_value
  4. Overall - Machine Name: field_overall

I've also created 4 Fivestar Voting tag:

  1. rating_support
  2. rating_value
  3. rating_performance
  4. rating_overall

I want the Overall field to display the average of the other three Fivestar Field. I've followed exactly as written here.

Here is the code that I've used on my template.php:

 * Implements hook_form_alter().
 * Hide average fivestar field from review edit/create form.
function bartik_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'review_node_form') {
    // Hide average field, this field is calculated upon node_presave.
    $form['field_overall']['#access'] = FALSE;

 * Implements hook_node_presave().
 * Calculates average vote rating.
function bartik_node_presave($node) {
  if ($node->type == 'review') {
    // Get values from 3 axis.
    $rating_support      = entity_metadata_wrapper('node', $node)->field_support->value();
    $rating_value      = entity_metadata_wrapper('node', $node)->field_value->value();
    $rating_performance    = entity_metadata_wrapper('node', $node)->field_performance->value();

    // Calculate and set average axe.
    $rating_overall      = ($rating_support + $rating_value + $rating_performance) / 3;
    // @info: Can't use entity_metadata_wrapper, because '->set' not supported on this field.
    //entity_metadata_wrapper('node', $node)->field_overall->set($rating_overall);
    $node->field_overall['und'][0]['rating'] = $rating_overall;  

But The overall fivestar rating field displaying 'no votes yet' after publishing content. How can I fix this, Have I done something wrong?

Your Answer

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

Browse other questions tagged or ask your own question.