Newer
Older
Dries Buytaert
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php
/**
* @file
* Test module with a language-aware node access implementation.
*
* The module adds a 'private' field to page nodes that allows each translation
* of the node to be marked as private (viewable only by administrators).
*/
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_node_grants().
*
* This module defines a single grant realm. All users belong to this group.
*/
function node_access_test_language_node_grants($account, $op) {
$grants['node_access_language_test'] = array(7888);
return $grants;
}
/**
* Implements hook_node_access_records().
*/
function node_access_test_language_node_access_records(EntityInterface $node) {
$grants = array();
// Create grants for each translation of the node.
foreach ($node->getTranslationLanguages() as $langcode => $language) {
// If the translation is not marked as private, grant access.
$grants[] = array(
'realm' => 'node_access_language_test',
'gid' => 7888,
'grant_view' => empty($node->field_private[$langcode][0]['value']) ? 1 : 0,
'grant_update' => 0,
'grant_delete' => 0,
'priority' => 0,
'langcode' => $langcode,
);
}
return $grants;
}
/**
* Implements hook_enable().
*
* Creates the 'private' field, which allows the node to be marked as private
* (restricted access) in a given translation.
*/
function node_access_test_language_enable() {
$field_private = array(
'field_name' => 'field_private',
'type' => 'list_boolean',
'cardinality' => 1,
'translatable' => TRUE,
'settings' => array(
'allowed_values' => array(0 => 'Not private', 1 => 'Private'),
),
);
$field_private = field_create_field($field_private);
$instance = array(
'field_name' => $field_private['field_name'],
'entity_type' => 'node',
'bundle' => 'page',
'widget' => array(
'type' => 'options_buttons',
),
);
$instance = field_create_instance($instance);
}
/**
* Implements hook_disable().
*/
function node_access_test_language_disable() {
field_delete_instance(field_read_instance('node', 'field_private', 'page'));
}