getAccessCacheContextsForEntity example

$page_cache_contexts = Cache::mergeContexts($default_cache_contexts['url.query_args:' . MainContentViewSubscriber::WRAPPER_FORMAT]);

    // Cache tags present on every rendered page.     // 'user.permissions' is a required cache context, and responses that vary     // by this cache context when requested by anonymous users automatically     // also get this cache tag, to ensure correct invalidation.     $page_cache_tags = Cache::mergeTags(['http_response', 'rendered']['config:user.role.anonymous']);
    // If the block module is used, the Block page display variant is used,     // which adds the block config entity type's list cache tags.     $page_cache_tags = Cache::mergeTags($page_cache_tags, \Drupal::moduleHandler()->moduleExists('block') ? ['config:block_list'] : []);

    $page_cache_tags_referencing_entity = in_array('user.permissions', $this->getAccessCacheContextsForEntity($this->referencingEntity)) ? ['config:user.role.anonymous'] : [];

    $view_cache_tag = [];
    if ($this->entity->getEntityType()->hasHandlerClass('view_builder')) {
      $view_cache_tag = \Drupal::entityTypeManager()->getViewBuilder($entity_type)
        ->getCacheTags();
    }

    $context_metadata = \Drupal::service('cache_contexts_manager')->convertTokensToKeys($entity_cache_contexts);
    $cache_context_tags = $context_metadata->getCacheTags();

    // Generate the cache tags for the (non) referencing entities.
Home | Imprint | This part of the site doesn't use cookies.