My computed field is calculating the difference in hours between two date fields.
It works great except for if the node is saved without a date field value I get an error.
How can I make the field not calculate unless both values are filled?

Comments

Exploratus’s picture

subscribe.

eminencehealthcare’s picture

Thanks to Spovlot's help, I used an if/else statement. If the field has a value, it evaluates, else it sets the value to 0.

Here is my specific code:

$start_value = $entity->field_clock_in[LANGUAGE_NONE][0]['value'];
$end_value = $entity->field_start_lunch[LANGUAGE_NONE][0]['value'];
$timezone = $entity->field_clock_in[LANGUAGE_NONE][0]['timezone'];
if (isset($start_value) && isset($end_value) && isset($timezone)) {
$start_date = new DateObject($start_value, $timezone);
$end_date = new DateObject($end_value, $timezone);
$duration = $start_date->difference($end_date, 'hours');
$entity_field[0]['value'] = $duration;
}
else {
$entity_field[0]['value'] = 0;
}

eminencehealthcare’s picture

Issue summary: View changes

typo