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: "View unpublished content" 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:

function view_unpub_permission()
    return array(
        'unconditionally view unpublished content' => array(
            'title' => t('Unconditionally View Unpublished Content'),
            'restrict access' => 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.
  • 1
    hook_permission no longer exists in Drupal 8. You need to add a mymodule.permissions.yml file, which is the new way. See: aram.cz/article/custom-permissions-drupal-8
    – Kevin
    Jul 6, 2017 at 20:49
  • thanks for your reply. And for the second problem I'm moving to a new post. I fixed the problem I changed FileInterface to \Drupal\file\FileInterface to get it to work but the is a other problem the came to my notice. My new post Image styles deleted on node save
    – Joost
    Jul 7, 2017 at 7:03

1 Answer 1


The code you are showing is not completely ported to Drupal 8. In particular:

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->bundle();
  switch ($op) {
    case 'create':
      return AccessResult::allowedIfHasPermission($account, 'create ' . $type . ' content');
    case 'update':
      if ($account->hasPermission('edit any ' . $type . ' content', $account)) {
        return AccessResult::allowed()->cachePerPermissions();
      else {
        return AccessResult::allowedIf($account->hasPermission('edit own ' . $type . ' content', $account) && $account->id() == $node->getOwnerId())->cachePerPermissions()->cachePerUser()->addCacheableDependency($node);
    case 'delete':
      if ($account->hasPermission('delete any ' . $type . ' content', $account)) {
        return AccessResult::allowed()->cachePerPermissions();
      else {
        return AccessResult::allowedIf($account->hasPermission('delete own ' . $type . ' content', $account) && $account->id() == $node->getOwnerId())->cachePerPermissions()->cachePerUser()->addCacheableDependency($node);

      // 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.

  • Hi there I thought I had i covered but I don't. I created a .permissions.yml file this works great and tried this, but that doesn't work: function view_unpub_node_access(\Drupal\node\NodeInterface $node, $op, $account) { switch ($op) { case 'view': return AccessResult::allowedIfHasPermission($account, 'view all unpublished'); default: // No opinion. return AccessResult::neutral(); } }
    – Joost
    Jul 7, 2017 at 7:55
  • Did you add use Drupal\Core\Access\AccessResult; as well as all the necessary use lines at the beginning of the file?
    – apaderno
    Jul 7, 2017 at 9:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.