Problem/Motivation

If field_view_field() is manually used to display the fivestar field and it is called more than once (e.g. because of different settings) the rating values can be removed when the field isn't exposed or it adds unnecessary overhead if the field is exposed.
Example code:

$small_widget = field_view_field('node', $node, 'field_rating', array(
  'label' => 'hidden',
  'type' => 'fivestar_formatter_default',
  'settings' => array(
	'style' => 'average',
	'text' => 'average',
	'widget' => array(
	  'fivestar_widget' => 'my_small_widget.css',
	),
  ),
));

$huge_widget = field_view_field('node', $node, 'field_rating', array(
  'label' => 'above',
  'type' => 'fivestar_formatter_default',
  'settings' => array(
	'style' => 'average',
	'text' => 'average',
	'widget' => array(
	  'fivestar_widget' => 'my_very_huge_widget.css',
	),
  ),
));

Proposed resolution

To prevent this fivestar_field_prepare_view() should check if the field items already were processed and skip processing if so.
The attached patch became bigger since there were quite some trailing white-spaces. My IDE removes them automatically to comply with the Drupal coding standards. If there's a need for a patch without these white space changes I'll provide one.

Remaining tasks

reviews needed.

User interface changes

none

API changes

none

Comments

j0rd’s picture

Here's another issue which relates to performance of fivestar_field_prepare_view() in case you're curious.
#1836444: DB Performance: hook_field_prepare_view & fivestar_get_votes to allow for database queries off multiple IDs at once.

whiteph’s picture

Status: Needs review » Fixed

I think this is a duplicate of Rating is reset, which I've fixed in 7.x-2.x-dev. However, your patch updates a sightly different part of the code in the same file, and looks to me like it doesn't clash with my patch. Also, it has a performance improvement too - so I've accepted the patch and applied it.

If you look at my patch, it too had the add/delete of blank lines. I tried all sorts of things to remove those from my patch, but with no success. My theory is that a patch/update in the long ago added blank lines which didn't conform to current Drupal standards. It seems to be git which is creating the adds/deletes rather than the IDE (Eclipse in my case).

My patch updated those blank lines, so patch failed to apply as git couldn't find the blank lines your patch included. I fell back to a 3-way apply, and git happily applied the real patch for fivestar_field_prepare_view. If anyone has another explanation, I'd be keen to hear it.

Status: Fixed » Closed (fixed)

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