function EntityTestRevlogAccessControlHandler::checkAccess

Same name and namespace in other branches
  1. 11.x core/modules/system/tests/modules/entity_test_revlog/src/EntityTestRevlogAccessControlHandler.php \Drupal\entity_test_revlog\EntityTestRevlogAccessControlHandler::checkAccess()

Performs access checks.

This method is supposed to be overwritten by extending classes that do their own custom access checking.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity for which to check access.

string $operation: The entity operation. Usually one of 'view', 'view label', 'update' or 'delete'.

\Drupal\Core\Session\AccountInterface $account: The user for which to check access.

Return value

\Drupal\Core\Access\AccessResultInterface The access result.

Overrides EntityAccessControlHandler::checkAccess

File

core/modules/system/tests/modules/entity_test_revlog/src/EntityTestRevlogAccessControlHandler.php, line 19

Class

EntityTestRevlogAccessControlHandler
Defines the access control handler for test entity types.

Namespace

Drupal\entity_test_revlog

Code

protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
  assert($entity instanceof EntityTestWithRevisionLog);
  // Access to revisions is based on labels, so access can vary by individual
  // revisions, since the 'name' field can vary by revision.
  $labels = explode(',', $entity->label());
  $labels = array_map('trim', $labels);
  if (in_array($operation, [
    'view',
    'view label',
    'view all revisions',
    'view revision',
  ], TRUE)) {
    return AccessResult::allowedIf(in_array($operation, $labels, TRUE));
  }
  elseif ($operation === 'revert') {
    return AccessResult::allowedIf(!$entity->isDefaultRevision() && !$entity->isLatestRevision() && in_array('revert', $labels, TRUE));
  }
  elseif ($operation === 'delete revision') {
    return AccessResult::allowedIf(!$entity->isLatestRevision() && in_array('delete revision', $labels, TRUE));
  }
  // No opinion.
  return AccessResult::neutral();
}

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