0

I'm building a website which has a member section. People subscribe and complete the "user" form (username, email, password and some other fields).

For an additional fee, they can be part of the "dating" part of the website, where they can meet new people.

After paying, I would like them to complete a form about them (height, eyes, etc.). Those fields are already in the user form.

I'm displaying the user form this way :

global $user;
module_load_include('inc', 'user', 'user.pages');
return drupal_render(drupal_get_form('user_profile_form', $user));

How would i go about hiding some fields that I don't want them to see/modify?

Thanks!

1

You should alter the form before rendering it.

$form = drupal_get_form('user_profile_form', $user);

// Uncomment this line to inspect the structure of the form array (needs devel module)
//dpm($form '$form');

// hide field.
unset($form['field_name']);

return drupal_render($form);
-1

I would implement hook_form_alter() and perform modifications there. The advantage of doing it in the form alter hook is that these changes will be applied whenever that form is requested (such as on other pages where you may also want to call it), and not just a single spot.

Additionally, don't use unset() to remove form elements, as other modules may depend on them being present, which will cause errors. Instead, set the element's access to FALSE:

$form['element_name']['#access'] = FALSE;
  • Unfortunatly, it's the "user_profile_form", quite an important one. How would i go about hiding fields but not on every pages where it is displayed, just on this particular module? – Ebpo Mar 6 '13 at 13: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.