3 namespace Drupal\entity_test;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Entity\EntityAccessControlHandler;
8 use Drupal\Core\Session\AccountInterface;
9 use Drupal\entity_test\Entity\EntityTestLabel;
12 * Defines the access control handler for the test entity type.
14 * @see \Drupal\entity_test\Entity\EntityTest
15 * @see \Drupal\entity_test\Entity\EntityTestBaseFieldDisplay
16 * @see \Drupal\entity_test\Entity\EntityTestCache
17 * @see \Drupal\entity_test\Entity\EntityTestMul
18 * @see \Drupal\entity_test\Entity\EntityTestMulRev
19 * @see \Drupal\entity_test\Entity\EntityTestRev
20 * @see \Drupal\entity_test\Entity\EntityTestWithBundle
21 * @see \Drupal\entity_test\Entity\EntityTestStringId
23 class EntityTestAccessControlHandler extends EntityAccessControlHandler {
26 * Allows to grant access to just the labels.
30 protected $viewLabelOperation = TRUE;
35 protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
36 /** @var \Drupal\entity_test\Entity\EntityTest $entity */
38 // Always forbid access to entities with the label 'forbid_access', used for
39 // \Drupal\system\Tests\Entity\EntityAccessControlHandlerTest::testDefaultEntityAccess().
40 if ($entity->label() == 'forbid_access') {
41 return AccessResult::forbidden();
44 if ($operation === 'view label' && $entity instanceof EntityTestLabel) {
45 // Viewing the label of the 'entity_test_label' entity type is allowed.
46 return AccessResult::allowed();
48 elseif (in_array($operation, ['view', 'view label'])) {
49 if (!$entity->isDefaultTranslation()) {
50 return AccessResult::allowedIfHasPermission($account, 'view test entity translations');
52 return AccessResult::allowedIfHasPermission($account, 'view test entity');
54 elseif (in_array($operation, ['update', 'delete'])) {
55 return AccessResult::allowedIfHasPermission($account, 'administer entity_test content');
59 return AccessResult::neutral();
66 protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
67 return AccessResult::allowedIfHasPermissions($account, [
68 'administer entity_test content',
69 'administer entity_test_with_bundle content',
70 'create ' . $entity_bundle . ' entity_test_with_bundle entities',