565c100e78c907e5d7048bb307f02aba246a6afb
[yaffs-website] / modules / node_access_test_language / node_access_test_language.module
1 <?php
2
3 /**
4  * @file
5  * Test module with a language-aware node access implementation.
6  *
7  * The module adds a 'private' field to page nodes that allows each translation
8  * of the node to be marked as private (viewable only by administrators).
9  */
10
11 use Drupal\node\NodeInterface;
12
13 /**
14  * Implements hook_node_grants().
15  *
16  * This module defines a single grant realm. All users belong to this group.
17  */
18 function node_access_test_language_node_grants($account, $op) {
19   $grants['node_access_language_test'] = [7888];
20   return $grants;
21 }
22
23 /**
24  * Implements hook_node_access_records().
25  */
26 function node_access_test_language_node_access_records(NodeInterface $node) {
27   $grants = [];
28
29   // Create grants for each translation of the node.
30   foreach ($node->getTranslationLanguages() as $langcode => $language) {
31     // If the translation is not marked as private, grant access.
32     $translation = $node->getTranslation($langcode);
33     $grants[] = [
34       'realm' => 'node_access_language_test',
35       'gid' => 7888,
36       'grant_view' => empty($translation->field_private->value) ? 1 : 0,
37       'grant_update' => 0,
38       'grant_delete' => 0,
39       'priority' => 0,
40       'langcode' => $langcode,
41     ];
42   }
43   return $grants;
44 }