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

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 Warningwarning appears for each empty field -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).

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).

Tried many different null and empty PHP codes, nothing works - sure it's my error - not a pro. 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;.

$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$price2 line, but it didn't work. if ($price2 == null) { $price2 = 1; }

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

Tried If else alsoI 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'))); }.

Many Thanks Daniel

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

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).

Tried many different null and empty PHP codes, nothing works - sure it's my error - not a pro. 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, didn't work. if ($price2 == null) { $price2 = 1; }

Tried If else also, didn't work:

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

Many Thanks Daniel

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')));
}
edited tags
Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284
Source Link

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).

Tried many different null and empty PHP codes, nothing works - sure it's my error - not a pro. 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, didn't work. if ($price2 == null) { $price2 = 1; }

Tried If else also, didn't work:

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

Many Thanks Daniel