1

I need to check if the node uid and the logged-in user's uid are identical when the user is in the node edit form, and then add a class to the form if that condition is true.* The relevant code in my custom module currently looks like this:

function custom_form_alter(&$form, $form_state, $form_id) { 
    global $user;
    $node = node_load(arg(1));
    switch($form_id) {            
        case "custom_type_node_form" && "$user->uid == $node->uid":
            $form['#attributes'] = array('class' => 'test-class'); 
    break;
    }
}

The class is getting added even if the user isn't the author.

*Use case is that all site members with role X can edit nodes of type Y, but if the member editing the node is the author, I'd like to add some CSS.

I'm using Drupal 6.31.

1

hey You can simply do this :-

function custom_form_alter(&$form, $form_state, $form_id) 
 { 
    global $user;
    $node = node_load(arg(1));
    if($form_id=='custom_type_node_form')
     {
       if($user->uid == $node->uid)
        {
          $form['#attributes'] = array('class' => 'test-class');
        }
      }
}

I hope this will work for you.

  • As this stands it attempts to do a node_load(arg(1)) regardless of the form or the path - so who knows what arg(1) might be returning! I'd suggest using hook_form_FORM_ID_alter() (and you can read the node directly from the form array in $form['#node']). – Andy Jul 25 '14 at 8:29
  • I had to change the if($user->uid == $node->uid) to if($user->uid === $node->uid) for reasons I am choosing not to investigate at the moment, but otherwise this was great, thanks! – Elizabeth Jul 25 '14 at 15:10
  • These conditions you need to add yourself. I just told you the way to do it. – TBI Infotech Jul 28 '14 at 6:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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