function ResourceTestBase::getExpectedCollectionCacheability
Same name in other branches
- 9 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getExpectedCollectionCacheability()
- 8.9.x core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getExpectedCollectionCacheability()
- 10 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getExpectedCollectionCacheability()
Computes the cacheability for a given entity collection.
Parameters
\Drupal\Core\Session\AccountInterface $account: An account for which cacheability should be computed (cacheability is dependent on access).
\Drupal\Core\Entity\EntityInterface[] $collection: The entities for which cacheability should be computed.
array $sparse_fieldset: (optional) If a sparse fieldset is being requested, limit the expected cacheability for the collection entities' fields to just those in the fieldset. NULL means all fields.
bool $filtered: Whether the collection is filtered or not.
Return value
\Drupal\Core\Cache\CacheableMetadata The expected cacheability for the given entity collection.
1 call to ResourceTestBase::getExpectedCollectionCacheability()
- ResourceTestBase::getExpectedCollectionResponse in core/
modules/ jsonapi/ tests/ src/ Functional/ ResourceTestBase.php - Returns a JSON:API collection document for the expected entities.
5 methods override ResourceTestBase::getExpectedCollectionCacheability()
- BlockTest::getExpectedCollectionCacheability in core/
modules/ jsonapi/ tests/ src/ Functional/ BlockTest.php - Computes the cacheability for a given entity collection.
- CommentTest::getExpectedCollectionCacheability in core/
modules/ jsonapi/ tests/ src/ Functional/ CommentTest.php - Computes the cacheability for a given entity collection.
- ContentLanguageSettingsTest::getExpectedCollectionCacheability in core/
modules/ jsonapi/ tests/ src/ Functional/ ContentLanguageSettingsTest.php - Computes the cacheability for a given entity collection.
- EntityTestTest::getExpectedCollectionCacheability in core/
modules/ jsonapi/ tests/ src/ Functional/ EntityTestTest.php - Computes the cacheability for a given entity collection.
- ShortcutTest::getExpectedCollectionCacheability in core/
modules/ jsonapi/ tests/ src/ Functional/ ShortcutTest.php - Computes the cacheability for a given entity collection.
File
-
core/
modules/ jsonapi/ tests/ src/ Functional/ ResourceTestBase.php, line 565
Class
- ResourceTestBase
- Subclass this for every JSON:API resource type.
Namespace
Drupal\Tests\jsonapi\FunctionalCode
protected static function getExpectedCollectionCacheability(AccountInterface $account, array $collection, ?array $sparse_fieldset = NULL, $filtered = FALSE) {
$cacheability = array_reduce($collection, function (CacheableMetadata $cacheability, EntityInterface $entity) use ($sparse_fieldset, $account) {
$access_result = static::entityAccess($entity, 'view', $account);
if (!$access_result->isAllowed()) {
$access_result = static::entityAccess($entity, 'view label', $account)->addCacheableDependency($access_result);
}
$cacheability->addCacheableDependency($access_result);
if ($access_result->isAllowed()) {
$cacheability->addCacheableDependency($entity);
if ($entity instanceof FieldableEntityInterface) {
foreach ($entity as $field_name => $field_item_list) {
/** @var \Drupal\Core\Field\FieldItemListInterface $field_item_list */
if (is_null($sparse_fieldset) || in_array($field_name, $sparse_fieldset)) {
$field_access = static::entityFieldAccess($entity, $field_name, 'view', $account);
$cacheability->addCacheableDependency($field_access);
if ($field_access->isAllowed()) {
foreach ($field_item_list as $field_item) {
/** @var \Drupal\Core\Field\FieldItemInterface $field_item */
foreach (TypedDataInternalPropertiesHelper::getNonInternalProperties($field_item) as $property) {
$cacheability->addCacheableDependency(CacheableMetadata::createFromObject($property));
}
}
}
}
}
}
}
return $cacheability;
}, new CacheableMetadata());
$entity_type = reset($collection)->getEntityType();
$cacheability->addCacheTags([
'http_response',
]);
$cacheability->addCacheTags($entity_type->getListCacheTags());
$cache_contexts = [
// Cache contexts for JSON:API URL query parameters.
'url.query_args',
// Drupal defaults.
'url.site',
];
// If the entity type is revisionable, add a resource version cache context.
$cache_contexts = Cache::mergeContexts($cache_contexts, $entity_type->isRevisionable() ? [
'url.query_args:resourceVersion',
] : []);
$cacheability->addCacheContexts($cache_contexts);
return $cacheability;
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.