Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
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
Comment #1
Exploratus CreditAttribution: Exploratus commentedsubscribe.
Comment #2
eminencehealthcare CreditAttribution: eminencehealthcare commentedThanks 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;
}
Comment #2.0
eminencehealthcare CreditAttribution: eminencehealthcare commentedtypo