1

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')));
}

2 Answers 2

1

Both @Neo's answer and @danielesus's question shows PHP code that will throw errors. @Neo, empty() only works with variables before PHP 5.5. Both: array_pop requires a variable or something returned by a reference-returning-function which field_get_items is not.

Now let's consider what field_get_items($entity_type, $entity, 'field_how_many') returns: it returns an array indexed by delta where the values are field_column => value. You can do

if ($all_items = field_get_items($entity_type, $entity, 'field_how_many')) {
  $item = reset($all_items);
  $value = reset($item);
}

as there's no need to actually modify the array with array_pop but also you can use your knowledge about Field API and know the actual column name which I suspect will be value here (would be tid for a taxonomy reference field for example) and that $delta is, if you have a field with cardinality 1 is actually 0:

$value = NULL;
if ($all_items = field_get_items($entity_type, $entity, 'field_how_many')) {
  $value = $all_items[0]['value'];
}

Fast and simple.

0
$howmany = !empty(field_get_items($entity_type, $entity, 'field_how_many')) ? array_pop(array_pop(field_get_items($entity_type, $entity, 'field_how_many'))) : 1; 

$price1 = !empty(field_get_items($entity_type, $entity, 'field_reg_price1')) ? array_pop(array_pop(field_get_items($entity_type, $entity, 'field_reg_price1'))) : 1; 

$price2 = !empty(field_get_items($entity_type, $entity, 'field_sale_price2')) ? array_pop(array_pop(field_get_items($entity_type, $entity, 'field_sale_price2'))) : 1; 

$entity_field[0]['value'] = $howmany*$price1*$price2;

If this still does not work, Please try to inspect the structure of the stuff returned by field_get_items(). I am pretty sure the issue lies in returning a null from the fist array_pop.

In order to inspect the structure, add these lines to your compute code after enabling and installing devel module:

dpm(field_get_items($entity_type, $entity, 'field_how_many')); dpm(field_get_items($entity_type, $entity, 'field_reg_price1')); dpm(field_get_items($entity_type, $entity, 'field_sale_price2')));

6
  • Hi Neo, thanks so much for your quickly reply. Been testing almost immediately, getting the same results as before, same Warning on empty fields. Using the code you gave, with only 2 fields (e.g., $howmany and $price1) works as before with computed value appearing correctly. I get the same error when I add a 3rd variable that is an empty field - and the results shows 0, most likely due the multiplication with empty field.
    – danielesus
    Commented Oct 4, 2013 at 14:32
  • modified the code. Please see if this works.
    – Neo
    Commented Oct 4, 2013 at 14:41
  • Same results, same Warning. Works fine with just 2 fields, computed value - great. As soon as I add a 3rd, get the error and 0 value.
    – danielesus
    Commented Oct 4, 2013 at 15:01
  • ok I think I figured out the issue. the issue lies in the two array_pop s. In case of an empty field, the first array_pop returns null. And hence the second one gets null as parameter. Updated my answer accordingly.
    – Neo
    Commented Oct 4, 2013 at 15:50
  • On form submission, get a WSOD with Fatal error on line 1: Fatal error: Can't use function return value in write context in /home/content/67/10793667/html/sites/all/modules/computed_field/computed_field.module(466) : eval()'d code on line 1
    – danielesus
    Commented Oct 4, 2013 at 16:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.