"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/layout_builder/src/Cache/LayoutBuilderIsActiveCacheContext.php" (26 Nov 2020, 2404 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "LayoutBuilderIsActiveCacheContext.php" see the Fossies "Dox" file reference documentation.

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