forked from a64f7bb4-7358-4778-9fbe-3b882c34cc1d/v1
32 lines
1.2 KiB
Twig
32 lines
1.2 KiB
Twig
/**
|
|
* Implements hook_node_access().
|
|
*/
|
|
function {{ machine_name }}_node_access(\Drupal\node\NodeInterface $node, $op, \Drupal\Core\Session\AccountInterface $account) {
|
|
$type = $node->bundle();
|
|
|
|
switch ($op) {
|
|
case 'create':
|
|
return AccessResult::allowedIfHasPermission($account, 'create ' . $type . ' content');
|
|
|
|
case 'update':
|
|
if ($account->hasPermission('edit any ' . $type . ' content')) {
|
|
return AccessResult::allowed()->cachePerPermissions();
|
|
}
|
|
else {
|
|
return AccessResult::allowedIf($account->hasPermission('edit own ' . $type . ' content') && ($account->id() == $node->getOwnerId()))->cachePerPermissions()->cachePerUser()->addCacheableDependency($node);
|
|
}
|
|
|
|
case 'delete':
|
|
if ($account->hasPermission('delete any ' . $type . ' content')) {
|
|
return AccessResult::allowed()->cachePerPermissions();
|
|
}
|
|
else {
|
|
return AccessResult::allowedIf($account->hasPermission('delete own ' . $type . ' content') && ($account->id() == $node->getOwnerId()))->cachePerPermissions()->cachePerUser()->addCacheableDependency($node);
|
|
}
|
|
|
|
default:
|
|
// No opinion.
|
|
return AccessResult::neutral();
|
|
}
|
|
}
|