function EntityTestRevlogAccessControlHandler::checkAccess

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

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.