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