When editing a Fivestar field, the Target Node ID field accepts a static integer (such as 10) but not PHP code as described at http://drupal.org/handbook/modules/fivestar in the "Configuration as a CCK field / Advanced Rating" section.

When supplying PHP code for Target Node ID, such as:

  return $node->field_whatever[0]['nid'];

or even

  return 10;

leaves no result in the field_name_target field of the associated entry in content_type_name.

I believe the responsible code is in fivestar_field.inc's fivestar_field function:

    case 'update':
      foreach ($items as $delta => $item) {
        if ($field['dynamic_target'] && !empty($node->$field['dynamic_target'])) {
          $items[$delta]['target'] = $node->$field['dynamic_target'];
        }
        elseif (is_numeric($item['target'])) {
          $items[$delta]['target'] = $item['target'];
        }
        elseif ($field['php'] && strpos($item['target'], '<?php') === 0) {
          // Use eval rather than drupal_eval to allow access to local variables.
          $items[$delta]['target'] = eval('?>'. $item['target']);
        }
        if (is_numeric($item['target'])) {
          _fivestar_cast_vote('node', $item['target'], $item['rating'], $item['axis'], $node->uid);
          votingapi_recalculate_results('node', $item['target']);
        }
      }
      $items = array($item);
      break;

At the end of the update case, $item['target'] is checked to be numeric, and if so the vote is recorded to the database - using the values in $item['target']. The following has made it work for me, however being not familiar with the Fivestar code, I am not sure how it should ideally be corrected.

This at least illustrated the problem - that the PHP code to be evaluated (what you put into the Target field) is in $item['target'], and the result of calling eval on it gets put in $items[$delta]['target'], while $item['target'] is sent on to the database.

    case 'update':
      foreach ($items as $delta => $item) {
        if ($field['dynamic_target'] && !empty($node->$field['dynamic_target'])) {
          $items[$delta]['target'] = $node->$field['dynamic_target'];
        }
        elseif (is_numeric($item['target'])) {
          $items[$delta]['target'] = $item['target'];
        }
        elseif ($field['php'] && strpos($item['target'], '<?php') === 0) {
          // Use eval rather than drupal_eval to allow access to local variables.
          $items[$delta]['target'] = eval('?>'. $item['target']);
		  $item['target'] = $items[$delta]['target'];
        }
        if (is_numeric($items[$delta]['target'])) {
          _fivestar_cast_vote('node', $item['target'], $item['rating'], $item['axis'], $node->uid);
          votingapi_recalculate_results('node', $item['target']);
        }
      }
      $items = array($item);
      break;

Comments

quicksketch’s picture

Status: Active » Fixed

Thanks, this is a nice catch. I've corrected the problem by using the $items[$delta]['target'] value rather than $item['target'].

Patch: http://cvs.drupal.org/viewvc.py/drupal/contributions/modules/fivestar/fi...

patchak’s picture

Is this the same issue in fivestar field for drupal 5 ?? Seems to me the vote is not recorded to the DB when passign a NID with php.

I'm using views to display the vote without any luck.

msielski’s picture

patchak, no this appears to be a different issue. I'm presently also having the same problem you describe, in Drupal 5. I think there's another issue for it somewhere, I will try to find it.

quicksketch, great thanks!

Anonymous’s picture

Status: Fixed » Closed (fixed)

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