You are here

function view_unpublished_node_grants in view_unpublished 8

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

Implements hook_node_grants().

File

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

Code

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