Skip to content
Snippets Groups Projects

Return a cacheable response from TimezoneController

1 unresolved thread
Files
2
@@ -2,7 +2,8 @@
namespace Drupal\system\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\Core\Cache\CacheableJsonResponse;
use Drupal\Core\Cache\CacheableMetadata;
/**
* Provides a callback for finding a time zone identifier.
@@ -23,15 +24,19 @@ class TimezoneController {
* Daylight saving time indicator. If abbreviation does not exist then the
* time zone is searched solely by offset and is DST.
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The time zone identifier or 'false' in JsonResponse object.
* @return \Drupal\Core\Cache\CacheableJsonResponse
* The time zone identifier or 'false' in CacheableJsonResponse object.
*/
public function getTimezone($abbreviation = '', $offset = -1, $is_daylight_saving_time = NULL) {
$offset = intval($offset);
// All timezone responses are cacheable based on the route.
$cacheable_metadata = (new CacheableMetadata())->addCacheContexts(['route']);
// Out of bounds check for offset. Offset +/- UTC is typically no
// smaller/larger than -12/+14.
if ($offset < -60000 || $offset > 60000) {
return new JsonResponse(FALSE);
return (new CacheableJsonResponse(FALSE))->addCacheableDependency($cacheable_metadata);
}
if (isset($is_daylight_saving_time)) {
@@ -39,15 +44,20 @@ public function getTimezone($abbreviation = '', $offset = -1, $is_daylight_savin
$is_daylight_saving_time = min(1, max(-1, intval($is_daylight_saving_time)));
// Catch if out of boundary.
if ($original !== $is_daylight_saving_time) {
return new JsonResponse(FALSE);
return (new CacheableJsonResponse(FALSE))->addCacheableDependency($cacheable_metadata);
}
}
else {
// If no daylight saving time indicator was specified, default to -1
    • This part is the only thing I'm not sure about, seems to be addressing another issue maybe?

      • It is addressing a different issue, but it's somewhat a prerequisite to get caching to work.

        Without this, $is_daylight_saving_time can be NULL when it is passed to timezone_name_from_abbr. As of PHP 8, this will generate a deprecation notice. And, depending on the current settings, a PHP notice will add a message to Messenger which will trigger the page cache KillSwitch (see Messenger::addMessage).

        So, while the controller can return a cacheable response without this change, the response will likely not end up getting cached due to the PHP notice.

      • Please register or sign in to reply
Please register or sign in to reply
// to indicate that daylight saving was not taken into consideration.
$is_daylight_saving_time = -1;
}
// An abbreviation of "0" passed in the callback arguments should be
// interpreted as the empty string.
$abbreviation = $abbreviation ?: '';
$timezone = timezone_name_from_abbr($abbreviation, $offset, $is_daylight_saving_time);
return new JsonResponse($timezone);
return (new CacheableJsonResponse($timezone))->addCacheableDependency($cacheable_metadata);
}
}
Loading