I want to print only bits of a very big form, my first approach was as follow:

function THEME_preprocess_user_profile(&$variables) {
    global $user;
    $account = user_load($user ->uid);  
    $variables['user_edit']  = drupal_get_form('user_profile_form', $account);
    $variables['user_edit']['field_about_me'] = 'invisible';
    $variables['user_edit']['field_some_filed'] = 'invisible';


This does work to hide certain element, the problem is that there is way more elements i want to hide than elements i want to show, so this process can be tedious..

Would anyone think of a better way to do so ?

UPDATE : Ideally i'd like to have a function similar to drupal_get_form, but that instead only return a targeted element.

Thanks a lot

3 Answers 3


Either save the elements you wish to print in temporary var, hide all (or unset them), then reassign the saved elements to $vairables array, or use DS module.


if you want to hide elements the appropiate way is to set the #access attribute on each element to FALSE, using a hook_form_alter()


Used by: All elements and forms

Description: Whether the element is accessible or not; when FALSE, the element is not rendered and the user submitted value is not taken into consideration.

Values: TRUE or FALSE.

Source: http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/7#access


Remember #access will not consider the particular elements in submit process. we can set #access to false if we want to totally ignore the fields.

In case you want to hide the field with a default value. just pass attribute in form alter:

$form[fldname]['#attributes']['style'][] = 'display:none;';

Your Answer

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

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