comparison core/modules/layout_builder/src/Cache/LayoutBuilderIsActiveCacheContext.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents
children af1871eacc83
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
1 <?php
2
3 namespace Drupal\layout_builder\Cache;
4
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;
9
10 /**
11 * Determines whether Layout Builder is active for a given entity type or not.
12 *
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).
16 */
17 class LayoutBuilderIsActiveCacheContext implements CalculatedCacheContextInterface {
18
19 /**
20 * The current route match.
21 *
22 * @var \Drupal\Core\Routing\RouteMatchInterface
23 */
24 protected $routeMatch;
25
26 /**
27 * LayoutBuilderCacheContext constructor.
28 *
29 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
30 * The current route match.
31 */
32 public function __construct(RouteMatchInterface $route_match) {
33 $this->routeMatch = $route_match;
34 }
35
36 /**
37 * {@inheritdoc}
38 */
39 public static function getLabel() {
40 return t('Layout Builder');
41 }
42
43 /**
44 * {@inheritdoc}
45 */
46 public function getContext($entity_type_id = NULL) {
47 if (!$entity_type_id) {
48 throw new \LogicException('Missing entity type ID');
49 }
50
51 $display = $this->getDisplay($entity_type_id);
52 return ($display && $display->isOverridable()) ? '1' : '0';
53 }
54
55 /**
56 * {@inheritdoc}
57 */
58 public function getCacheableMetadata($entity_type_id = NULL) {
59 if (!$entity_type_id) {
60 throw new \LogicException('Missing entity type ID');
61 }
62
63 $cacheable_metadata = new CacheableMetadata();
64 if ($display = $this->getDisplay($entity_type_id)) {
65 $cacheable_metadata->addCacheableDependency($display);
66 }
67 return $cacheable_metadata;
68 }
69
70 /**
71 * Returns the entity view display for a given entity type and view mode.
72 *
73 * @param string $entity_type_id
74 * The entity type ID.
75 *
76 * @return \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface|null
77 * The entity view display, if it exists.
78 */
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();
83 }
84 }
85 }
86
87 }