You are here

public function DateRecurItem::getHelper in Recurring Dates Field 8.2

Same name and namespace in other branches
  1. 3.x src/Plugin/Field/FieldType/DateRecurItem.php \Drupal\date_recur\Plugin\Field\FieldType\DateRecurItem::getHelper()
  2. 3.0.x src/Plugin/Field/FieldType/DateRecurItem.php \Drupal\date_recur\Plugin\Field\FieldType\DateRecurItem::getHelper()
  3. 3.1.x src/Plugin/Field/FieldType/DateRecurItem.php \Drupal\date_recur\Plugin\Field\FieldType\DateRecurItem::getHelper()

Get the helper for this field item.

Will always return a helper even if field value is non-recurring.

Return value

\Drupal\date_recur\DateRecurHelperInterface The helper.

Throws

\Drupal\date_recur\Exception\DateRecurHelperArgumentException If a helper could not be created due to faulty field value.

File

src/Plugin/Field/FieldType/DateRecurItem.php, line 416

Class

DateRecurItem
Plugin implementation of the 'date_recur' field type.

Namespace

Drupal\date_recur\Plugin\Field\FieldType

Code

public function getHelper() : DateRecurHelperInterface {
  if (isset($this->helper)) {
    return $this->helper;
  }
  try {
    $timeZoneString = $this->timezone;

    // If its not a string then cast it so a TypeError is not thrown. An empty
    // string will cause the exception to be thrown.
    $timeZone = new \DateTimeZone(is_string($timeZoneString) ? $timeZoneString : '');
  } catch (\Exception $exception) {
    throw new DateRecurHelperArgumentException('Invalid time zone');
  }
  $startDate = NULL;
  $startDateEnd = NULL;
  if ($this->start_date instanceof DrupalDateTime) {
    $startDate = $this->start_date
      ->getPhpDateTime();
    $startDate
      ->setTimezone($timeZone);
  }
  else {
    throw new DateRecurHelperArgumentException('Start date is required.');
  }
  if ($this->end_date instanceof DrupalDateTime) {
    $startDateEnd = $this->end_date
      ->getPhpDateTime();
    $startDateEnd
      ->setTimezone($timeZone);
  }
  $this->helper = $this
    ->isRecurring() ? DateRecurHelper::create((string) $this->rrule, $startDate, $startDateEnd) : DateRecurNonRecurringHelper::createInstance('', $startDate, $startDateEnd);
  return $this->helper;
}