-
Notifications
You must be signed in to change notification settings - Fork 2
/
node_unpublished.module
91 lines (80 loc) · 2.48 KB
/
node_unpublished.module
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
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/**
* @file
* Contains node_unpublished.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeInterface;
/**
* Implements hook_help().
*/
function node_unpublished_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the node_unpublished module.
case 'help.page.node_unpublished':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Provides node alterations to allow access to view all unpublished and alter status.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_node_access_records().
*/
function node_unpublished_node_access_records(NodeInterface $node) {
// We only care about the node if is unpublished. If not, it is
// treated just like any other node and we completely ignore it.
if ($node->isPublished() === FALSE) {
$grants = [];
// Unpublished nodes should be viewable to all editors.
$grants[] = array(
'realm' => 'node_unpublished_view_unpublished',
'gid' => 1,
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
'priority' => 0,
);
$grants[] = array(
'realm' => 'view_unpublished_author',
'gid' => $node->getOwnerId(),
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
'priority' => 0,
);
return $grants;
}
}
/**
* Implements hook_node_grants().
*/
function node_unpublished_node_grants(AccountInterface $account, $op) {
$grants = [];
if ($op == 'view') {
if ($account->hasPermission('view own unpublished content')) {
$grants['view_unpublished_author'][] = $account->id();
}
if ($account->hasPermission('override node publish options')) {
$grants['node_unpublished_view_unpublished'][] = 1;
return $grants;
}
}
return $grants;
}
/**
* Implements hook_entity_field_access_alter().
*/
function node_unpublished_entity_field_access_alter(array &$grants, array $context) {
$entity_type = $context['field_definition']->getTargetEntityTypeId();
if ($entity_type == 'node' && $context['operation'] == 'edit' && !$context['account']->hasPermission('administer nodes')) {
switch ($context['field_definition']->getName()) {
case 'status':
$grants[':default'] = AccessResult::allowedIfHasPermission($context['account'], 'override node publish options');
break;
}
}
}