EntityTestRevlogAccessControlHandler.php

Same filename in other branches
  1. 11.x core/modules/system/tests/modules/entity_test_revlog/src/EntityTestRevlogAccessControlHandler.php

Namespace

Drupal\entity_test_revlog

File

core/modules/system/tests/modules/entity_test_revlog/src/EntityTestRevlogAccessControlHandler.php

View source
<?php

namespace Drupal\entity_test_revlog;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Session\AccountInterface;
use Drupal\entity_test_revlog\Entity\EntityTestWithRevisionLog;

/**
 * Defines the access control handler for test entity types.
 */
class EntityTestRevlogAccessControlHandler extends EntityAccessControlHandler {
    
    /**
     * {@inheritdoc}
     */
    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();
    }

}

Classes

Title Deprecated Summary
EntityTestRevlogAccessControlHandler Defines the access control handler for test entity types.

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