public function ActivityEndDateFieldItemList::onChange in CiviCRM Entity 8.3
Recalculate the activities duration if the end date has been changed.
Overrides ItemList::onChange
File
- src/
Plugin/ Field/ ActivityEndDateFieldItemList.php, line 56
Class
- ActivityEndDateFieldItemList
- A computed field item list for Activities to provide an end date and time.
Namespace
Drupal\civicrm_entity\Plugin\FieldCode
public function onChange($delta) {
$entity = $this
->getEntity();
assert($entity instanceof CivicrmEntity);
// Since we're calculating a difference in times, we can use UTC.
$activity_date_time = new \DateTime($entity
->get('activity_date_time')->value, new \DateTimeZone('UTC'));
$new_end_date = new \DateTime($this
->get($delta)->value, new \DateTimeZone('UTC'));
$diff = $new_end_date
->getTimestamp() - $activity_date_time
->getTimestamp();
$minutes = $diff / 60;
$entity
->get('duration')
->setValue($minutes);
parent::onChange($delta);
}