Same name and namespace in other branches
  1. 8.x-1.x entity_clone.module \entity_clone_entity_access() 1 commentaire

Implements hook_entity_access().

Fichier

./entity_clone.module, line 142

Code

function entity_clone_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
    if ($operation !== 'clone') {
        return AccessResult::neutral();
    }
    $cache = new CacheableMetadata();
    $cache->addCacheContexts([
        'user.permissions',
    ]);
    // Deny access if the user cannot clone the entity.
    $access = AccessResult::forbiddenIf(!$account->hasPermission('clone ' . $entity->getEntityTypeId() . ' entity') && !$account->hasPermission('clone ' . $entity->bundle() . ' ' . $entity->getEntityTypeId() . ' entities'));
    if ($access->isForbidden()) {
        return $access->addCacheableDependency($cache);
    }
    // Deny access if the user can clone but cannot create new entities of this
    // type. However, we have some exceptions in which the access control handler
    // doesn't have a say in things. In these cases, we go based on the clone
    // permission only.
    $exceptions = [
        'file',
        'paragraph',
    ];
    if (in_array($entity->getEntityTypeId(), $exceptions)) {
        return AccessResult::allowed()->addCacheableDependency($cache);
    }
    $handler = \Drupal::entityTypeManager()->getAccessControlHandler($entity->getEntityTypeId());
    $access = $handler->createAccess($entity->bundle(), $account, [], TRUE);
    if (!$access->isAllowed()) {
        $cache->addCacheableDependency($access);
        $forbidden = AccessResult::forbidden();
        return $forbidden->addCacheableDependency($cache);
    }
    return AccessResult::allowed()->addCacheableDependency($cache);
}