LayoutBuilderIsActiveCacheContext.php in Drupal 9
File
core/modules/layout_builder/src/Cache/LayoutBuilderIsActiveCacheContext.php
View source
<?php
namespace Drupal\layout_builder\Cache;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Cache\Context\CalculatedCacheContextInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\layout_builder\OverridesSectionStorageInterface;
class LayoutBuilderIsActiveCacheContext implements CalculatedCacheContextInterface {
protected $routeMatch;
public function __construct(RouteMatchInterface $route_match) {
$this->routeMatch = $route_match;
}
public static function getLabel() {
return t('Layout Builder');
}
public function getContext($entity_type_id = NULL) {
if (!$entity_type_id) {
throw new \LogicException('Missing entity type ID');
}
$display = $this
->getDisplay($entity_type_id);
return $display && $display
->isOverridable() ? '1' : '0';
}
public function getCacheableMetadata($entity_type_id = NULL) {
if (!$entity_type_id) {
throw new \LogicException('Missing entity type ID');
}
$cacheable_metadata = new CacheableMetadata();
if ($display = $this
->getDisplay($entity_type_id)) {
$cacheable_metadata
->addCacheableDependency($display);
}
return $cacheable_metadata;
}
protected function getDisplay($entity_type_id) {
if ($entity = $this->routeMatch
->getParameter($entity_type_id)) {
if ($entity instanceof OverridesSectionStorageInterface) {
return $entity
->getDefaultSectionStorage();
}
}
}
}