1

The users on my website can rate an article with the Fivestar module while they comment. I'd like to create a view with the 5 articles that have the highest average score.

I've nearly been able to achieve what I want with a 'Vote results' relationship and a 'Vote results: Value' field. The problem is that the average score is displayed as a number, but I'd rather like it to be shown as stars. I achieved this in the past without custom coding (thanks to a deleted video that was situated here), but I forgot how to do it.

Can you help?

7
  • This has it for Drupal 6: drupal.stackexchange.com/questions/8200/…; I'd assume something similar is the case for Drupal 7
    – Jimajamma
    Mar 7, 2014 at 22:02
  • @Jimajamma: Thanks for your reply. Indeed, I've tried the 'Vote results' relationship too, but as you can read in my updated question, it didn't solve my problem completely.
    – Jeroen
    Mar 7, 2014 at 22:44
  • Ah! What you will have to do there is find out where in Views it is outputting that score/number and then feed that into the Five Star theme function that generates the stars from that number.
    – Jimajamma
    Mar 7, 2014 at 23:02
  • @Jimajamma: You're right. I think I succeeded in doing that in the past (only using the Views UI), but I can't remember how I did it... That's why I eventually started this question.
    – Jeroen
    Mar 7, 2014 at 23:07
  • I unfortunately don't have any D7 systems with 5star loaded on them to try to dig into it, but between the theme templates that the Views UI allows you to change and the theme functions in 5star, I am sure you'll be able to figure it out!
    – Jimajamma
    Mar 7, 2014 at 23:23

2 Answers 2

0

This is what I came up with:

Create a Views template for this specific field and put it in your theme (or module if you have Views templates already there and enabled via the Views api hook).

In my case it was called views-view-field--top-content--page-1--value.tpl.php for just the included and now enabled top content View.

In there, do the following:

<?php
  $data = $row->{$field->field_alias};
  $path = drupal_get_path('module', 'fivestar');
  print theme('fivestar_static', array('rating' => $data, 'widget' => array('name' => 'basic', 'css' => $path . '/widgets/basic/basic.css')));
?>

And if all is well in the world of copy and paste, you should now have a View with basic stars displayed instead of the number. If you don't like the basic stars, just look in the widgets directory and you can take your pick of any of the other styles there.

0

I finally rediscovered how I achieved this in the past. First, follow each step in this article and call the Fivestar field 'Stars'. Next, import the following view:

$view = new view();
$view->name = 'best_content';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'Best content';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/* Relationship: Content: Vote results */
$handler->display->display_options['relationships']['votingapi_cache']['id'] = 'votingapi_cache';
$handler->display->display_options['relationships']['votingapi_cache']['table'] = 'node';
$handler->display->display_options['relationships']['votingapi_cache']['field'] = 'votingapi_cache';
$handler->display->display_options['relationships']['votingapi_cache']['required'] = TRUE;
$handler->display->display_options['relationships']['votingapi_cache']['votingapi'] = array(
  'value_type' => 'percent',
  'tag' => 'vote',
  'function' => 'average',
);
/* Field: Content: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';
$handler->display->display_options['fields']['title']['label'] = '';
$handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;
/* Field: Field: Stars */
$handler->display->display_options['fields']['field_stars']['id'] = 'field_stars';
$handler->display->display_options['fields']['field_stars']['table'] = 'field_data_field_stars';
$handler->display->display_options['fields']['field_stars']['field'] = 'field_stars';
$handler->display->display_options['fields']['field_stars']['label'] = '';
$handler->display->display_options['fields']['field_stars']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['field_stars']['click_sort_column'] = 'rating';
$handler->display->display_options['fields']['field_stars']['settings'] = array(
  'widget' => array(
    'fivestar_widget' => 'sites/all/modules/fivestar/widgets/basic/basic.css',
  ),
  'expose' => 0,
  'style' => 'average',
  'text' => 'none',
);
/* Sort criterion: Field: Stars (field_stars:rating) */
$handler->display->display_options['sorts']['field_stars_rating']['id'] = 'field_stars_rating';
$handler->display->display_options['sorts']['field_stars_rating']['table'] = 'field_data_field_stars';
$handler->display->display_options['sorts']['field_stars_rating']['field'] = 'field_stars_rating';
$handler->display->display_options['sorts']['field_stars_rating']['order'] = 'DESC';
/* Sort criterion: Content: Post date */
$handler->display->display_options['sorts']['created']['id'] = 'created';
$handler->display->display_options['sorts']['created']['table'] = 'node';
$handler->display->display_options['sorts']['created']['field'] = 'created';
$handler->display->display_options['sorts']['created']['order'] = 'DESC';
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;

Done!


PS: This comment might explain why this didn't work before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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