public function SitewideAlertsController::load in Sitewide Alert 8
Load.
Return value
\Symfony\Component\HttpFoundation\JsonResponse Return Hello string.
Throws
\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
\Drupal\Component\Plugin\Exception\PluginNotFoundException
1 string reference to 'SitewideAlertsController::load'
File
- src/
Controller/ SitewideAlertsController.php, line 61
Class
- SitewideAlertsController
- Class SitewideAlertsController.
Namespace
Drupal\sitewide_alert\ControllerCode
public function load() {
$response = new CacheableJsonResponse([]);
$sitewideAlertsJson = [
'sitewideAlerts' => [],
];
$sitewideAlerts = $this->sitewideAlertManager
->activeVisibleSitewideAlerts();
$viewBuilder = $this
->entityTypeManager()
->getViewBuilder('sitewide_alert');
foreach ($sitewideAlerts as $sitewideAlert) {
$message = $viewBuilder
->view($sitewideAlert);
$sitewideAlertsJson['sitewideAlerts'][] = [
'uuid' => $sitewideAlert
->uuid(),
'message' => $this->renderer
->renderPlain($message),
'dismissible' => $sitewideAlert
->isDismissible(),
'dismissalIgnoreBefore' => $sitewideAlert
->getDismissibleIgnoreBeforeTime(),
'styleClass' => $sitewideAlert
->getStyleClass(),
'showOnPages' => $sitewideAlert
->getPagesToShowOn(),
'negateShowOnPages' => $sitewideAlert
->shouldNegatePagesToShowOn(),
];
}
// Set the response cache to be dependent on whenever sitewide alerts get updated.
$cacheableMetadata = (new CacheableMetadata())
->setCacheMaxAge(30)
->addCacheContexts([
'languages',
])
->setCacheTags([
'sitewide_alert_list',
]);
$response
->addCacheableDependency($cacheableMetadata);
$response
->setData($sitewideAlertsJson);
// Set the date this response expires so that Drupal's Page Cache will
// expire this response when the next scheduled alert will be removed.
// This is needed because Page Cache ignores max age as it does not respect
// the cache max age. Note that the cache tags will still invalidate this
// response in the case that new sitewide alerts are added or changed.
// See Drupal\page_cache\StackMiddleware:storeResponse().
if ($expireDate = $this->sitewideAlertManager
->nextScheduledChange()) {
$response
->setExpires($expireDate
->getPhpDateTime());
}
// Prevent the browser and downstream caches from caching for more than 15 seconds.
$response
->setMaxAge(15);
$response
->setSharedMaxAge(15);
return $response;
}