Wednesday, June 27, 2007

Drupal 4.7 Allow Certain Role Users to View Unpublished nodes for Custom Module

If you're like me you have come across a need for non-admin role users, like a content editor, to be able to view unpublished nodes of which they are not the author. By default, Drupal does not allow non-admin users who are not the node author to view unpublished nodes.

Of course, Drupal being a great platform to extend, you can resolve this in your custom module.

In the
hook_access
function of your custom module, just try code similar to this:


if ($op == 'view' &&
(user_access('edit periodic updates') ||
user_access('edit own periodic updates')) &&
$node->status != 1) {
return TRUE;
}


What you are telling Drupal is if the node operation is 'view' and the user has one of the specified two role permissions and the node status is not published, then allow the view access.

Easy enough.

No comments: