You are here

function view_unpublished_node_access_records in view_unpublished 8

Same name and namespace in other branches
  1. 7 view_unpublished.module \view_unpublished_node_access_records()

Implements hook_node_access_records().

File

./view_unpublished.module, line 16
Main functions and hook implementations of the View Unpublished module.

Code

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;
}