I want to hide certain elements in form using form_alter or a template file. I have read somewhere that in Drupal 7 , we can specify $form elements so it will not display in form, but I can't remember! Not unset() but some other function!

3 Answers 3


Using unset() isn't a good solution for forms (as I think you know), as you never know what submit/validate handlers might be expecting the unset element to exist when they're processed. This can lead to unwanted errors.

There are three methods you can safely use to 'hide' a form element

  • Use '#type' => 'value' on the element, which will have the effect that the value for the element will still be passed through to the submit/validate functions with its #value as normal. This is my preferred method.
  • Use '#access' => FALSE on the element to denote it should not be outputted to the screen.
  • Use '#type' => 'hidden' as Bart has suggested (although use of this depends on your use case; I imagine if you're trying to hide the value of a form element you don't want to output it in the form as a hidden input).
  • forgot hide() .
    – Yuseferi
    Sep 5, 2012 at 11:35
  • 2
    @zhilevan hide() shouldn't be used on form elements, it can easily mess the whole form submission process up as validators are still run for the element. For example, if you hide($form['title']); on a node form you won't ever be able to submit the form, as the title field is required, but a value won't be provided. The 3 methods above are the best ones to use, in that order :)
    – Clive
    Sep 5, 2012 at 11:48
  • @Clive thanks, i know above methods, my question was something related to hide() i think(still cant remember the function).. anyway please refer my comment in 1st answer, why drupal_render($form['base']['prices']['sell_price']) is not working?
    – Serjas
    Sep 5, 2012 at 11:54
  • Is that code in a template file that's theming the form? If not, the reason it's not working is that while you can print out any render element to the screen with drupal_render(), you won't get any of the form tags/tokens/etc. unless you output the whole $form array
    – Clive
    Sep 5, 2012 at 11:57
  • i used hook theme and print_r($form) and even drupal_render($form['title']) are working , the problem is the elements under vertcal tabs!
    – Serjas
    Sep 5, 2012 at 12:05

If you're using a template file you can hide stuff using hide().

If you want to hide a form element in hook_form_alter, unset() might be your best option.

If it's just to store a value you could use '#type' => 'hidden' as well.

  • hide is better method to exluce of element.
    – Yuseferi
    Sep 5, 2012 at 11:34
  • What I was trying to achieve was a custom Ubsercart product node form, all I wanted to display is sell_price, and hide other elements like shipping, weight etc. But drupal_render($form['base']['prices']['sell_price']) will display form , but its not saving!
    – Serjas
    Sep 5, 2012 at 11:35

I've found using #access to false can break some forms if used on submit buttons or similar. In those cases i've just used CSS to hide elements:

$form['basic']['submit']['#attributes']['class'][] = 'element-hidden';

Just another option.

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.