view_unpublished.module in view_unpublished 8
Same filename and directory in other branches
Main functions and hook implementations of the View Unpublished module.
File
view_unpublished.moduleView source
<?php
/**
* @file
* Main functions and hook implementations of the View Unpublished module.
*/
use Drupal\Core\Session\AccountInterface;
use Drupal\node\Entity\NodeType;
use Drupal\node\NodeInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_node_access_records().
*/
function view_unpublished_node_access_records(NodeInterface $node) {
$grants = [];
$access_content_grants = [];
foreach ($node
->getTranslationLanguages(TRUE) as $langcode => $language) {
$translated_node = $node
->getTranslation($langcode);
if ($translated_node
->isPublished() === TRUE) {
$access_content_grants[] = [
'realm' => 'view_unpublished_published_content',
'gid' => 1,
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
'langcode' => $langcode,
];
}
else {
// Unpublished nodes should be viewable to all editors.
$grants[] = [
'realm' => 'view_unpublished_content',
'gid' => 1,
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
'priority' => 0,
'langcode' => $langcode,
];
$grants[] = [
'realm' => "view_unpublished_{$node->getType()}_content",
'gid' => 1,
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
'priority' => 0,
'langcode' => $langcode,
];
$grants[] = [
'realm' => 'view_unpublished_author',
'gid' => $node
->getOwnerId(),
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
'priority' => 0,
'langcode' => $langcode,
];
}
}
// Only use the $access_content_grants if we have to.
if (count($grants) > 0) {
$grants = array_merge($grants, $access_content_grants);
}
return $grants;
}
/**
* Implements hook_node_grants().
*/
function view_unpublished_node_grants(AccountInterface $account, $op) {
$grants_cache =& drupal_static(__FUNCTION__, []);
if (isset($grants_cache[$account
->id()][$op])) {
return $grants_cache[$account
->id()][$op];
}
$grants = [];
if ($op == 'view') {
if ($account
->hasPermission('view own unpublished content')) {
$grants['view_unpublished_author'] = [
$account
->id(),
];
}
if ($account
->hasPermission('access content')) {
$grants['view_unpublished_published_content'] = [
1,
];
}
if ($account
->hasPermission('view any unpublished content')) {
$grants['view_unpublished_content'] = [
1,
];
return $grants;
}
foreach (NodeType::loadMultiple() as $type) {
$type_id = $type
->id();
if ($account
->hasPermission("view any unpublished {$type_id} content")) {
$grants["view_unpublished_{$type_id}_content"] = [
1,
];
}
}
}
$grants_cache[$account
->id()][$op] = $grants;
return $grants_cache[$account
->id()][$op];
}
/**
* @file
* Contains view_unpublished.module.
*/
/**
* Implements hook_help().
*/
function view_unpublished_help($route_name, RouteMatchInterface $arg) {
switch ($route_name) {
case 'help.page.view_unpublished':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('This module allows you to grant access for specific user roles to view unpublished nodes of a specific type. Access control is quite granular in this regard.') . '</p>';
// Add a link to the Drupal.org project.
$output .= '<p>';
$output .= t('Visit the <a href=":project_link">View Unpublished project pages</a> on Drupal.org for more information.', [
':project_link' => 'https://www.drupal.org/project/view_unpublished',
]);
$output .= '</p>';
return $output;
}
}
Functions
Name | Description |
---|---|
view_unpublished_help | Implements hook_help(). |
view_unpublished_node_access_records | Implements hook_node_access_records(). |
view_unpublished_node_grants | Implements hook_node_grants(). |