When using content moderation, the view_unpblished module does not work. But I really needed to get a user role to see all nodes created of this type and have access to it. With the following snippet, I fixed this. Users of this role can now see and edit nodes of content type 'tools'. In this way, with 10 lines of code we replaced a full module. Not bad!
use Drupal\node\NodeInterface;
use Drupal\Core\Access\AccessResult;
/**
* Implements hook_node_access().
* Drupal 8 does not provide a permission of
* "View all unpublished content of content type X"
* With this hook, we allow the tools_admin user to view and edit
* all unpublished content of content type tools
*/
function MYMODULE_node_access(NodeInterface $node, $op, $account) {
$type = $node->bundle();
$roles = $account->getRoles();
if ($type == 'tools') {
if(in_array('tools_admin', $roles)) {
return AccessResult::allowed();
}
}
}