本文整理汇总了PHP中Drupal\Core\Entity\EntityInterface::getCacheTag方法的典型用法代码示例。如果您正苦于以下问题:PHP EntityInterface::getCacheTag方法的具体用法?PHP EntityInterface::getCacheTag怎么用?PHP EntityInterface::getCacheTag使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Entity\EntityInterface
的用法示例。
在下文中一共展示了EntityInterface::getCacheTag方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testEntityFormatter
/**
* Tests the entity formatter.
*/
public function testEntityFormatter()
{
$formatter = 'entity_reference_entity_view';
$field_name = $this->fieldName;
// Create the entity that will have the entity reference field.
$referencing_entity = entity_create($this->entityType, array('name' => $this->randomMachineName()));
$referencing_entity->save();
$referencing_entity->{$field_name}->entity = $this->referencedEntity;
$referencing_entity->{$field_name}->access = TRUE;
// Build the renderable array for the entity reference field.
$items = $referencing_entity->get($field_name);
$build = $items->view(array('type' => $formatter));
$expected_rendered_name_field = '<div class="field field-entity-test--name field-name-name field-type-string field-label-hidden">
<div class="field-items">
<div class="field-item">' . $this->referencedEntity->label() . '</div>
</div>
</div>
';
$expected_rendered_body_field = '<div class="field field-entity-test--body field-name-body field-type-text field-label-above">
<div class="field-label">Body: </div>
<div class="field-items">
<div class="field-item"><p>Hello, world!</p></div>
</div>
</div>
';
drupal_render($build[0]);
$this->assertEqual($build[0]['#markup'], 'default | ' . $this->referencedEntity->label() . $expected_rendered_name_field . $expected_rendered_body_field, format_string('The markup returned by the @formatter formatter is correct.', array('@formatter' => $formatter)));
$expected_cache_tags = Cache::mergeTags(\Drupal::entityManager()->getViewBuilder($this->entityType)->getCacheTag(), $this->referencedEntity->getCacheTag(), FilterFormat::load('full_html')->getCacheTag());
$this->assertEqual($build[0]['#cache']['tags'], $expected_cache_tags, format_string('The @formatter formatter has the expected cache tags.', array('@formatter' => $formatter)));
}
示例2: testReferencedEntity
/**
* Tests cache tags presence and invalidation of the entity when referenced.
*
* Tests the following cache tags:
* - entity type view cache tag: "<entity type>_view"
* - entity cache tag: "<entity type>:<entity ID>"
* - entity type list cache tag: "<entity type>_list"
* - referencing entity type view cache tag: "<referencing entity type>_view"
* - referencing entity type cache tag: "<referencing entity type>:<referencing entity ID>"
*/
public function testReferencedEntity()
{
$entity_type = $this->entity->getEntityTypeId();
$referencing_entity_path = $this->referencing_entity->getSystemPath();
$non_referencing_entity_path = $this->non_referencing_entity->getSystemPath();
$listing_path = 'entity_test/list/' . $entity_type . '_reference/' . $entity_type . '/' . $this->entity->id();
$empty_entity_listing_path = 'entity_test/list_empty/' . $entity_type;
$nonempty_entity_listing_path = 'entity_test/list_labels_alphabetically/' . $entity_type;
$render_cache_tags = array('rendered');
$theme_cache_tags = array('theme:stark', 'theme_global_settings');
$view_cache_tag = array();
if ($this->entity->getEntityType()->hasHandlerClass('view_builder')) {
$view_cache_tag = \Drupal::entityManager()->getViewBuilder($entity_type)->getCacheTag();
}
// Generate the cache tags for the (non) referencing entities.
$referencing_entity_cache_tags = Cache::mergeTags($this->referencing_entity->getCacheTag(), \Drupal::entityManager()->getViewBuilder('entity_test')->getCacheTag(), $this->entity->getCacheTag(), $this->getAdditionalCacheTagsForEntity($this->entity), $view_cache_tag);
$non_referencing_entity_cache_tags = Cache::mergeTags($this->non_referencing_entity->getCacheTag(), \Drupal::entityManager()->getViewBuilder('entity_test')->getCacheTag());
// 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(), $theme_cache_tags, $render_cache_tags);
$nonempty_entity_listing_cache_tags = Cache::mergeTags($this->entity->getEntityType()->getListCacheTags(), $this->entity->getCacheTag(), $this->getAdditionalCacheTagsForEntityListing($this->entity), $theme_cache_tags, $render_cache_tags);
$this->pass("Test referencing entity.", 'Debug');
$this->verifyPageCache($referencing_entity_path, 'MISS');
// Verify a cache hit, but also the presence of the correct cache tags.
$tags = Cache::mergeTags($render_cache_tags, $theme_cache_tags, $referencing_entity_cache_tags);
$this->verifyPageCache($referencing_entity_path, 'HIT', $tags);
// Also verify the existence of an entity render cache entry.
$cid = 'entity_view:entity_test:' . $this->referencing_entity->id() . ':full:stark:r.anonymous:' . date_default_timezone_get();
$tags = Cache::mergeTags($render_cache_tags, $referencing_entity_cache_tags);
$this->verifyRenderCache($cid, $tags);
$this->pass("Test non-referencing entity.", 'Debug');
$this->verifyPageCache($non_referencing_entity_path, 'MISS');
// Verify a cache hit, but also the presence of the correct cache tags.
$tags = Cache::mergeTags($render_cache_tags, $theme_cache_tags, $non_referencing_entity_cache_tags);
$this->verifyPageCache($non_referencing_entity_path, 'HIT', $tags);
// Also verify the existence of an entity render cache entry.
$cid = 'entity_view:entity_test:' . $this->non_referencing_entity->id() . ':full:stark:r.anonymous:' . date_default_timezone_get();
$tags = Cache::mergeTags($render_cache_tags, $non_referencing_entity_cache_tags);
$this->verifyRenderCache($cid, $tags);
$this->pass("Test listing of referencing entities.", 'Debug');
// Prime the page cache for the listing of referencing entities.
$this->verifyPageCache($listing_path, 'MISS');
// Verify a cache hit, but also the presence of the correct cache tags.
$tags = Cache::mergeTags($render_cache_tags, $theme_cache_tags, $referencing_entity_cache_tags);
$this->verifyPageCache($listing_path, 'HIT', $tags);
$this->pass("Test empty listing.", 'Debug');
// Prime the page cache for the empty listing.
$this->verifyPageCache($empty_entity_listing_path, 'MISS');
// Verify a cache hit, but also the presence of the correct cache tags.
$this->verifyPageCache($empty_entity_listing_path, 'HIT', $empty_entity_listing_cache_tags);
$this->pass("Test listing containing referenced entity.", 'Debug');
// Prime the page cache for the listing containing the referenced entity.
$this->verifyPageCache($nonempty_entity_listing_path, 'MISS');
// Verify a cache hit, but also the presence of the correct cache tags.
$this->verifyPageCache($nonempty_entity_listing_path, 'HIT', $nonempty_entity_listing_cache_tags);
// Verify that after modifying the referenced entity, there is a cache miss
// for every route except the one for the non-referencing entity.
$this->pass("Test modification of referenced entity.", 'Debug');
$this->entity->save();
$this->verifyPageCache($referencing_entity_path, 'MISS');
$this->verifyPageCache($listing_path, 'MISS');
$this->verifyPageCache($empty_entity_listing_path, 'MISS');
$this->verifyPageCache($nonempty_entity_listing_path, 'MISS');
$this->verifyPageCache($non_referencing_entity_path, 'HIT');
// Verify cache hits.
$this->verifyPageCache($referencing_entity_path, 'HIT');
$this->verifyPageCache($listing_path, 'HIT');
$this->verifyPageCache($empty_entity_listing_path, 'HIT');
$this->verifyPageCache($nonempty_entity_listing_path, 'HIT');
// Verify that after modifying the referencing entity, there is a cache miss
// for every route except the ones for the non-referencing entity and the
// empty entity listing.
$this->pass("Test modification of referencing entity.", 'Debug');
$this->referencing_entity->save();
$this->verifyPageCache($referencing_entity_path, 'MISS');
$this->verifyPageCache($listing_path, 'MISS');
$this->verifyPageCache($nonempty_entity_listing_path, 'HIT');
$this->verifyPageCache($non_referencing_entity_path, 'HIT');
$this->verifyPageCache($empty_entity_listing_path, 'HIT');
// Verify cache hits.
$this->verifyPageCache($referencing_entity_path, 'HIT');
$this->verifyPageCache($listing_path, 'HIT');
$this->verifyPageCache($nonempty_entity_listing_path, 'HIT');
// Verify that after modifying the non-referencing entity, there is a cache
// miss only for the non-referencing entity route.
$this->pass("Test modification of non-referencing entity.", 'Debug');
$this->non_referencing_entity->save();
$this->verifyPageCache($referencing_entity_path, 'HIT');
$this->verifyPageCache($listing_path, 'HIT');
//.........这里部分代码省略.........
示例3: cacheUntilEntityChanges
/**
* Convenience method, adds the entity's cache tag.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity whose cache tag to set on the access result.
*
* @return $this
*/
public function cacheUntilEntityChanges(EntityInterface $entity)
{
$this->addCacheTags($entity->getCacheTag());
return $this;
}
示例4: testReferencedEntity
/**
* Tests cache tags presence and invalidation of the entity when referenced.
*
* Tests the following cache tags:
* - "<entity type>_view:1"
* - "<entity type>:<entity ID>"
* - "<referencing entity type>_view:1"
* * - "<referencing entity type>:<referencing entity ID>"
*/
public function testReferencedEntity()
{
$entity_type = $this->entity->getEntityTypeId();
$referencing_entity_path = $this->referencing_entity->getSystemPath();
$non_referencing_entity_path = $this->non_referencing_entity->getSystemPath();
$listing_path = 'entity_test/list/' . $entity_type . '_reference/' . $entity_type . '/' . $this->entity->id();
$render_cache_tags = array('rendered:1');
$theme_cache_tags = array('theme:stark', 'theme_global_settings:1');
$view_cache_tag = array();
if ($this->entity->getEntityType()->hasControllerClass('view_builder')) {
$view_cache_tag = \Drupal::entityManager()->getViewBuilder($entity_type)->getCacheTag();
}
// Generate the cache tags for the (non) referencing entities.
$referencing_entity_cache_tags = NestedArray::mergeDeep($this->referencing_entity->getCacheTag(), \Drupal::entityManager()->getViewBuilder('entity_test')->getCacheTag(), $this->entity->getCacheTag(), $view_cache_tag);
$referencing_entity_cache_tags = explode(' ', HtmlViewSubscriber::convertCacheTagsToHeader($referencing_entity_cache_tags));
$referencing_entity_cache_tags = array_merge($referencing_entity_cache_tags, $this->getAdditionalCacheTagsForEntity($this->entity));
$non_referencing_entity_cache_tags = NestedArray::mergeDeep($this->non_referencing_entity->getCacheTag(), \Drupal::entityManager()->getViewBuilder('entity_test')->getCacheTag());
$non_referencing_entity_cache_tags = explode(' ', HtmlViewSubscriber::convertCacheTagsToHeader($non_referencing_entity_cache_tags));
$this->pass("Test referencing entity.", 'Debug');
$this->verifyPageCache($referencing_entity_path, 'MISS');
// Verify a cache hit, but also the presence of the correct cache tags.
$tags = array_merge($render_cache_tags, $theme_cache_tags, $referencing_entity_cache_tags);
$this->verifyPageCache($referencing_entity_path, 'HIT', $tags);
// Also verify the existence of an entity render cache entry.
$cid = 'entity_view:entity_test:' . $this->referencing_entity->id() . ':full:stark:r.anonymous:' . date_default_timezone_get();
$tags = array_merge($render_cache_tags, $referencing_entity_cache_tags);
$this->verifyRenderCache($cid, $tags);
$this->pass("Test non-referencing entity.", 'Debug');
$this->verifyPageCache($non_referencing_entity_path, 'MISS');
// Verify a cache hit, but also the presence of the correct cache tags.
$tags = array_merge($render_cache_tags, $theme_cache_tags, $non_referencing_entity_cache_tags);
$this->verifyPageCache($non_referencing_entity_path, 'HIT', $tags);
// Also verify the existence of an entity render cache entry.
$cid = 'entity_view:entity_test:' . $this->non_referencing_entity->id() . ':full:stark:r.anonymous:' . date_default_timezone_get();
$tags = array_merge($render_cache_tags, $non_referencing_entity_cache_tags);
$this->verifyRenderCache($cid, $tags);
$this->pass("Test listing of referencing entities.", 'Debug');
// Prime the page cache for the listing of referencing entities.
$this->verifyPageCache($listing_path, 'MISS');
// Verify a cache hit, but also the presence of the correct cache tags.
$tags = array_merge($render_cache_tags, $theme_cache_tags, $referencing_entity_cache_tags);
$this->verifyPageCache($listing_path, 'HIT', $tags);
// Verify that after modifying the referenced entity, there is a cache miss
// for both the referencing entity, and the listing of referencing entities,
// but not for the non-referencing entity.
$this->pass("Test modification of referenced entity.", 'Debug');
$this->entity->save();
$this->verifyPageCache($referencing_entity_path, 'MISS');
$this->verifyPageCache($listing_path, 'MISS');
$this->verifyPageCache($non_referencing_entity_path, 'HIT');
// Verify cache hits.
$this->verifyPageCache($referencing_entity_path, 'HIT');
$this->verifyPageCache($listing_path, 'HIT');
// Verify that after modifying the referencing entity, there is a cache miss
// for both the referencing entity, and the listing of referencing entities,
// but not for the non-referencing entity.
$this->pass("Test modification of referencing entity.", 'Debug');
$this->referencing_entity->save();
$this->verifyPageCache($referencing_entity_path, 'MISS');
$this->verifyPageCache($listing_path, 'MISS');
$this->verifyPageCache($non_referencing_entity_path, 'HIT');
// Verify cache hits.
$this->verifyPageCache($referencing_entity_path, 'HIT');
$this->verifyPageCache($listing_path, 'HIT');
// Verify that after modifying the non-referencing entity, there is a cache
// miss for only the non-referencing entity, not for the referencing entity,
// nor for the listing of referencing entities.
$this->pass("Test modification of non-referencing entity.", 'Debug');
$this->non_referencing_entity->save();
$this->verifyPageCache($referencing_entity_path, 'HIT');
$this->verifyPageCache($listing_path, 'HIT');
$this->verifyPageCache($non_referencing_entity_path, 'MISS');
// Verify cache hits.
$this->verifyPageCache($non_referencing_entity_path, 'HIT');
if ($this->entity->getEntityType()->hasControllerClass('view_builder')) {
// Verify that after modifying the entity's display, there is a cache miss
// for both the referencing entity, and the listing of referencing
// entities, but not for the non-referencing entity.
$referenced_entity_view_mode = $this->selectViewMode($this->entity->getEntityTypeId());
$this->pass("Test modification of referenced entity's '{$referenced_entity_view_mode}' display.", 'Debug');
$entity_display = entity_get_display($entity_type, $this->entity->bundle(), $referenced_entity_view_mode);
$entity_display->save();
$this->verifyPageCache($referencing_entity_path, 'MISS');
$this->verifyPageCache($listing_path, 'MISS');
$this->verifyPageCache($non_referencing_entity_path, 'HIT');
// Verify cache hits.
$this->verifyPageCache($referencing_entity_path, 'HIT');
$this->verifyPageCache($listing_path, 'HIT');
}
$bundle_entity_type = $this->entity->getEntityType()->getBundleEntityType();
if ($bundle_entity_type !== 'bundle') {
//.........这里部分代码省略.........