function LatestRevisionCheck::access

Same name in other branches
  1. 9 core/modules/content_moderation/src/Access/LatestRevisionCheck.php \Drupal\content_moderation\Access\LatestRevisionCheck::access()
  2. 10 core/modules/content_moderation/src/Access/LatestRevisionCheck.php \Drupal\content_moderation\Access\LatestRevisionCheck::access()
  3. 11.x core/modules/content_moderation/src/Access/LatestRevisionCheck.php \Drupal\content_moderation\Access\LatestRevisionCheck::access()

Checks that there is a pending revision available.

This checker assumes the presence of an '_entity_access' requirement key in the same form as used by EntityAccessCheck.

Parameters

\Symfony\Component\Routing\Route $route: The route to check against.

\Drupal\Core\Routing\RouteMatchInterface $route_match: The parametrized route.

\Drupal\Core\Session\AccountInterface $account: The current user account.

Return value

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

See also

\Drupal\Core\Entity\EntityAccessCheck

File

core/modules/content_moderation/src/Access/LatestRevisionCheck.php, line 55

Class

LatestRevisionCheck
Access check for the entity moderation tab.

Namespace

Drupal\content_moderation\Access

Code

public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) {
    // This tab should not show up unless there's a reason to show it.
    $entity = $this->loadEntity($route, $route_match);
    if ($this->moderationInfo
        ->hasPendingRevision($entity)) {
        // Check the global permissions first.
        $access_result = AccessResult::allowedIfHasPermissions($account, [
            'view latest version',
            'view any unpublished content',
        ]);
        if (!$access_result->isAllowed()) {
            // Check entity owner access.
            $owner_access = AccessResult::allowedIfHasPermissions($account, [
                'view latest version',
                'view own unpublished content',
            ]);
            $owner_access = $owner_access->andIf(AccessResult::allowedIf($entity instanceof EntityOwnerInterface && $entity->getOwnerId() == $account->id()));
            $access_result = $access_result->orIf($owner_access);
        }
        return $access_result->addCacheableDependency($entity);
    }
    return AccessResult::forbidden('No pending revision for moderated entity.')->addCacheableDependency($entity);
}

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