X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Flayout_builder%2Fsrc%2FCache%2FLayoutBuilderIsActiveCacheContext.php;fp=web%2Fcore%2Fmodules%2Flayout_builder%2Fsrc%2FCache%2FLayoutBuilderIsActiveCacheContext.php;h=3c3bc25c400585e5ee2d09365fdb7dae69c7dd6e;hp=0000000000000000000000000000000000000000;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0 diff --git a/web/core/modules/layout_builder/src/Cache/LayoutBuilderIsActiveCacheContext.php b/web/core/modules/layout_builder/src/Cache/LayoutBuilderIsActiveCacheContext.php new file mode 100644 index 000000000..3c3bc25c4 --- /dev/null +++ b/web/core/modules/layout_builder/src/Cache/LayoutBuilderIsActiveCacheContext.php @@ -0,0 +1,87 @@ +routeMatch = $route_match; + } + + /** + * {@inheritdoc} + */ + public static function getLabel() { + return t('Layout Builder'); + } + + /** + * {@inheritdoc} + */ + public function getContext($entity_type_id = NULL) { + if (!$entity_type_id) { + throw new \LogicException('Missing entity type ID'); + } + + $display = $this->getDisplay($entity_type_id); + return ($display && $display->isOverridable()) ? '1' : '0'; + } + + /** + * {@inheritdoc} + */ + public function getCacheableMetadata($entity_type_id = NULL) { + if (!$entity_type_id) { + throw new \LogicException('Missing entity type ID'); + } + + $cacheable_metadata = new CacheableMetadata(); + if ($display = $this->getDisplay($entity_type_id)) { + $cacheable_metadata->addCacheableDependency($display); + } + return $cacheable_metadata; + } + + /** + * Returns the entity view display for a given entity type and view mode. + * + * @param string $entity_type_id + * The entity type ID. + * + * @return \Drupal\layout_builder\Entity\LayoutEntityDisplayInterface|null + * The entity view display, if it exists. + */ + protected function getDisplay($entity_type_id) { + if ($entity = $this->routeMatch->getParameter($entity_type_id)) { + if ($entity instanceof OverridesSectionStorageInterface) { + return $entity->getDefaultSectionStorage(); + } + } + } + +}