Scroll
Verificação de acesso + cacheabilidade
Проверщики доступа к маршруту, hook_entity_access() и все, что требуется для возврата объекта AccessResultInterface, должны добавить соответствующие метаданные кешируемости.
Если вы еще не читали, прочитайте cache tags, cache contexts и max-age.
Параметры проверки доступа
Средство проверки доступа получит различные параметры - по крайней мере, учетную запись пользователя (AccountInterface) и часто объект. Затем он примет решение о свойствах этих параметров.
Кэшируемая зависимость от $parameter должна быть добавлена, если изменение хотя бы одного свойства этого параметра изменит результат доступа.
Например:
$access_result = AccessResult::allowedIf($node->isPublished())
// Access result depends on a property of the object that might change: it is a cacheable dependency.
->addCacheableDependency($node);
Outro caso comum: resultado depende de propriedade imutavel (usualmente id UUID). Ex.: permitir se a conta e dona do objeto:
$access_result = AccessResult::allowedIf($node->getOwnerId() === $account->id())
// Access result depends on the node's owner, the owner might change.
->addCacheableDependency($node);
// Access result also depends on a user account, and the ID of the user account can never change. Hence we don't need to add $account as a cacheable dependency.
// But, if $account is the current user, and not some hardcoded user, we also need to make sure we vary this by the current user, so that we don't run this access check once and then reuse its result for all users.
if ($account->id() === \Drupal::currentUser()->id()) {
$access_result->cachePerUser();
}