Rate and review in one step
Oct 31, 2007 - This tutorial works but I've since found a more elegant way of doing it, so I recommend Rate and Review 2.
Purpose: This tutorial will show you one way of allowing users to rate and review a node all in the same form. This isn't the only way of doing it. If you are looking for a more drop in solution, see http://drupal.org/project/userreview . I went this route because I wanted to use fivestar and I already have CCK and views installed so all I needed was prepopulate.
Skill level: This tutorial is not aimed at beginners. You need to be comfortable with creating content types and theming them.
Support: Please use the support forum if you have questions and I will try to answer if I am able.
Modules needed: CCK, Views, Fivestar (as well as dependencies for those modules)
Note: In order for this to work, you need to make the following change to fivestar_field.inc:
Change
$items[$delta]['target'] = drupal eval($item['target']);
to
$items[$delta]['target'] = eval($item['target']);
The reason for this is that the $node object is not in scope in the evaluated PHP when using drupal_eval but it is with just eval. Hacking a module is uncool, so suggestions to get around that fix are welcome.
Step 1 - The review type:
- Create a node type named "Review"
- Get rid of the body field (makes theming easier)
- Do not enable fivestar (we will be adding it as a CCK field instead)
- Add the follwing fields:
- Review - Text area field
- ParentNID - Integer text field. This is a reference to the node ID of the node that is being reviewed.
- Rating - Fivestar field
- On Manage fields tab, configure the rating field. In the node id text box, put "return $node->field_parentnid[0][value];". Ignore the help text that says to surround it with PHP tags. You must have permission to use PHP for this in access control.
- Set access control to give permissions for create/edit of the review type
- Set up pathauto (if you use it) for review nodes.
Step 2 - Clean up the form:
If you have a site specific module, add this form_alter code:
<?php
function YOURMODULENAME_form_alter($form_id, &$form) {
switch ($form_id) {
// Only do this for the review type's form
case ('review_node_form'):
//If we are adding a review, we need to grab the parent node ID from the URL
if (arg(1) == "add") {
// Get the parent node's id from the url
$pnid = $_GET['pnid'];
// Set the parentnid field's value
$form['field_parentnid'][0]['value']['#value'] = $pnid;
} else {
// Otherwise, grab the existing parent node ID to redirect to
$pnid = $form['field_parentnid'][0]['value']['#default_value'];
}
// Hide the field
$form['field_parentnid'][0]['value']['#access'] = FALSE;
// On submission, redirect to the parent node
$form['#redirect'] = "node/$pnid";
// This is in case we add more forms later
break;
}
}
?>If you don't already have one and don't know how to make a module, just use the one in the zip.
Step 3 - Create a view:
- Add a new view called "reviews_of_node"
- Make it a page view
- Have it list teasers or full nodes (your choice)
- Add "parentnid" as an argument
- Sort by node updated time descending
Step 4 - Create node types that can be reviewed:
- Create or edit the node type for which you want reviews enabled
- Enable fivestar and set teaser and body both to hidden.
- To show the stars, put this in your node-type.tpl.php:
<?php
$current_rating = votingapi_get_voting_result('node', $nid, 'percent', 'vote', 'average');
$numstars = 5; // Change this to how many stars you want
print theme('fivestar_static', $current_rating->value, $numstars);
?> - To add link a to add a review, put this in your node-type.tpl.php:
<?php
print l("Add a review","node/add/review",NULL,"pnid=" . $node->nid,NULL,NULL,TRUE);
?> - To show the reviews, put this in your node-type.tpl.php at the end:
<?php
// Retrieve the view
$view = views_get_view('reviews_of_node');
// Build the view into the $reviews variable with paging on and nodes per page set to 20
$reviews = views_build_view('embed', $view, array(strval($node->nid)), true, 20);
// Get the number of reviews
global $pager_total_items;
$numreviews = $pager_total_items[0];
// Print the reviews
print "Number of reviews: $numreviews";
print $reviews
?>
Enjoy!
| Attachment | Size |
|---|---|
| rateandreview.zip | 817 bytes |
Comments
6 comments postedThis is great, thank you! Do you know how I could embed the add review form into the node page? I've tried drupal_get_form but keep getting parse errors for some reason.
I've never tried to embed a node entry form. No idea how you do it.
Michelle
Hi Michelle,
I was only interested in your "User Profiles" tutorial. After searching around drupal.org, then I see your name again that meet my requirement. I was just thinking about "fivestar + nodecomments" but you already adressed the issue.
http://drupal.org/node/166064"
I like to use fivestar, but can you tell me how is it compare to userreview module, since this also has similar voting widget?
My goal is to create multi-criterias rating and use "computed_field" module to get the average. Use the "activeedit" for inline editing. Then, display it on of the tab (JS tab) of the node as a "Views" display. And give userpoints to those who review/rate.
Before I start ...any words on this.
p/s : good thing I don't need a calculator to answer your captcha question! he he ..
Noticed that the ParentNID field is null in all my reviews, so eliminated it (mostly because I have not been able to understand yet how to 'theme it away', tho I've spent a few days digging through Drupal 5 documentation...). It does not seem to have created any issues. Is that because I'm using taxonomy terms? I also eliminated step 3, and things seem to work.
My main reason for asking these questions is to make sure my site will continue to work once it goes public. A close second reason, though, is to understand Drupal better - I've been digging through it for a month now, and still feel like I get very little!
Thanks for a very useful tutorial!!!
Hmm... Guess I forgot to turn off comments on book nodes... Anyway...
If you get rid of the parentnid, then there's not much point in using this tutorial. The whole point is to make a way of tying review nodes with the node being reviewed as though they were comments.
If you get rid of step 3, then you can't see the rating so, again, not much point...
But, if it's working for you, great :)
Michelle
interesting! my site is for products review (a community site for folks who participate in the same outdoor activity as a hobby). your tutorial produced the perfect way for members to review various products - but I guess since the reviews are related to products (which are listed via a hierarchical taxonomy of product-category/brand/model) instead of being related to nodes, it continues to work. Perhaps this is also why Step 3 is also not necessary? (Admittedly I don't understand the mechanics well enough to say!!)
Hope this is all positive feedback in your mind. Sounds like an untended (but very useful, in my opinion) application for your work! Would hate to be pestering you with these comments, but wanted to let you know - and perhaps get a few insights back that help me understand what's happening.