Custom permissions for unpublished content I'm running in to a problem with my drupal 8 site. I want to give access to unpublished content. I found a solution using a hook.

I want to create a custom module these don't seem to work.

I found this for the unpublished problem:
&quot;View unpublished content&quot; permission for non-author

It looks like a solid solutions for my problems but I can't get them to work. I can enable the modules created, and it doesn't give any errors.

My view_unpub.info.yml file:

name: View Unpublished
type: module
description: 'View Unpublished Content'
package: SVDS
core: 8.x


My view_unpub.module file:

&lt;?php
function view_unpub_permission()
{
    return array(
        'unconditionally view unpublished content' =&gt; array(
            'title' =&gt; t('Unconditionally View Unpublished Content'),
            'restrict access' =&gt; TRUE
        )
    );
}

function view_unpub_node_access($node, $op, $account)
{
// We're only bothered about the 'view' operation at the moment
    if ($op == 'view') {
// If the user has access to our new permission, let 'em at it
        if (user_access('unconditionally view unpublished content', $account)) {
            return NODE_ACCESS_ALLOW;
        }
    }

// For everything else let the system decide.
    return NODE_ACCESS_IGNORE;
}
The code you are showing is not completely ported to Drupal 8. In particular:


hook_permission() is not used anymore from Drupal 8; what you need is describing the module permissions in view_unpub.permissions.yml
user_access() has been removed
Constants like NODE_ACCESS_ALLOW have been removed


As example of hook_node_access() implementation, see node_node_access(), which also shows what replaces user_access() in Drupal 8.

function node_node_access(NodeInterface $node, $op, $account) {
  $type = $node-&gt;bundle();
  switch ($op) {
    case 'create':
      return AccessResult::allowedIfHasPermission($account, 'create ' . $type . ' content');
    case 'update':
      if ($account-&gt;hasPermission('edit any ' . $type . ' content', $account)) {
        return AccessResult::allowed()-&gt;cachePerPermissions();
      }
      else {
        return AccessResult::allowedIf($account-&gt;hasPermission('edit own ' . $type . ' content', $account) &amp;&amp; $account-&gt;id() == $node-&gt;getOwnerId())-&gt;cachePerPermissions()-&gt;cachePerUser()-&gt;addCacheableDependency($node);
      }
    case 'delete':
      if ($account-&gt;hasPermission('delete any ' . $type . ' content', $account)) {
        return AccessResult::allowed()-&gt;cachePerPermissions();
      }
      else {
        return AccessResult::allowedIf($account-&gt;hasPermission('delete own ' . $type . ' content', $account) &amp;&amp; $account-&gt;id() == $node-&gt;getOwnerId())-&gt;cachePerPermissions()-&gt;cachePerUser()-&gt;addCacheableDependency($node);
      }
    default:

      // No opinion.
      return AccessResult::neutral();
  }


The rest of the code seems fine, to me. Check all the files you are using, and see if they need any change to make your modules work in Drupal 8.