You are here

public static function DateRangeAllDayHelper::isAllDay in Date all day 8

Helper function to check if a daterange item covers all day.

Parameters

array|DateRangeItem $item: The date range item to check.

Return value

bool A boolean indicating if a daterange item covers all day or not.

3 calls to DateRangeAllDayHelper::isAllDay()
DateRangeAllDayTrait::viewElements in src/Plugin/Field/FieldFormatter/DateRangeAllDayTrait.php
DatetimeRangeAllDayWidget::formElement in src/Plugin/Field/FieldWidget/DatetimeRangeAllDayWidget.php
Returns the form for a single field widget.
DatetimeRangeAllDayWidget::massageFormValues in src/Plugin/Field/FieldWidget/DatetimeRangeAllDayWidget.php
Massages the form values into the format expected for field values.

File

src/Utility/DateRangeAllDayHelper.php, line 26

Class

DateRangeAllDayHelper
Class DateRangeAllDayHelper.

Namespace

Drupal\date_all_day\Utility

Code

public static function isAllDay($item) {
  if ($item instanceof DateRangeItem) {

    /** @var \Drupal\Core\Datetime\DrupalDateTime $start_date */
    $start_date = $item
      ->get('value')
      ->getDateTime();

    /** @var \Drupal\Core\Datetime\DrupalDateTime $end_date */
    $end_date = $item
      ->get('end_value')
      ->getDateTime();
  }
  elseif (is_array($item) && isset($item['value'])) {

    /** @var \Drupal\Core\Datetime\DrupalDateTime $start_date */
    $start_date = $item['value'];

    /** @var \Drupal\Core\Datetime\DrupalDateTime $end_date */
    if (is_array($item['end_value']) && empty($item['end_value']['object'])) {
      $end_date = NULL;
    }
    else {
      $end_date = $item['end_value'];
    }
  }
  else {
    throw new \InvalidArgumentException('Argument $item should be either a Drupal\\datetime_range\\Plugin\\Field\\FieldType\\DateRangeItem object, either an array with a \\Drupal\\Core\\Datetime\\DrupalDateTime in the "value" key.');
  }
  $timezone = date_default_timezone_get();
  return !empty($start_date) && $start_date
    ->format(self::TIME_FORMAT, [
    'timezone' => $timezone,
  ]) === '00:00:00' && (empty($end_date) || $end_date
    ->format(self::TIME_FORMAT, [
    'timezone' => $timezone,
  ]) === '23:59:59');
}