annotate core/modules/layout_builder/src/Cache/LayoutBuilderIsActiveCacheContext.php @ 19:fa3358dc1485 tip

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