I have a form in a page, so what I want to do is to check wether the user is a guest or a logged user and depending of that add an additional field to a guest, but hide it for the logged user, should this be done in the code of the form or there are some built-in settings for that behaviour?

  • 1
    What version of Drupal?
    – Alex Gill
    Feb 15, 2018 at 9:17

1 Answer 1


Can you please elaborate the question. As per the understanding you have to use this condition to check the user login status. Since you have to use it in a form, you can implements with hook_form_FORM_ID_alter().

Use this code to check.

if (user_is_logged_in()) {
    echo "Logged In";
else {
    echo "Not logged in";


Example for user_profile_form alter. FORM_ID = user_profile_form

 * Implements hook_form_FORM_ID_alter().
function mymodule_form_user_profile_form_alter(&$form, &$form_state, $form_id) {

To find form id add this in your custom module.

 * Implements hook_form_alter().
function mymodule_form_alter(&$form, &$form_state, $form_id) {

When you visit the page the form is on, the form_id will be displayed.

print_r($form); , you can see all details of a form, like form ID, field names of a form etc.

Also you can inspect the form and find the form id

<form enctype="multipart/form-data" action="/user/288/edit" method="post" id="user-profile-form" accept-charset="UTF-8">

Here id="user-profile-form", in order to use it in code replace hiphen (-) with undescore(_)

 user-profile-form => user_profile_form
  • where can I find that FORM_ID in hook_form_FORM_ID_alter()? Feb 15, 2018 at 9:46

Your Answer

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

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