You are here

public function BillingPeriodItem::setValue in Commerce Recurring Framework 8

Overrides \Drupal\Core\TypedData\TypedData::setValue().

Parameters

array|null $values: An array of property values.

Overrides FieldItemBase::setValue

File

src/Plugin/Field/FieldType/BillingPeriodItem.php, line 75

Class

BillingPeriodItem
Plugin implementation of the 'commerce_billing_period' field type.

Namespace

Drupal\commerce_recurring\Plugin\Field\FieldType

Code

public function setValue($values, $notify = TRUE) {

  // Allow callers to pass a BillingPeriod value object.
  if ($values instanceof BillingPeriod) {
    $values = [
      'starts' => $values
        ->getStartDate(),
      'ends' => $values
        ->getEndDate(),
    ];
  }

  // DrupalDateTime values passed by the caller or taken via BillingPeriod.
  if (isset($values['starts']) && $values['starts'] instanceof DrupalDateTime) {
    $values['starts']
      ->setTimezone(new \DateTimezone('UTC'));
    $values['starts'] = $values['starts']
      ->format('U');
  }
  if (isset($values['ends']) && $values['ends'] instanceof DrupalDateTime) {
    $values['ends']
      ->setTimezone(new \DateTimezone('UTC'));
    $values['ends'] = $values['ends']
      ->format('U');
  }
  parent::setValue($values, $notify);
}