You are here

function entity_test_entity_access in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/modules/entity_test/entity_test.module \entity_test_entity_access()

Implements hook_entity_access().

1 string reference to 'entity_test_entity_access'
EntityAccessControlHandlerTest::testHooks in core/tests/Drupal/KernelTests/Core/Entity/EntityAccessControlHandlerTest.php
Tests hook invocations.

File

core/modules/system/tests/modules/entity_test/entity_test.module, line 729
Test module for the entity API providing several entity types for testing.

Code

function entity_test_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {

  // Only apply to the 'entity_test' entities.
  if ($entity
    ->getEntityType()
    ->getProvider() != 'entity_test') {
    return AccessResult::neutral();
  }
  \Drupal::state()
    ->set('entity_test_entity_access', TRUE);

  // Attempt to allow access to entities with the title forbid_access,
  // this will be overridden by
  // \Drupal\entity_test\EntityTestAccessControlHandler::checkAccess().
  if ($entity
    ->label() == 'forbid_access') {
    return AccessResult::allowed();
  }

  // Create specific labels to allow or deny access based on certain test
  // conditions.
  // @see \Drupal\KernelTests\Core\Entity\EntityAccessControlHandlerTest
  if ($entity
    ->label() == 'Accessible') {
    return AccessResult::allowed();
  }
  if ($entity
    ->label() == 'Inaccessible') {
    return AccessResult::forbidden();
  }

  // Uncacheable because the access result depends on a State key-value pair and
  // might therefore change at any time.
  $condition = \Drupal::state()
    ->get("entity_test_entity_access.{$operation}." . $entity
    ->id(), FALSE);
  return AccessResult::allowedIf($condition)
    ->setCacheMaxAge(0);
}