On our directory theme BizReview showing listings and their reviews, we need a rating mechanism so users can leave their votes on comments and the parent listings will calculate average ratings.
We believe this rating mechanism is popular among listing and shopping websites, so we wrote a tutorial for setting it up on Drupal 9 (and probably Drupal 10).
The good news is this kind of setup is quite straightforward in Drupal using Fivestar module.
1. Install Fivestar and its dependencies
Install Fivestar according to the module guide here: https://www.drupal.org/project/fivestar
When we wrote this tutorial on 2023, Fivestar officially supports Drupal 9. We tested it on Drupal 10 and found it is working properly.
Pls note that when you install it using composer, it will automatically install required libraries and modules, in this case, Voting API module as well.
Pls enable Fivestar on the admin interface, or use "drush en fivestar".
2. Create a rating field on parent nodes
On your chosen content type, pls create a new field of type Fivestar rating, name it "field_rating" for example.
On the field setting form, pls choose "Rated while editing" to prevent users to vote on the node directly without using comments.
3. Create a rating field on comments
On /admin/structure/comment and your chosen comment type, please create a new field of type type Fivestar rating as above.
Now it is the interesting part, on the field setting form, please check the option "Set voting target" and type the following values:
- Target bridge field: entity_id (this is very important, so comments' ratings will contribute towards parent nodes)
- Target fivestar field: field_rating (the parent rating field you created above)
4. Test the results:
Now add some comments with votes, the parent node will show the average rating.
Good luck!