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 |
Site contents are licensed under a