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!
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
'#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
#valueas normal. This is my preferred method.
'#access' => FALSEon the element to denote it should not be outputted to the screen.
'#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).
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.