I know how to populate the form fields in hook_form_alter but i have no idea how to hide them...

regards Volkan


muschpusch’s picture

I found a solution at lullabot


Too lazy to read try to add '#access' = false to your form element

$form['author']['name']['#access'] = FALSE;

westbywest’s picture

Please note that if you restrict the access permissions for a form field, then you are also restricting the active user's ability to submit any value for that field, whether via manual entry or via hook_form_alter() 'fu.

That is, this hook_form_alter() code wouldn't work:

$form['author']['name']['#access'] = FALSE;
$form['author']['name']['#value'] = 'something';

As an alternative, try simply hiding the field (or change to type hidden) if you still want the acting user to modify a form's field in some controlled way:

$form['author']['name']['#prefix'] = '<div style="display:none;">';
$form['author']['name']['#value'] = 'something';
$form['author']['name']['#suffix'] = '</div>';
cdmo’s picture

Changing type to hidden also prevents setting values via hook_form_alter() calls. Guess we're left with hiding via CSS.


ehsankhfr’s picture

I checked both unset($form[...]) and $form['...']['#access']=false , both didn't work.

I guess styling hidden is the only option remained for this issue.

alesr’s picture

If you use "'#disabled' => 'TRUE'", you keep the value, but the fields are visible in the form. Sometime you don't want to show that to a user (data leak).
If you use "'#access' => 'FALSE'", you hide the field in the form, but lose the value of it.

To keep the default_value and hide the field you need to store the default value, unset field item and create it again with the value before you unset it.
Here's an example.

// Store default value in a variable.
$my_field_value = $form['my_field'][LANGUAGE_NONE]['#default_value']; 
// Unset "old" field item.
// Create new hidden field with the stored value.
$form['my_field'] = array(
   '#type' => 'hidden',
   '#value' => $my_field_value,

Works for creating and editing!

Thangobrind’s picture

This was a massive help and saved me loads of time