Skip to content
Snippets Groups Projects

Add status code cache context and use it.

Open catch requested to merge issue/drupal-3516173:3516173-block-status-code into 11.x
1 unresolved thread
Files
3
<?php
namespace Drupal\Core\Cache\Context;
use Drupal\Core\Cache\CacheableMetadata;
/**
* Defines the ExceptionStatusCodeCacheContext service.
*
* Cache context ID: 'exception_status_code'.
*/
class ExceptionStatusCodeCacheContext extends RequestStackCacheContextBase {
/**
* {@inheritdoc}
*/
public static function getLabel(): \Stringable {
return t('Exception status code');
}
/**
* {@inheritdoc}
*/
public function getContext(): string {
$exception = $this->requestStack->getCurrentRequest()->attributes->get('exception');
if ($exception) {
return (string) $exception->getStatusCode();
}
// If there's no exception status code, usually a 200, return '0' because we
// don't know what might be set by response subscribers.
return '0';
}
/**
* {@inheritdoc}
*/
public function getCacheableMetadata(): CacheableMetadata {
return new CacheableMetadata();
}
}
Loading