View source
<?php
namespace Drupal\site_alert;
use DateTimeZone;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
class GetAlerts implements GetAlertsInterface {
protected $entityTypeManager;
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
}
public function getActiveAlertIds() {
$now = $this
->dateNow();
$query = $this
->getStorage()
->getQuery();
$start_value = $query
->orConditionGroup()
->condition('scheduling.value', $now, '<=')
->notExists('scheduling.value');
$end_value = $query
->orConditionGroup()
->condition('scheduling.end_value', $now, '>')
->notExists('scheduling.end_value');
$query
->condition('active', 1, '=')
->condition($start_value)
->condition($end_value);
return $query
->execute();
}
public function getActiveAlerts() {
$ids = $this
->getActiveAlertIds();
if (!empty($ids)) {
return $this
->getStorage()
->loadMultiple($ids);
}
return [];
}
protected function dateNow() {
$now = new DrupalDateTime();
$now
->setTimezone(new DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE));
return $now
->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
}
protected function getStorage() {
return $this->entityTypeManager
->getStorage('site_alert');
}
}