SiteAlertStorage.php in Site Alert 8
File
src/SiteAlertStorage.php
View source
<?php
namespace Drupal\site_alert;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
class SiteAlertStorage extends SqlContentEntityStorage implements SiteAlertStorageInterface {
public function getCacheMaxAge() {
$current_datetime = new DrupalDateTime('now', DateTimeItemInterface::STORAGE_TIMEZONE);
$query = 'SELECT MIN(t) AS t FROM (SELECT scheduling__value t FROM {' . $this
->getBaseTable() . '} WHERE scheduling__value > :current_time AND active = :active UNION SELECT scheduling__end_value FROM {' . $this
->getBaseTable() . '} WHERE scheduling__end_value > :current_time AND active = :active) AS u;';
$next_scheduled_datetime = $this->database
->query($query, [
':current_time' => $current_datetime
->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT),
':active' => SiteAlertStorageInterface::ACTIVE,
])
->fetchField();
if (empty($next_scheduled_datetime)) {
return CacheBackendInterface::CACHE_PERMANENT;
}
$current_unix = (int) $current_datetime
->format('U');
$next_scheduled_unix = (int) (new DrupalDateTime($next_scheduled_datetime, DateTimeItemInterface::STORAGE_TIMEZONE))
->format('U');
return max(1, $next_scheduled_unix - $current_unix);
}
}