Implements hook_ENTITY_TYPE_access() for entity type "paragraph".
function paragraphs_type_permissions_paragraph_access(ParagraphInterface $entity, $operation, AccountInterface $account) {
$permissions =& drupal_static(__FUNCTION__, array());
if (!in_array($operation, array(
'view',
'update',
'delete',
), TRUE)) {
// If there was no type to check against, or the $op was not one of the
// supported ones, we return access denied.
return AccessResult::neutral();
}
// Set static cache id to use the type machine name.
$type = $entity
->getType();
if ($operation == 'view' && !$entity->status->value) {
return AccessResult::forbidden();
}
// If we've already checked access for this type, user and op, return from
// cache.
if (isset($permissions[$account
->id()][$type][$operation])) {
return $permissions[$account
->id()][$type][$operation];
}
// If the current user has access to this type/operation, return access
// allowed, forbidden otherwise.
if ($account
->hasPermission('bypass paragraphs type content access') || $account
->hasPermission($operation . ' paragraph content ' . $type)) {
$permissions[$account
->id()][$type][$operation] = AccessResult::allowed()
->cachePerPermissions();
}
else {
$permissions[$account
->id()][$type][$operation] = AccessResult::forbidden()
->cachePerPermissions();
}
return $permissions[$account
->id()][$type][$operation];
}