Rate and review 2
Posted 10/31/2007 - 09:03 by Michelle
Aug 26, 2008: A recent user of this tutorial wrote some updates here.
Note: This tutorial was written before fivestar gained the ability to use comments for rating. It's still useful if you want your reviews to be nodes.
This is a revised version of Rate and review in one step using the node relativity module. (Thanks to greggles for pointing this module out.)
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 .
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, and Node Relativity (as well as dependencies for those modules)
Step 1 - Modify fivestar:
Fivestar uses drupal_eval() to determine the target node for the voting widget. Unfortunately, the $node object is not in scope in drupal_eval() and we need to pull the target from the $node object. The fix is to make the following change to fivestar_field.inc:
Change
$items[$delta]['target'] = drupal eval($item['target']);
to
$items[$delta]['target'] = eval($item['target']);
Hacking a module is uncool, so suggestions to get around that fix are welcome.
Step 2 - Create node type(s) 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);
?> - Node Relativity will automatically add a link to add a child node. If you want to place it where you want, turn that off in settings and put this in your node-type.tpl.php:
<?php
print l("node/add/review/parent/$node->nid",NULL,NULL,NULL,FALSE,TRUE);
?> - Node Relativity will automatically list the child nodes but you can add this code to your node-type.tpl.php to place them manually:
<?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
?>
Step 3 - 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
- Rating - Fivestar field
- On Manage fields tab, configure the rating field. In the node id text box, put "return $node->parent_node;". 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. Note that the relativity module adds tokens to access the parent node in pathauto such as [parent-title]
Step 4 - 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)
- Set empty text to "No reviews found."
- Add "Relativity: Parent Node ID" as an argument and use empty text
- Sort by node updated time descending
Step 5 - Putting it all together with Node Relativity:
- Go to example.com?=admin/settings/relativity [screenshot]
- For the question "Which node types should relationships be managed for?", select all node types for which you are enabling reviewing. In the screenshot, you can see we have a node type of "general" that will be reviewed. You will also need to select "Review" here.
- For each type you selected in 1 (except review), set "Parental Ordinality" to none and "Allowable Child Node types" to "Review"
- Under "Options for Review (review) nodes", set "Parental Ordinality" to one and "Allowable Child Node types" to "none"
- Go to example.com?=admin/settings/relativity/advanced [screenshot]
- Check "Enforce Parental Rules"
- Go to example.com?=admin/settings/relativity/display [screenshot]
- For each content type for which you enabled reviews, set "Rendering option for children nodes of type Review" to the view you created in step 3. In our example, we have set the option for the general content type to "reviews_of_node"
Step 6 - Set up redirection:
When submitting the review, it goes to the review view page. It makes more sense to go back to the node being reviewed. If you have already 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'):
// On submission, redirect to the parent node
$pnid = arg(4);
$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.
Enjoy!
| Attachment | Size |
|---|---|
| rateandreview.zip | 745 bytes |
| rate-review-screen-1.jpg | 63.17 KB |
| rate-review-screen-2.jpg | 75.38 KB |
| rate-review-screen-3.jpg | 63.21 KB |
Site contents are licensed under a