class LayoutBuilderIsActiveCacheContext

Same name and namespace in other branches
  1. 9 core/modules/layout_builder/src/Cache/LayoutBuilderIsActiveCacheContext.php \Drupal\layout_builder\Cache\LayoutBuilderIsActiveCacheContext
  2. 8.9.x core/modules/layout_builder/src/Cache/LayoutBuilderIsActiveCacheContext.php \Drupal\layout_builder\Cache\LayoutBuilderIsActiveCacheContext
  3. 10 core/modules/layout_builder/src/Cache/LayoutBuilderIsActiveCacheContext.php \Drupal\layout_builder\Cache\LayoutBuilderIsActiveCacheContext

Determines whether Layout Builder is active for a given entity type or not.

Cache context ID: 'layout_builder_is_active:%entity_type_id', e.g. 'layout_builder_is_active:node' (to vary by whether custom layout overrides are allowed for the Node entity specified by the route parameter).

@internal Tagged services are internal.

Hierarchy

Expanded class hierarchy of LayoutBuilderIsActiveCacheContext

1 file declares its use of LayoutBuilderIsActiveCacheContext
LayoutBuilderIsActiveCacheContextTest.php in core/modules/layout_builder/tests/src/Unit/LayoutBuilderIsActiveCacheContextTest.php
1 string reference to 'LayoutBuilderIsActiveCacheContext'
layout_builder.services.yml in core/modules/layout_builder/layout_builder.services.yml
core/modules/layout_builder/layout_builder.services.yml
1 service uses LayoutBuilderIsActiveCacheContext
cache_context.layout_builder_is_active in core/modules/layout_builder/layout_builder.services.yml
Drupal\layout_builder\Cache\LayoutBuilderIsActiveCacheContext

File

core/modules/layout_builder/src/Cache/LayoutBuilderIsActiveCacheContext.php, line 22

Namespace

Drupal\layout_builder\Cache
View source
class LayoutBuilderIsActiveCacheContext implements CalculatedCacheContextInterface {
    
    /**
     * The current route match.
     *
     * @var \Drupal\Core\Routing\RouteMatchInterface
     */
    protected $routeMatch;
    
    /**
     * LayoutBuilderCacheContext constructor.
     *
     * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
     *   The current route match.
     */
    public function __construct(RouteMatchInterface $route_match) {
        $this->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 FieldableEntityInterface) {
                // @todo Expand to work for all view modes in
                //   https://www.drupal.org/node/2907413.
                $view_mode = 'full';
                $display = EntityViewDisplay::collectRenderDisplay($entity, $view_mode);
                if ($display instanceof LayoutEntityDisplayInterface) {
                    return $display;
                }
            }
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
LayoutBuilderIsActiveCacheContext::$routeMatch protected property The current route match.
LayoutBuilderIsActiveCacheContext::getCacheableMetadata public function Gets the cacheability metadata for the context based on the parameter value. Overrides CalculatedCacheContextInterface::getCacheableMetadata
LayoutBuilderIsActiveCacheContext::getContext public function Returns the string representation of the cache context. Overrides CalculatedCacheContextInterface::getContext
LayoutBuilderIsActiveCacheContext::getDisplay protected function Returns the entity view display for a given entity type and view mode.
LayoutBuilderIsActiveCacheContext::getLabel public static function Returns the label of the cache context. Overrides CalculatedCacheContextInterface::getLabel
LayoutBuilderIsActiveCacheContext::__construct public function LayoutBuilderCacheContext constructor.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.