0

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

8
  • Use dpm() from the devel module and dpm($user). You can also add a dpm($op) just after the global $user to see which operations are executed.
    – user334
    May 10, 2011 at 4:07
  • Passing $user->roles to array_values() is not necessary; $user->roles can be passed directly to in_array().
    – apaderno
    May 10, 2011 at 4:26
  • Drupol, the global $user can be execusted, because 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."
    – enjoylife
    May 10, 2011 at 5:08
  • 3
    Generally it's not a good practice to directly refer to a role from code that manage user rights. Instead you should check for a specific permission and grant this permission to user with this role. May 10, 2011 at 11:00
  • 1
    It's enough you use user_access('permission to check').
    – apaderno
    May 11, 2011 at 2:04

1 Answer 1

1

Regarding your problem, you're editing the node right ? Because presave acts during saving.

On a site I use this module which is quite handy, maybe it can help you: Publish Content

Your Answer

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

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