1

I have a field called 'email' I added to my node comments form. However I don't want this to display to users who are logged in. What's the best way to hide this field for logged in users?

I created a module 'hideemail.module' (and info file) with the following code, and flushed caches multiple times...but it's still not working. Note: I double-checked the form name with Firebug and the field name in the resource content type > comments fields.

From hideemail.module (and yes its enabled):

 <?php
 /**
 * Implements hook_form_alter().
 */
function hideemail_form_alter(&$form, &$form_state, $form_id) {
  global $user;
  if ($form_id == 'comment-form' && $user->uid != 0) {
    $form['field_email']['#access'] = FALSE;
  }
}
?>
2

Use hook_form_alter():

/**
 * Implements hook_form_alter().
 */
function my_module_form_alter(&$form, &$form_state, $form_id) {
  global $user;
  if ($form_id == 'comment_form' && $user->uid != 0) {
    $form['field_email']['#access'] = FALSE;
  }
}
| improve this answer | |
  • thanks. I tried that, see my edits above...but it's still not working. Am I doing this right by creating a module? – Sage Mar 10 '15 at 18:05
  • Yes, create a custom module. Use dpm($form_id); and dpm($form); to inspect your form's structure to make sure you are using the right id and machine name of your field. That function is provided by the devel module. – tyler.frankenstein Mar 10 '15 at 20:55
  • I tried that...everything was correct, so I tried just putting your code into my template.php file instead of a module and it worked. – Sage Mar 12 '15 at 2:10
  • From your sample code above, it looks like your form id is incorrect. Perhaps replace the hyphen with an underscore. – tyler.frankenstein Mar 13 '15 at 3:57

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.