You are here

function fivestar_field_prepare_view in Fivestar 7.2

Implements hook_field_prepare_view().

File

includes/fivestar.field.inc, line 740
Provides CCK integration for fivestar module.

Code

function fivestar_field_prepare_view($entity_type, $entities, $field, $instances, $langcode, &$items) {

  // @todo Clean this function up!
  $exposed_widgets = array();
  foreach ($entities as $id => $entity) {

    // Ensure the items aren't processed twice.
    if (!isset($items[$id][0]['count'])) {
      if ($instances[$id]['widget']['type'] == 'exposed') {
        $exposed_widgets[] = $id;
      }
      else {

        // If the widget type is not exposed, then the count is always 1 or 0.
        // The count is pointless to display.
        if (!empty($items[$id][0]['rating'])) {
          $values['count'] = 1;
          $values['user'] = $items[$id][0]['rating'];
          $values['average'] = $items[$id][0]['rating'];
        }
        else {
          $values['count'] = 0;
          $values['user'] = 0;
          $values['average'] = 0;
        }
        $items[$id] = array(
          $values,
        );
      }
    }
    if (!empty($exposed_widgets)) {
      $votes = fivestar_get_votes_multiple($entity_type, $exposed_widgets, $field['settings']['axis']);
      foreach ($votes[$entity_type] as $id => $vote) {

        // Populating the $items[$id] array even for items with no value forces
        // the render system to output a widget.
        $values['user'] = isset($vote['user']['value']) ? $vote['user']['value'] : 0;
        $values['average'] = isset($vote['average']['value']) ? $vote['average']['value'] : 0;
        $values['count'] = isset($vote['count']['value']) ? $vote['count']['value'] : 0;
        $items[$id] = array(
          $values,
        );
      }
    }
  }
}