getAdditionalCacheTagsForEntity example

// Also verify the existence of an entity render cache entry, if this entity     // type supports render caching.     if (\Drupal::entityTypeManager()->getDefinition($entity_type)->isRenderCacheable()) {
      $cache_keys = ['entity_view', $entity_type$this->entity->id()$view_mode];
      $cid = $this->createCacheId($cache_keys$entity_cache_contexts);
      $redirected_cid = NULL;
      $additional_cache_contexts = $this->getAdditionalCacheContextsForEntity($this->entity);
      if (count($additional_cache_contexts)) {
        $redirected_cid = $this->createCacheId($cache_keys, Cache::mergeContexts($entity_cache_contexts$additional_cache_contexts));
      }
      $expected_cache_tags = Cache::mergeTags($cache_tag$view_cache_tag);
      $expected_cache_tags = Cache::mergeTags($expected_cache_tags$this->getAdditionalCacheTagsForEntity($this->entity));
      $expected_cache_tags = Cache::mergeTags($expected_cache_tags[$render_cache_tag]);
      $this->verifyRenderCache($cid$expected_cache_tags$redirected_cid);
    }

    // Verify that after modifying the entity, there is a cache miss.     $this->entity->save();
    $this->verifyPageCache($entity_url, 'MISS');

    // Verify a cache hit.     $this->verifyPageCache($entity_url, 'HIT');

    
$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.     $referencing_entity_cache_tags = Cache::mergeTags($this->referencingEntity->getCacheTags(), \Drupal::entityTypeManager()->getViewBuilder('entity_test')->getCacheTags());
    // Includes the main entity's cache tags, since this entity references it.     $referencing_entity_cache_tags = Cache::mergeTags($referencing_entity_cache_tags$this->entity->getCacheTags());
    $referencing_entity_cache_tags = Cache::mergeTags($referencing_entity_cache_tags$this->getAdditionalCacheTagsForEntity($this->entity));
    $referencing_entity_cache_tags = Cache::mergeTags($referencing_entity_cache_tags$view_cache_tag);
    $referencing_entity_cache_tags = Cache::mergeTags($referencing_entity_cache_tags$cache_context_tags);
    $referencing_entity_cache_tags = Cache::mergeTags($referencing_entity_cache_tags['rendered']);

    $non_referencing_entity_cache_tags = Cache::mergeTags($this->nonReferencingEntity->getCacheTags(), \Drupal::entityTypeManager()->getViewBuilder('entity_test')->getCacheTags());
    $non_referencing_entity_cache_tags = Cache::mergeTags($non_referencing_entity_cache_tags['rendered']);

    // Generate the cache tags for all two possible entity listing paths.     // 1. list cache tag only (listing query has no match)     // 2. list cache tag plus entity cache tag (listing query has a match)     $empty_entity_listing_cache_tags = Cache::mergeTags($this->entity->getEntityType()->getListCacheTags()$page_cache_tags);

    
// Expected keys, contexts, and tags for the block.     // @see \Drupal\block\BlockViewBuilder::viewMultiple()     $expected_block_cache_keys = ['entity_view', 'block', $block->id()];
    $expected_block_cache_contexts = ['languages:' . LanguageInterface::TYPE_INTERFACE, 'theme', 'user.permissions'];
    $expected_block_cache_tags = Cache::mergeTags(['block_view', 'rendered']$block->getCacheTags());
    $expected_block_cache_tags = Cache::mergeTags($expected_block_cache_tags$block->getPlugin()->getCacheTags());

    // Expected contexts and tags for the BlockContent entity.     // @see \Drupal\Core\Entity\EntityViewBuilder::getBuildDefaults().     $expected_entity_cache_contexts = ['theme'];
    $expected_entity_cache_tags = Cache::mergeTags(['block_content_view']$this->entity->getCacheTags());
    $expected_entity_cache_tags = Cache::mergeTags($expected_entity_cache_tags$this->getAdditionalCacheTagsForEntity($this->entity));

    // Verify that what was render cached matches the above expectations.     $cid = $this->createCacheId($expected_block_cache_keys$expected_block_cache_contexts);
    $redirected_cid = $this->createCacheId($expected_block_cache_keys, Cache::mergeContexts($expected_block_cache_contexts$expected_entity_cache_contexts));
    $this->verifyRenderCache($cid, Cache::mergeTags($expected_block_cache_tags$expected_entity_cache_tags)($cid !== $redirected_cid) ? $redirected_cid : NULL);
  }

}
Home | Imprint | This part of the site doesn't use cookies.