I want the administrator users can publish nodes of the story content type; the other roles can unpublish. The following is the code I am using, but it doesn't work. I unchecked the box "published", which is in the "story" content type.
function mymodulename_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
global $user;
if ($op== 'presave' && $node->type == 'story') {
if (in_array('administrator',array_values($user->roles))) {
$node->status = 1;
}
}
}
I am using echo 'test'
above on the if ($op....)
, the page outputs "test." When I put the echo 'test'
into if
body, the page doesn't output "test." but When I delete $op == 'presave'
, it can output "test."
dpm()
from the devel module anddpm($user)
. You can also add adpm($op)
just after theglobal $user
to see which operations are executed.$user->roles
toarray_values()
is not necessary;$user->roles
can be passed directly toin_array()
.user_access('permission to check')
.