Skip to main content
3 of 3
deleted 59 characters in body
avpaderno
  • 97.9k
  • 15
  • 164
  • 284

Need to ignore empty variable values for Computed Field when multiplying

In my entityform, different fields appear based on choices (dependencies). Two appear at once. I'm multiplying the values using a Computed field. All works if the variables have a value. I have 8 fields I want to multiply, but ignore the empty values. Can't set field defaults to 0 (wrecks multiplication) or 1 (messes with totals).

The following warning appears for each empty field; here it's "line 1" for the 1st variable/field. No warning for those that have a value.

Warning: array_pop() expects parameter 1 to be array, null given in eval() (line 1 of /home/content/67/10793667/html/sites/all/modules/computed_field/computed_field.module(466) : eval()'d code).

How can I instruct the computed field to ignore the empty fields?

Here is my Computed Code (PHP).

$howmany = array_pop(array_pop(field_get_items($entity_type, $entity, 'field_how_many')));
$price1 = array_pop(array_pop(field_get_items($entity_type, $entity, 'field_reg_price1')));
$price2 = array_pop(array_pop(field_get_items($entity_type, $entity, 'field_sale_price2')));
$entity_field[0]['value'] = $howmany*$price1*$price2;

I tried adding this after the $price2 line, but it didn't work.

if ($price2 == null) {
    $price2 = 1;
}

I tried this too, and it didn't work.

if (!empty($price2)) {
  $price2 = 1;
}
else {
  $price2 = array_pop(array_pop(field_get_items($entity_type, $entity, 'field_sale_price2')));
}