Entity Reference will, in time, replace the references module.
So it would be great if fivestar could support it.

Files: 
CommentFileSizeAuthor
#20 Contact teacher2 Mudarrisi.png347.22 KBmurtza
#15 entityreference-support-1488914-15.patch1.98 KBcafuego
PASSED: [[SimpleTest]]: [MySQL] 115 pass(es).
[ View ]
#8 Screen Shot 2012-04-17 at 10.14.36 PM.png24.83 KBnafmarcus
#6 fivestar-support_entityreference_1488914-6.patch1.75 KBCmKeen
PASSED: [[SimpleTest]]: [MySQL] 115 pass(es).
[ View ]

Comments

Agree, should be rather easy to write.

I have exactly the same need.
If someone could guide me, I'm willing to try to patch this one.

I made some tests and I managed to link the rating field from my review content type to the main content type.
However, whenever I create a new review with a rating, the rating field on the main content type gets the new value and don't add the different votes. I don't really know what I did wrong. Maybe someone can help review my changes.
Here there are:

Here is the place where I add the entityreference link

<?php
/**
* Implements hook_fivestar_target_info().
*/
function fivestar_fivestar_target_info($field, $instance) {
 
$entity_type = $instance['entity_type'];
 
$bundle = $instance['bundle'];
 
$options = array(
   
'none' => array(
     
'title' => t('None'),
    ),
  );
 
// Add node_referrence support.
 
if (module_exists('node_reference')) {
   
$field_names = array_keys(field_read_fields(array('module' => 'node_reference')));
    if (!empty(
$field_names)) {
     
$field_instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle, 'field_name' => $field_names));
      if (!empty(
$field_instances)) {
        foreach (
$field_instances as $field_instance) {
         
$options[$field_instance['field_name']] = array(
           
'title' => t('Node reference: @field', array('@field' => $field_instance['field_name'])),
           
'callback' => '_fivestar_target_node_reference'
         
);
        }
      }
    }
  }
   
// Add entityreference support.
 
if (module_exists('entityreference')) {
   
$field_names = array_keys(field_read_fields(array('module' => 'entityreference')));
    if (!empty(
$field_names)) {
     
$field_instances = field_read_instances(array('entity_type' => $entity_type, 'bundle' => $bundle, 'field_name' => $field_names));
      if (!empty(
$field_instances)) {
        foreach (
$field_instances as $field_instance) {
         
$options[$field_instance['field_name']] = array(
           
'title' => t('Entity reference: @field', array('@field' => $field_instance['field_name'])),
           
'callback' => '_fivestar_target_entityreference'
         
);
        }
      }
    }
  }
 
// Add comment module support.
 
if ($instance['entity_type'] == 'comment') {
   
$options['parent_node'] = array(
     
'title' => t('Parent Node'),
     
'callback' => '_fivestar_target_comment_parent_node'
   
);
  }
  return
$options;
}
?>

And here is the callback function

<?php
/**
*
* @return (array) array('entity_type', 'entity_id')
*/
function _fivestar_target_entityreference($entity, $field, $instance, $langcode) {
 
$target = array();
 
$entityreference = $instance['settings']['target'];
  if (isset(
$entity->{$entityreference}[$langcode][0]) && is_numeric($entity->{$entityreference}[$langcode][0]['target_id'])) {
   
$target['entity_id'] = $entity->{$entityreference}[$langcode][0]['target_id'];
   
$target['entity_type'] = 'node';
  }
  return
$target;
}
?>

OK, now I know.
In fact it works as planned. The last rating of a specific user overwrite the previous one; but if another user creates a new rating, it works flawlessly.

So, my modification works perfectly.

Can someone help me to create a real patch of it?

Tx.

If you have git installed its really easy, see: http://drupal.org/node/707484

Status:Active» Needs review
StatusFileSize
new1.75 KB
PASSED: [[SimpleTest]]: [MySQL] 115 pass(es).
[ View ]

Here is the patch.
Hope I got it right

One first attempt, looks good. Thanks so much!

StatusFileSize
new24.83 KB

I altered the registration form and added my own form with field of type fivestar.....
$form['review_overall_rating'] = array(
'#type' => 'fivestar',
'#title' => t('General rating'),
'#required' => TRUE,
'#stars' => 5,
'#weight' => -45,
);

After the user clicks "Register", I create a node and copy the rating field

function gitb_reviews_user_insert(&$edit, $account, $category) {
Create $node object.
...assign some fields
$node->field_overall_rating['und']['0']['rating'] = $edit['review_overall_rating'];
....assign some more fields
call node_save($node)
}

I receive the following error (see attached file):
"Notice: Undefined index: target_id in fivestar_target_entityreference() (line 804 of /User/..../contrib/fivestar/fivestar.module)

I don't quite understand the fivestar module well enough to know if this is related to supporting entityreference but it seems like it might be.

Status:Needs review» Reviewed & tested by the community

Thank you this is very close to the solution am looking for in my project. The patch is working perfectly when i am using references module and fivestar along with this patch.

Can any one tell me why there is no data being added to field target. Why is it always null in the database, what am i missing or doing wrong ?

Status:Reviewed & tested by the community» Needs work

Looks like it needs work based on everyones comments above.

+++ b/fivestar.moduleundefined
@@ -775,6 +791,22 @@ function _fivestar_target_node_reference($entity, $field, $instance, $langcode)
+  if (isset($entity->{$entityreference}[$langcode][0]) && is_numeric($entity->{$entityreference}[$langcode][0]['target_id'])) {

The isset is checking for the key of 0, yet the is_numeric checks the target_id. The is_numeric is going to throw a notice if target_id isn't set which is what it should be checking in the isset before. Which I'm guessing is also the problem with the comment in #8.

At least part of the error was mine. I had not initialized the entity reference correctly in the node.

In your code you set target to node:
$target['entity_type'] = 'node';
But entity could not be only 'node', I'm using Entity Reference module to set reference to user. Is it possible set a real entity type which I referenced to?

I made a quick test, and it seams to me that the target_id field is also NULL if you use node_reference module. The vote is stored right in the votingapi_vote table.

Status:Needs work» Needs review
StatusFileSize
new1.98 KB
PASSED: [[SimpleTest]]: [MySQL] 115 pass(es).
[ View ]

Attached patch contains all changes from #6 by CmKeen and has an extra data check to avoid the warning seen by nafmarcus in #8.

Additionally, the _fivestar_target_entityreference() callback now loads the targeted entity type information so the correct entity type is stored with the vote in the votingapi_vote table.

Please test! :-)

First - very grateful to all working to bring entity reference support to fivestar :)

Now the issue... In short, after patch in #15 voting target apparently does not receive votes. In detail,

Setup:
- Drupal 7.18
- Entity Reference 7.x-1.0
- Fivestar7.x-2.0-alpha2+5-dev (+#15 patch)
- Voting API 7.x-2.10

Workflow:
1) Created "Explanation" content type
2) Created "Explanation Peer Review" content type with ER field "field_explanation_review_parent" to "Explanation" bundle
3) For both content types: Added fivestar field "Evidential", (vote while editing), tag "explanation_evidential"
4) Applied #15 patch and am now given "Voting target" option on fivestar field settings which displays my entity reference field
5) Selected the only ER field I created

Results:
- "Explanation" (parent entity) fivestar field shows 0 votes
- Target column in table "field_data_field_evidential" is NULL

I have tried typical end user troubleshooting: re-patching and starting over, deleting and recreating the ER field and fivestar fields, clearing cache, attempted w/wo Entityreference prepopulate enabled on ER field.

I apologize I do not have the necessary coding experience to shed more light or possible solutions on the issue.

Thanks again!

Levi

Hmm, on my install (only referencing nodes) the votes all end up in the {votingapi_vote} table and I can access those votes via Views. Can you check if there is anything in that table on your install? When you view the content with the vote, are stars or a percentage rendered ok?

You are correct, voting on the child node correctly stores the vote data in the {votingapi_vote} table with the parent NID. This is accessible through views as well. When viewing the child content the fivestar field is rendered correctly also.

However, I find no way to display the Voting API voting results using fivestar? The target column in the fivestar table still shows NULL. Is this by design?

Below deviates from the original issue, please let me know if/where I should move it.

My goal was to achieve the same effect as described in the documentation here Fivestar 7.x: Creating a User Comment/Rating and Average Rating for a Content type except using my own custom review content type instead of comments. In this tutorial, comments and their node parent shared the same fivestar field as well as the same field data. This allowed me to configure the comments to show "user's vote" and the node to show "average vote". When votes were cast via comments, both the comment and the parent could display the data using fivestar, very cool. This does not seem possible when retrieving data from {votingapi_vote} through views.

Your comment did inspire a solution which produces the same result as the tutorial above, except using views and panels. Hopefully this helps others too:

  1. create a view of the content type being reviewed (parent)
  2. add a content pane display
  3. add contextual filter on NID
  4. set argument input under pane settings to Content Nid Source: From panel argument, Panel argument: First
  5. add a relationship to the review (child) content type's ER field "Entity Reference: Referencing entity"
  6. add the review (child) content type's fivestar field using the relationship above (now we have the fivestar vote data we need)
  7. enable aggregation in views under advanced
  8. set aggreation settings on the fivestar field to Aggrreagation type: Average, Group column: Entity ID, Group Columns (additional): check the box on "Rating"
  9. add your view pane to the panel for the parent content

This view could be used without panels on any fieldable parent entity using Entity Views Attachment (EVA). If you do this, I believe you will need to provide the contextual filter with a default value: Content ID from URL.

Since the vote data is taken directly from the child node, the fivestar field does not need to exist on the parent entity nor does the fivestar field need to have a voting target. The downside I see here is that the parent's total average vote will not be available to other modules (such as rules) as it is not stored but calculated at run-time.

update
Although the fivestar formatters render correctly using this view, the text under the stars will read incorrectly :(

Hi, Thanks for the great module.

I have an issue. I create content type A with fivestar field tag vote1 set to display average vote, rate while viewing, exposed no. I create content type B with entity reference to a type A and fivestar field tag vote1 target of voting the entity reference to type A, rate while editing, display users vote. This works fine and the average vote is shown on type A when several pieces of content type B are added.

However then I add another fivestar field to type B, tag vote2, rate while viewing, exposed yes. But when a user votes on vote2, the scores in fivestar field with tag vote1 on type B are nulled which also effects the average displayed on type A.

All help appreciated. Thanks very much.

Adam

Category:feature» bug
StatusFileSize
new347.22 KB

I want to rate user entity so i have created a fivestar rating field (rated while viewing and display the average rating) in user account fields and i have created a content type "review" and in this content type i have added a fivestar field (rated while editing) and voting target is user entity. i have added three reviews for a user and when i see the user profile, there is no average instead user profile i can see the rating of latest review but i want to display the average rating here. How can i do it.

Category:bug» feature
Status:Needs review» Reviewed & tested by the community

This works really well for me. It's crucial functionality that should be in the dev release.

I've tested with the latest dev branch of fivestar and:

  • Entity reference 7.x-1.0
  • Voting API 7.x-2.11

Hopefully this can move forward and be added to the 7.x-2.x dev branch. Especially considering that Entity Reference is in Drupal 8 core and is really the path to the future in regards to referencing between entities and bundles.

Patch from #15 works great, thank you cafuego.

I would definitely love to see this patch getting in as well.

Patch from #15 works perfectly. Thanks !

Maybe we can apply it to the dev now ?
A few months have past and it is really crucial.

Issue summary:View changes
Status:Reviewed & tested by the community» Fixed

I've just committed the patch in #15. Thanks @cafuego and everyone who has worked on this.

I am using the latest Dev version.

I have done everything similar to #16's comment.

I have a product and a review content type, all my reviews have references to the products. The field has the setting for the entity reference field that references the product on the Review content type.

The product rating does not get updated. I have saved, resaved, created new reviews but it does not seem to update the fivestar rating on the product page. In the database the review rating target does not change from NULL. I have also set the appropriate target id in one of them just to test and it doesn't effect anything.

Any ideas on this issue are much appreciated.

-Jesse

Status:Fixed» Needs work

Like mentioned in #26 I am also having trouble with the parent node (Product) showing an average of all reviews.
I followed the steps but no luck.

Any advice would be great.

A quick follow up to #28.

I noticed if I set the fivestar field in parent node (Product) to use the widget "Stars (rated while viewing)" (this is system option know as "exposed"), the average is calculated on the product page. But using this option allows people to vote when viewing the product and I only want them to vote via the review content type.

Is there some option I am missing?

I found out what I was doing wrong.

Just going to explain what I did in case somebody else is having the same issue.

In the parent content type (E.g Product) add the Fivestar field and set the widget to "Stars (rated while viewing)"
Then in the display mode for the Fivestar field uncheck the checkbox "Allow voting on the entity."

This then will calculate the averages from the review content type but make them not editable when viewing the Product page.

You can see steps here https://drupal.org/node/1308114
Even though it's about nodes with using comments for reviews, the first section "Setting up the Content Type (node)" uses the same process with using nodes and entity references for reviews.

Status:Needs work» Fixed

Following on from @shi99 feedback in #28, #29, and #30, it seems that this can now be closed. To get average votes in the entity and user votes in comments, it's key to follow exactly every step in Fivestar 7.x: Creating a User Comment/Rating and Average Rating for a Content type

If anybody disagrees, please reopen this issue and give a detailed description of the problem.

Status:Fixed» Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.