Thursday, April 21, 2011

View unpublished content by an moderator or content editor(custom role created)

1.Create an custom module view_unpublished
2.Place this code in it

<?php

/**
* Implementation of hook_perm().
*/
function view_unpublished_perm() {
$perms = array('view all unpublished content');
foreach (node_get_types() as $type => $name) {
$perms[] = 'view unpublished ' . $type . ' content';
}
return $perms;
}

/**
* Implementation of hook_menu_alter().
*
* Modifies the path node/nid to use our access callback.
*/
function view_unpublished_menu_alter(&$items) {

$old_access_callback = $items['node/%node']['access callback'];
$items['node/%node']['access callback'] = '_view_unpublished_node_access';
$items['node/%node']['access arguments'] = array(1, $old_access_callback);
}

/**
* Returns true if the user has 'view all unpublished content' or if
* they have the permission corresponding to the node's content type.
*/
function _view_unpublished_node_access($node, $old_access_callback = "node_access") {
// Only check permissions on nodes that are unpublished.
if ($node->status == 0) {
if (user_access('view all unpublished content')) {
return TRUE;
}

if (user_access('view unpublished ' . $node->type . ' content')) {
return TRUE;
}
}
// If none of the above conditions were satisfied, then use the normal callback.
return $old_access_callback('view', $node);
}


?>

No comments:

Post a Comment