annotate core/modules/layout_builder/src/Cache/LayoutBuilderIsActiveCacheContext.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents c75dbcec494b
children
rev   line source
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@5 16 *
Chris@5 17 * @internal
Chris@5 18 * Tagged services are internal.
Chris@0 19 */
Chris@0 20 class LayoutBuilderIsActiveCacheContext implements CalculatedCacheContextInterface {
Chris@0 21
Chris@0 22 /**
Chris@0 23 * The current route match.
Chris@0 24 *
Chris@0 25 * @var \Drupal\Core\Routing\RouteMatchInterface
Chris@0 26 */
Chris@0 27 protected $routeMatch;
Chris@0 28
Chris@0 29 /**
Chris@0 30 * LayoutBuilderCacheContext constructor.
Chris@0 31 *
Chris@0 32 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
Chris@0 33 * The current route match.
Chris@0 34 */
Chris@0 35 public function __construct(RouteMatchInterface $route_match) {
Chris@0 36 $this->routeMatch = $route_match;
Chris@0 37 }
Chris@0 38
Chris@0 39 /**
Chris@0 40 * {@inheritdoc}
Chris@0 41 */
Chris@0 42 public static function getLabel() {
Chris@0 43 return t('Layout Builder');
Chris@0 44 }
Chris@0 45
Chris@0 46 /**
Chris@0 47 * {@inheritdoc}
Chris@0 48 */
Chris@0 49 public function getContext($entity_type_id = NULL) {
Chris@0 50 if (!$entity_type_id) {
Chris@0 51 throw new \LogicException('Missing entity type ID');
Chris@0 52 }
Chris@0 53
Chris@0 54 $display = $this->getDisplay($entity_type_id);
Chris@0 55 return ($display && $display->isOverridable()) ? '1' : '0';
Chris@0 56 }
Chris@0 57
Chris@0 58 /**
Chris@0 59 * {@inheritdoc}
Chris@0 60 */
Chris@0 61 public function getCacheableMetadata($entity_type_id = NULL) {
Chris@0 62 if (!$entity_type_id) {
Chris@0 63 throw new \LogicException('Missing entity type ID');
Chris@0 64 }
Chris@0 65
Chris@0 66 $cacheable_metadata = new CacheableMetadata();
Chris@0 67 if ($display = $this->getDisplay($entity_type_id)) {
Chris@0 68 $cacheable_metadata->addCacheableDependency($display);
Chris@0 69 }
Chris@0 70 return $cacheable_metadata;
Chris@0 71 }
Chris@0 72
Chris@0 73 /**
Chris@0 74 * Returns the entity view display for a given entity type and view mode.
Chris@0 75 *
Chris@0 76 * @param string $entity_type_id
Chris@0 77 * The entity type ID.
Chris@0 78 *
Chris@0 79 * @return \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface|null
Chris@0 80 * The entity view display, if it exists.
Chris@0 81 */
Chris@0 82 protected function getDisplay($entity_type_id) {
Chris@0 83 if ($entity = $this->routeMatch->getParameter($entity_type_id)) {
Chris@0 84 if ($entity instanceof OverridesSectionStorageInterface) {
Chris@0 85 return $entity->getDefaultSectionStorage();
Chris@0 86 }
Chris@0 87 }
Chris@0 88 }
Chris@0 89
Chris@0 90 }