X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=vendor%2Fchi-teck%2Fdrupal-code-generator%2Ftemplates%2Fd8%2Fhook%2Fquery_TAG_alter.twig;fp=vendor%2Fchi-teck%2Fdrupal-code-generator%2Ftemplates%2Fd8%2Fhook%2Fquery_TAG_alter.twig;h=15cc658d27c1dbf166b17ee491019312c1fe8a9c;hp=0000000000000000000000000000000000000000;hb=af6d1fb995500ae68849458ee10d66abbdcfb252;hpb=680c79a86e3ed402f263faeac92e89fb6d9edcc0 diff --git a/vendor/chi-teck/drupal-code-generator/templates/d8/hook/query_TAG_alter.twig b/vendor/chi-teck/drupal-code-generator/templates/d8/hook/query_TAG_alter.twig new file mode 100644 index 000000000..15cc658d2 --- /dev/null +++ b/vendor/chi-teck/drupal-code-generator/templates/d8/hook/query_TAG_alter.twig @@ -0,0 +1,35 @@ +/** + * Implements hook_query_TAG_alter(). + */ +function {{ machine_name }}_query_TAG_alter(Drupal\Core\Database\Query\AlterableInterface $query) { + // Skip the extra expensive alterations if site has no node access control modules. + if (!node_access_view_all_nodes()) { + // Prevent duplicates records. + $query->distinct(); + // The recognized operations are 'view', 'update', 'delete'. + if (!$op = $query->getMetaData('op')) { + $op = 'view'; + } + // Skip the extra joins and conditions for node admins. + if (!\Drupal::currentUser()->hasPermission('bypass node access')) { + // The node_access table has the access grants for any given node. + $access_alias = $query->join('node_access', 'na', '%alias.nid = n.nid'); + $or = new Condition('OR'); + // If any grant exists for the specified user, then user has access to the node for the specified operation. + foreach (node_access_grants($op, $query->getMetaData('account')) as $realm => $gids) { + foreach ($gids as $gid) { + $or->condition((new Condition('AND')) + ->condition($access_alias . '.gid', $gid) + ->condition($access_alias . '.realm', $realm) + ); + } + } + + if (count($or->conditions())) { + $query->condition($or); + } + + $query->condition($access_alias . 'grant_' . $op, 1, '>='); + } + } +}