class GetAlerts in Site Alert 8
Service for retrieving site alerts.
Hierarchy
- class \Drupal\site_alert\GetAlerts implements GetAlertsInterface
Expanded class hierarchy of GetAlerts
1 string reference to 'GetAlerts'
1 service uses GetAlerts
File
- src/
GetAlerts.php, line 13
Namespace
Drupal\site_alertView source
class GetAlerts implements GetAlertsInterface {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a new GetAlerts service.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager) {
$this->entityTypeManager = $entityTypeManager;
}
/**
* {@inheritdoc}
*/
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();
}
/**
* {@inheritdoc}
*/
public function getActiveAlerts() {
$ids = $this
->getActiveAlertIds();
if (!empty($ids)) {
return $this
->getStorage()
->loadMultiple($ids);
}
return [];
}
/**
* Returns the date in correct timezone and format to compare with database.
*
* @return string
* The date string representing the current time.
*/
protected function dateNow() {
$now = new DrupalDateTime();
$now
->setTimezone(new DateTimeZone(DateTimeItemInterface::STORAGE_TIMEZONE));
return $now
->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
}
/**
* Returns the entity storage for site alert entities.
*
* @return \Drupal\Core\Entity\EntityStorageInterface
* The entity storage.
*/
protected function getStorage() {
return $this->entityTypeManager
->getStorage('site_alert');
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
GetAlerts:: |
protected | property | The entity type manager. | |
GetAlerts:: |
protected | function | Returns the date in correct timezone and format to compare with database. | |
GetAlerts:: |
public | function |
Returns the entity IDs of the currently active site alerts. Overrides GetAlertsInterface:: |
|
GetAlerts:: |
public | function |
Returns the currently active site alerts. Overrides GetAlertsInterface:: |
|
GetAlerts:: |
protected | function | Returns the entity storage for site alert entities. | |
GetAlerts:: |
public | function | Constructs a new GetAlerts service. |