You are here

protected function ActivityEndDateFieldItemList::computeValue in CiviCRM Entity 8.3

Computes the values for an item list.

Overrides ComputedItemListTrait::computeValue

File

src/Plugin/Field/ActivityEndDateFieldItemList.php, line 29

Class

ActivityEndDateFieldItemList
A computed field item list for Activities to provide an end date and time.

Namespace

Drupal\civicrm_entity\Plugin\Field

Code

protected function computeValue() {
  $entity = $this
    ->getEntity();
  assert($entity instanceof CivicrmEntity);
  $activity_date_time = $entity
    ->get('activity_date_time')->value;
  $duration = $entity
    ->get('duration')->value;
  if (!$activity_date_time) {
    return;
  }

  // The time is already in UTC due to ::initFieldValues in storage.
  // @see \Drupal\civicrm_entity\CiviEntityStorage::initFieldValues
  $date = new \DateTime($activity_date_time, new \DateTimeZone('UTC'));

  // We have to change this _back_ to the default timezone, as initFieldValues
  // will be called again and it assumes the value is from CiviCRM, in the
  // default timezone.
  $date
    ->setTimezone(new \DateTimeZone(date_default_timezone_get()));
  if (is_numeric($duration)) {
    $date
      ->add(new \DateInterval("PT{$duration}M"));
  }
  $this->list[0] = $this
    ->createItem(0, $date
    ->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT));
}