function EntityTestHooks::entityAccess

Implements hook_entity_access().

Attributes

#[Hook('entity_access')]

File

core/modules/system/tests/modules/entity_test/src/Hook/EntityTestHooks.php, line 623

Class

EntityTestHooks
Hook implementations for entity_test.

Namespace

Drupal\entity_test\Hook

Code

public function entityAccess(EntityInterface $entity, $operation, AccountInterface $account) : AccessResultInterface {
  // 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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.