18

I googled a bit to see if there's a permission-level way to allow a logged-in (but non-admin) user see unpublished content (marked with the CSS node-unpublished class). This appears to be scheduled for Drupal 8 only. But maybe someone was clever enough to do a workaround?

I have a role named Content Editor which should be able to see all unpublished content, regardless of whether they were the author or not.

8 Answers 8

18

If you're ok writing a very small custom module you could use a combination of hook_permission() and hook_node_access() to do this:

function MYMODULE_permission() {
  return array(
    'unconditionally view unpublished content' => array(
      'title' => t('Unconditionally View Unpublished Content'),
      'restrict access' => TRUE
    )
  );
}

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

Any role you assign the new permission to should now be able to view any node, regardless of whether it's published or not.

4
  • To strengthen the security a bit though I added && user_access('administer nodes') so that it depends on the rights to create content. Now it's not that "unconditional". Thanks for the example! Commented May 10, 2012 at 12:01
  • Yeah I should have mentioned you might want to 'beef' it up a bit :) glad to help
    – Clive
    Commented May 10, 2012 at 12:02
  • great stuff again, Clive. This solves one of my issues as well to restrict access to nodes with a certain taxonomy term!!!
    – uwe
    Commented May 10, 2012 at 18:39
  • 1
    The code really is "unconditionally view any content". It never checks to see if the node is published or not. Or if the role has permission to see the content in the first place.
    – JM01
    Commented Aug 30, 2012 at 20:58
6

I first tried the code above, which is great. But after giving rights onto unpublished content to a role, that role still couldn't see unpublished content in views, and this can be very useful for some editors roles...

I solved it using the module view_unpublished.

2
  • I installed this plugin & instead of solving my issue, it didn't & made all content not displayed for the custom Admin group that i made. Commented Dec 17, 2014 at 14:47
  • Well I'm sorry you had such behaviour. I've been using it for a while now and never had such pb with any role (if you meant your Admin group is a role). You may post in issue in DO then.
    – Kojo
    Commented Dec 17, 2014 at 18:43
3

I use the Revisioning module for this. Upon installation, it creates the following permissions for each content type:

  • Content type: View revisions of own content

  • Content type: View revisions of anyone's content

  • Content type: Publish revisions of own content

  • Content type: Publish revisions of anyone's content

By giving my editors permission to view revisions of anyone's content, they are able to access and publish all nodes that have been created or revised by other users.

0
1

This is what I ended up making....

It creates a 'view any content permission' for each node type. It's pretty much the same as what the node module does, but adds the extra option.

/**
 * Implements hook_permission().
 */
function MODULE_permission() {
  $perms = array();
  foreach (node_permissions_get_configured_types() as $type) {
    $info = node_type_get_type($type);
    $perms += array(
      "view $type content" => array(
        'title' => t('%type_name: View any content', array('%type_name' => $info->name)),
      )
    );
  }
  return $perms;
}

/**
 * Implements hook_node_access().
 */
function MODULE_node_access($node, $op, $account) {
  $type = is_string($node) ? $node : $node->type;

  if (in_array($type, node_permissions_get_configured_types())) {
    if ($op == 'view' && user_access('view ' . $type . ' content', $account)) {
      return NODE_ACCESS_ALLOW;
    }
  }

  return NODE_ACCESS_IGNORE;
}
1

There is a module for Drupal 7 and Drupal 8 for that:

View_unpublished

This module allows you to grant access for specific user roles to view unpublished nodes of a specific type. Access control is quite granular in this regard.

EDIT: Since Drupal 8.6 I guess, the permission "view any unpublished content" is part of the content moderation module. Which is included in Drupal core.

0

Refering to the official Administration guide: https://drupal.org/node/1106606 the module view_unpublished suggested by Kojo is a quick and easy solution.

0

A simple, reasonable work-around is to use the 'promote to frontpage' flag on nodes.

If you have no other use for this flag, filter your views on 'promoted to frontpage'. Your content editors can work on the node before it is promoted (by using a direct link). But it will not yet appear to site visitors, potentially.

We got that inspiration from Youtube's 'unlisted video' approach.

0

If you only need to see the content in a view, disable SQL rewriting in Query Settings under Other in the Views UI.

This works for me as I only want users to be able to unflag unpublished content.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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