3 namespace Drupal\layout_builder\Cache;
5 use Drupal\Core\Cache\CacheableMetadata;
6 use Drupal\Core\Cache\Context\CalculatedCacheContextInterface;
7 use Drupal\Core\Routing\RouteMatchInterface;
8 use Drupal\layout_builder\OverridesSectionStorageInterface;
11 * Determines whether Layout Builder is active for a given entity type or not.
13 * Cache context ID: 'layout_builder_is_active:%entity_type_id', e.g.
14 * 'layout_builder_is_active:node' (to vary by whether the Node entity type has
15 * Layout Builder enabled).
17 class LayoutBuilderIsActiveCacheContext implements CalculatedCacheContextInterface {
20 * The current route match.
22 * @var \Drupal\Core\Routing\RouteMatchInterface
24 protected $routeMatch;
27 * LayoutBuilderCacheContext constructor.
29 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
30 * The current route match.
32 public function __construct(RouteMatchInterface $route_match) {
33 $this->routeMatch = $route_match;
39 public static function getLabel() {
40 return t('Layout Builder');
46 public function getContext($entity_type_id = NULL) {
47 if (!$entity_type_id) {
48 throw new \LogicException('Missing entity type ID');
51 $display = $this->getDisplay($entity_type_id);
52 return ($display && $display->isOverridable()) ? '1' : '0';
58 public function getCacheableMetadata($entity_type_id = NULL) {
59 if (!$entity_type_id) {
60 throw new \LogicException('Missing entity type ID');
63 $cacheable_metadata = new CacheableMetadata();
64 if ($display = $this->getDisplay($entity_type_id)) {
65 $cacheable_metadata->addCacheableDependency($display);
67 return $cacheable_metadata;
71 * Returns the entity view display for a given entity type and view mode.
73 * @param string $entity_type_id
76 * @return \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface|null
77 * The entity view display, if it exists.
79 protected function getDisplay($entity_type_id) {
80 if ($entity = $this->routeMatch->getParameter($entity_type_id)) {
81 if ($entity instanceof OverridesSectionStorageInterface) {
82 return $entity->getDefaultSectionStorage();