1

Possible Duplicate:
specify $form elements to be excluded from display?

I would like to hide a field on a form using hook_form_ID_alter. The form is owned by another module. This field cannot be unset because it contains a value.

Using CSS {display: none} for the standard field class is not good because the field should be hidden only in specific conditions (which must be determined using php).

In hook_form_ID_alter, I could add an #attribute to the field with a class value e.g. hideme and then use CSS to hide this class. But it will leave the field title (tried).

The field has #prefix set to <div class='some_class'> (in this particular case). This prefix precedes the field as well as the title when displayed in a browser. So I could analyze the prefix with regular expression and add a hideme class there. But I am not 100% sure what the #prefix will contain, so the regexp could be complex.

Perhaps there is a better way to hide a field with title on a form?

marked as duplicate by Clive Nov 19 '12 at 10:10

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

You can set the #access property for the form field to FALSE. The form field will not be shown to the users, but its value will be still be available in $form_state['values'] for the submission, and validation handlers.

Using #access is always the suggested way to avoid users can change the value of a form field, and avoid any third-party module depending on that form field starts to raise warnings, or fails to work as expected.

0

You can use the hide() function instead of unset(), if you want to retain the value in the field. From the docs:

Hides an element from later rendering.

In simple words, hide() marks the render-able array as already rendered, so that the theme's function will not render it.

  • hide() shouldn't be used for form elements, it can cause a lot of problems. See the comments on the accepted answer to the duplicate question :) – Clive Nov 19 '12 at 10:11

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