Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Hi all! Sorry for writing here, but my PHP skills a pretty limited... I got a node type Club: it has four fivestar fields (food, service, price, comfort). I would like to have a fifth fivestar field, lets call it Average Node Rating which is computed as the average of the other four ratings. How do you think I could reproduce it? Is Computed Fields the right module?
Comments
Comment #1
joelrotelli CreditAttribution: joelrotelli commentedYes, i have done that with Computed field.
I have created a computed_field named "field_moyenne_evaluation" (for the average), and I have surcharged the calculation and display functions by a custom module with this code :
Remember to change the field "field_moyenne_evaluation" by yours !
Comment #2
cesareaugusto CreditAttribution: cesareaugusto commentedHi Joel! First thank you really much for replying and sharing! I got a question (I'm not much into the deep Drupal coding thing):
I know how to create a computed field... though I don't know how to create a custom module... I found this on the internet http://drupal.org/node/361112... Is it what you are talking about?
Comment #3
joelrotelli CreditAttribution: joelrotelli commentedHi,
You have to install the Computed Field module, add a Computed field to your content type (mine is field_moyenne_evaluation) and create a Drupal module (with http://drupal.org/node/361112 / the name of your module it's free) and create the two functions I have pasted.
You have to replace the name computed_field_field_moyenne_evaluation_compute and computed_field_field_moyenne_evaluation_display by computed_field_field_YOUR_FIELD_compute
Then you do your calculation in computed_field_field_YOUR_FIELD_compute and save the result in the $entity_field[0]['value'] variable :
For me :
In computed_field_field_YOUR_FIELD_display, you get the value previously saved with : $entity_field_item['value'];
For me :
And you send all the variables to the theme function.
Important: it's possible that you have to update your node to apply the changes on your computed field
Hope it help ! I'm french so my english explanation is maybe not very clear...
Comment #4
cesareaugusto CreditAttribution: cesareaugusto commentedThanks again for your help Joel! I'll try it out and post here my results!
I was wondering... What about Views sorting according to such a computed Average Node Rating field? I would like to sort nodes according to their average rating. Would be the average computed on page view? Or stored within the Drupal database?
Again... what about when new users rate contents? Is the computed field updated on the fly?
Merci beaucoup! :)
Comment #5
joelrotelli CreditAttribution: joelrotelli commentedYes, there is a view support for computed field, so you can sort with the average, I did it, so it works :)
The computed field is updated on the fly when a new user rate content.
Comment #6
Louis Bob CreditAttribution: Louis Bob commentedHi,
I have a view where I managed to get the averaged numeric value for the nodes => it is the equivalent to the "$moyenne" variable in Joel Rotelli's code.
So for example, I get values such as 67.333, 73.333, etc.
Now I just need to display these values with fivestars, with the stars.
So I used a Global PHP field in the view, in which I pasted the second function of the code (function computed_field_field_moyenne_evaluation_display). I have no error message, but nothing is displayed either. Am I missing a "print" or "echo" something to have the stars displayed correctly ?
Comment #7
descender CreditAttribution: descender commentedHi all,
The solution in #1 didn't work for me. For some reason, calling field_view_field() in _compute() crashes PHP with a memory exhaustion error after recursing indefinitely. Another problem is that field_view_field() returns a render array, which is heavy and complex.
What did work, and much simpler for me was to use fivestar_get_votes() like this:
axis1, axis2, and axis are the 3 different voting tags I created, each assigned to one of the 3 fivestar fields I have. You have to take care to handle the case where no one has voted yet, as the $axis1['average'] array (for example) will be empty. The snippet above doesn't account for this.
Other things to watch out are:
Comment #8
jacksboredom CreditAttribution: jacksboredom commentedThe second solution by descender worked out for me really well. I used Joel Rotelli's method to manage the displaying of the field and have a question now:
The star-version of the field works but I want to have the numeric value to be displayed too.
How can I manage to do that? What do I have to change in the code?
Comment #9
joelrotelli CreditAttribution: joelrotelli commentedIn the fivestar_theme function from the fivestar.module, you can find all the theme formatters you can use to display the value.
I used fivestar_classic (return theme('fivestar_static', $variables);) , but you can try others and send a correct $variables array
Comment #10
jacksboredom CreditAttribution: jacksboredom commentedI'm not a big coding kid. Could you please add a code suggestion for a fitting formatter for displaying the numeric value of the computed average field.
Comment #11
joelrotelli CreditAttribution: joelrotelli commentedI don't know exactly which theme formatter you need, but for exemple, with "fivestar_summary" :
You have to format a $variables array corresponding to the array needed in the fivestar_theme function
Comment #12
jacksboredom CreditAttribution: jacksboredom commentedWhen I use your solution with fivestar_summary, I get back "No Votes" instead of the numeric value.
Any suggestions?
Comment #13
jacksboredom CreditAttribution: jacksboredom commented?
Comment #14
kapil.ropalekar CreditAttribution: kapil.ropalekar commentedHi joel rotelli
I followed your exact steps and managed to achieve the average rating from computed field.
My problem is i have set fivestar widget as Stars (rated while editing). So once i enter the rating and save the node only the computed stars are displayed properly and the other ratings show a "No votes yet".
What could i have done wrong. Can anyone help pls
Regards
Comment #15
kapil.ropalekar CreditAttribution: kapil.ropalekar commentedAnybody ???
I figured out that the on node publication the fivestar average values are not getting saved in database.
As of now I have used a simple custom module and used a hook_node_update to update the table by firing a query myself. Working for me :)
Comment #16
glycid CreditAttribution: glycid commented@ kapil.ropalekar
I've the same problem. Can you post an example of your solution with hook_node_update ?
thanks a lot for your help
Comment #17
ndf CreditAttribution: ndf commentedWorking example:
Create 4 voting tags/axis on /admin/config/content/fivestar.
Create a "review" content-type with 4 voting fields:
to vote on:
calculated:
set your voting targets as you normally do (with a node_reference field).
We hide the average field and calculate it when the review is saved or updated.
BTW: this code uses entity api (function entity_metadata_wrapper) & fivestar.
Comment #18
irgnet CreditAttribution: irgnet commentedHow do i change #17 code to work with comments, not with review content type ?
Comment #19
ndf CreditAttribution: ndf commentedA little:
In this example the content-type name is "YOUR-CONTENT-TYPE":
Comment #20
irgnet CreditAttribution: irgnet commentedTnanks nielsdefeyter,
A try both with Review and Comments and both works and calulculates average rating.
But firs part of code, which must hide overall rating field
$form['field_review_votes_average']['#access'] = FALSE;
$form['field_votes_average']['#access'] = FALSE;
They both don't work. The field still shows on comment form / node review add form
Comment #21
edg CreditAttribution: edg commented@irgnet You need make sure you have the right $form_id
There's a good video intro to hook_form_alter by Bob at Mustard Seed Media at http://youtu.be/7Wn0sIrGWDI
Comment #22
chris_h CreditAttribution: chris_h commented@nielsdefeyter to check, do your examples use computed field at all?
Comment #23
mariuscmr CreditAttribution: mariuscmr commentedThank you, @cesareaugusto! Your implementation works great on my website, which has this structure:
Article
etc.
Comment #24
ndf CreditAttribution: ndf commentedReply to #22
No, the examples don't use the module 'computed field'. Only entity api & fivestar.
Comment #25
ndf CreditAttribution: ndf commentedReply to #20
@irgnet: Did #21 solved your problem?
Comment #26
chris_h CreditAttribution: chris_h commented@nielsdefeyter thanks for clarifying. This works perfectly. It's probably worth bringing this to the attention of the Fivestar queue as there's a new maintainer trying to put out a stable release at the moment
Comment #27
ndf CreditAttribution: ndf commented@#26 : https://drupal.org/node/2218349
Comment #28
whiteph CreditAttribution: whiteph commented@nielsdefeyter & chris_h - thanks for your help re FiveStar.
Comment #29
ebenfarnworth CreditAttribution: ebenfarnworth commentedThanks for the example in #19. How would you set up the code for views integration? That example works great but views seams unable to pull the displayed data from field_votes_average. When the field is loaded in a view no data is shown, just a blank fivestar field.
Comment #30
NatiNog CreditAttribution: NatiNog commentedHi, I used the descender solution at #7 and it worked perfectly. But now I can't sort my view depending on that field.
Can anybody tell my hoe to do that? The field doesn't appear on the list when I try to add a sort criteria.
Comment #31
SseggembeMoses CreditAttribution: SseggembeMoses commented@ndf. Thanks for this solution bro at #19.
It has really helped me a lot.
Comment #32
3dnathaniel CreditAttribution: 3dnathaniel commentedI was able to get #1 to work in my use case. Works when viewing the node.
However, I can't seem to get views to recognize the computed field. It always shows up as "0" in views.
Anyone have an idea?