how do I show/hide field of the form based on currently logged in user's field? my users' profiles have start_date_field.

my form has 5 questions:

day_1_activity -> drop down
day_2_activity -> drop down
day_3_activity -> drop down
day_4_activity -> drop down
day_5_activity -> drop down

Now I would like to do a simple comparison:

start_date_field of logged in user with current_date.

if start_date_field = current_date show day_1_activity only.

if start_date_field + 1 = current_date show day_1_activity and day_2_activity only.


  • are you referring to a specific form in Drupal or a custom form that you've created?
    – Aboodred1
    Commented Apr 10, 2013 at 15:20
  • custom that I have created using entityforms
    – sylwester
    Commented Apr 10, 2013 at 15:22

1 Answer 1


First you need to hide all questions drop down fields by setting #access to FALSE then show each field according to the condition.

You should use hook_form_alter to alter your entityform form.

function MODULE_NAME_form_MACHINE_NAME_entityform_edit_form_alter(&$form, &$form_state) {
  global $user; // to get the current user uid

  // hide all drop down
  $form['field_day_1_activity']['#access'] = FALSE;
  $form['field_day_2_activity']['#access'] = FALSE;
  $form['field_day_3_activity']['#access'] = FALSE;
  $form['field_day_4_activity']['#access'] = FALSE;
  $form['field_day_5_activity']['#access'] = FALSE;

  // load user
  $account = user_load($user->uid);

  // get user start_date_field value
  $user_start_date = $account->field_start_date_field['und'][0]['value'];

  // show question drop down based on the condition.
  switch ($user_start_date) {
    case date('Y-m-d'):
      $form['field_day_1_activity']['#access'] = TRUE;
    case date('Y-m-d', strtotime('+1 day')):
      $form['field_day_1_activity']['#access'] = TRUE;
      $form['field_day_2_activity']['#access'] = TRUE;
    case date('Y-m-d', strtotime('+2 day')):
      $form['field_day_1_activity']['#access'] = TRUE;
      $form['field_day_2_activity']['#access'] = TRUE;
      $form['field_day_3_activity']['#access'] = TRUE;
  • looks like exactly what I needed. If I were to make it more flexible it the way that I would be able to choose, in the code of this module, which forms should be altered, how would I go about it?
    – sylwester
    Commented Apr 11, 2013 at 9:20
  • 1
    worked great! just needed to change $user_start_date = $account->field_start_date_field['und'][0]['value']; to $user_start_date = date('Y-m-d', strtotime($account->field_start_date['und'][0]['value'])); as it was giving me time as well time as well 00:00:00
    – sylwester
    Commented Apr 11, 2013 at 9:49
  • and strtotime('+1 day') to strtotime('-1 day') etc.. Thanks a lot for this! I you wouldn't mind helping me with my first comment or directing to literature that will help it would be very appreciated
    – sylwester
    Commented Apr 11, 2013 at 9:57
  • 1
    yes. I've altered the code above to suit my requirements and now all forms with different fields, days etc work great and its very flexible as well. It would be nice to add radio button in entity form settings (add and edit) to say (run this code on this form) yes/no. Thanks a lot again for your help - really helped me understand basic of hooks
    – sylwester
    Commented Apr 11, 2013 at 16:21
  • 1
    My friend, your request is so detailed; so I recommend you to submit your question, in a question post not in a comment.
    – Aboodred1
    Commented Apr 12, 2013 at 15:15

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.