You are here

public function SitewideAlert::isScheduledToShowAt in Sitewide Alert 8

Determines if this SitewideAlert should be shown at the given time.

Parameters

\DateTime $dateTime: The time to compare.

Return value

bool TRUE if this Alert should show at the given time, FALSE otherwise.

Overrides SitewideAlertInterface::isScheduledToShowAt

File

src/Entity/SitewideAlert.php, line 401

Class

SitewideAlert
Defines the Sitewide Alert entity.

Namespace

Drupal\sitewide_alert\Entity

Code

public function isScheduledToShowAt(\DateTime $dateTime) {

  // If this Sitewide Alert is not a scheduled alert, it should show regardless of time.
  if (!$this
    ->isScheduled()) {
    return TRUE;
  }
  $startTime = $this
    ->getScheduledStartDateTime();
  $endTime = $this
    ->getScheduledEndDateTime();

  // The Sitewide Alert is marked as scheduled but dates have not been provided.
  if ($startTime === NULL || $endTime === NULL) {
    return FALSE;
  }

  // Convert to a DrupalDatetime.
  $dateTimeToCompare = DrupalDateTime::createFromDateTime($dateTime);
  return $dateTimeToCompare >= $startTime && $dateTimeToCompare <= $endTime;
}