The description has strange example with double array_pop.
array_pop(array_pop(field_get_items($entity_type, $entity, 'field_a')))
It will generate something like this: Strict warning: Only variables should be passed by reference in eval() (line 3 of ...\modules\computed_field\computed_field.module(399) : eval()'d code).
Here is working example:

$entity_field[0]['value'] = 
  field_get_items($entity_type, $entity, 'field_a')[0]['value'] / 
  field_get_items($entity_type, $entity, 'field_b')[0]['value'];

Comments

core44’s picture

Thanks for this, caught me out for a while!

bkirkendall’s picture

Thanks! You saved me so much agony!

sardbaba’s picture

break9’s picture

Issue summary: View changes

to avoid php notices this should be written as:

  $field_a = field_get_items($entity_type, $entity, 'field_a');
  $field_b = field_get_items($entity_type, $entity, 'field_b');
  $entity_field[0]['value'] = $field_a[0]['value'] / $field_b[0]['value'];
arruk’s picture

how would this be used to total a single field with multiple values?

kiricou’s picture

thanks !!!

colan’s picture

Version: 7.x-1.0-beta1 » 7.x-1.x-dev
Category: Bug report » Task
Status: Active » Needs review

Okay, I think I got this fixed on the doc page over at https://www.drupal.org/node/126522. Please review.

colan’s picture

Just committed this to the in-line example as well. Thanks!

  • colan committed 750681d on 7.x-1.x
    Issue #1896130 by break9, colan: Updated in-line example to avoid new...
colan’s picture

Status: Needs review » Fixed

I'll assume this is fixed for now. Speak up if there are any issues.

Status: Fixed » Closed (fixed)

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

jaredcampbell’s picture

Are there any plans to push this fix to the production version of this module for Drupal 7?

Edit: I misunderstood the post... Will be updating my code to eliminate the double array pop. Thanks!

colan’s picture

Version: 7.x-1.x-dev » 7.x-1.1

Thanks for the idea. We needed another D7 release anyway. Please find this fix in 7.x-1.1. It should be published in the next few minutes.

siramsay’s picture

I couldn't get this working using the example code but were getting the Strict warning: Only variables should be passed by reference in eval()

I am working with 5 star fields and this code works, just posting for others experiencing the same problem and maybe someone can optimize it a bit?
it passes variables to the array_pop function and does it twice, or does double array_pop as the issue describes .

running PHP 5.5.30


$field_a = field_get_items($entity_type, $entity, 'field_a');
$field_a_1 = array_pop($field_a);
$field_a_2 = array_pop($field_a_1);
$field_b = field_get_items($entity_type, $entity, 'field_b');
$field_b_1 = array_pop($field_b);
$field_b_2 = array_pop($field_b_1);

$sum =  $field_b_1 + $field_b_2;
$average = $sum / 20; // divide by 20 to get a value out of 5, fivestar uses scale of 100 = 5
$average = $average / 2; //number of fivestar fields
$entity_field[0]['value'] = $average;