Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\layout_builder\Cache;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Cache\CacheableMetadata;
|
Chris@0
|
6 use Drupal\Core\Cache\Context\CalculatedCacheContextInterface;
|
Chris@0
|
7 use Drupal\Core\Routing\RouteMatchInterface;
|
Chris@0
|
8 use Drupal\layout_builder\OverridesSectionStorageInterface;
|
Chris@0
|
9
|
Chris@0
|
10 /**
|
Chris@0
|
11 * Determines whether Layout Builder is active for a given entity type or not.
|
Chris@0
|
12 *
|
Chris@0
|
13 * Cache context ID: 'layout_builder_is_active:%entity_type_id', e.g.
|
Chris@0
|
14 * 'layout_builder_is_active:node' (to vary by whether the Node entity type has
|
Chris@0
|
15 * Layout Builder enabled).
|
Chris@0
|
16 */
|
Chris@0
|
17 class LayoutBuilderIsActiveCacheContext implements CalculatedCacheContextInterface {
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * The current route match.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @var \Drupal\Core\Routing\RouteMatchInterface
|
Chris@0
|
23 */
|
Chris@0
|
24 protected $routeMatch;
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * LayoutBuilderCacheContext constructor.
|
Chris@0
|
28 *
|
Chris@0
|
29 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
|
Chris@0
|
30 * The current route match.
|
Chris@0
|
31 */
|
Chris@0
|
32 public function __construct(RouteMatchInterface $route_match) {
|
Chris@0
|
33 $this->routeMatch = $route_match;
|
Chris@0
|
34 }
|
Chris@0
|
35
|
Chris@0
|
36 /**
|
Chris@0
|
37 * {@inheritdoc}
|
Chris@0
|
38 */
|
Chris@0
|
39 public static function getLabel() {
|
Chris@0
|
40 return t('Layout Builder');
|
Chris@0
|
41 }
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * {@inheritdoc}
|
Chris@0
|
45 */
|
Chris@0
|
46 public function getContext($entity_type_id = NULL) {
|
Chris@0
|
47 if (!$entity_type_id) {
|
Chris@0
|
48 throw new \LogicException('Missing entity type ID');
|
Chris@0
|
49 }
|
Chris@0
|
50
|
Chris@0
|
51 $display = $this->getDisplay($entity_type_id);
|
Chris@0
|
52 return ($display && $display->isOverridable()) ? '1' : '0';
|
Chris@0
|
53 }
|
Chris@0
|
54
|
Chris@0
|
55 /**
|
Chris@0
|
56 * {@inheritdoc}
|
Chris@0
|
57 */
|
Chris@0
|
58 public function getCacheableMetadata($entity_type_id = NULL) {
|
Chris@0
|
59 if (!$entity_type_id) {
|
Chris@0
|
60 throw new \LogicException('Missing entity type ID');
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 $cacheable_metadata = new CacheableMetadata();
|
Chris@0
|
64 if ($display = $this->getDisplay($entity_type_id)) {
|
Chris@0
|
65 $cacheable_metadata->addCacheableDependency($display);
|
Chris@0
|
66 }
|
Chris@0
|
67 return $cacheable_metadata;
|
Chris@0
|
68 }
|
Chris@0
|
69
|
Chris@0
|
70 /**
|
Chris@0
|
71 * Returns the entity view display for a given entity type and view mode.
|
Chris@0
|
72 *
|
Chris@0
|
73 * @param string $entity_type_id
|
Chris@0
|
74 * The entity type ID.
|
Chris@0
|
75 *
|
Chris@0
|
76 * @return \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface|null
|
Chris@0
|
77 * The entity view display, if it exists.
|
Chris@0
|
78 */
|
Chris@0
|
79 protected function getDisplay($entity_type_id) {
|
Chris@0
|
80 if ($entity = $this->routeMatch->getParameter($entity_type_id)) {
|
Chris@0
|
81 if ($entity instanceof OverridesSectionStorageInterface) {
|
Chris@0
|
82 return $entity->getDefaultSectionStorage();
|
Chris@0
|
83 }
|
Chris@0
|
84 }
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 }
|