Mercurial > hg > cmmr2012-drupal-site
diff vendor/chi-teck/drupal-code-generator/templates/d8/hook/node_access.twig @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/vendor/chi-teck/drupal-code-generator/templates/d8/hook/node_access.twig Thu Jul 05 14:24:15 2018 +0000 @@ -0,0 +1,31 @@ +/** + * 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', $account)) { + return AccessResult::allowed()->cachePerPermissions(); + } + else { + return AccessResult::allowedIf($account->hasPermission('edit own ' . $type . ' content', $account) && ($account->id() == $node->getOwnerId()))->cachePerPermissions()->cachePerUser()->addCacheableDependency($node); + } + + case 'delete': + if ($account->hasPermission('delete any ' . $type . ' content', $account)) { + return AccessResult::allowed()->cachePerPermissions(); + } + else { + return AccessResult::allowedIf($account->hasPermission('delete own ' . $type . ' content', $account) && ($account->id() == $node->getOwnerId()))->cachePerPermissions()->cachePerUser()->addCacheableDependency($node); + } + + default: + // No opinion. + return AccessResult::neutral(); + } +}