0

I am currently using the following code to hide the fields in the form

1.  $form['field_name']['#access'] = FALSE;
2.  hide($form['picture']);
3.  unset($form['picture']);
4.  $form['account']['mail']['#disabled'] = TRUE; 

i saw this post https://stackoverflow.com/a/21041471/2251933 , https://drupal.stackexchange.com/a/49985/36808 explains some of the point but not all.

i want to retain the values in the form after form save but for some reason i want to hide the fields in the form.

what is the best way to do it and why it is so.

10

The basic differences are:

$form['field_name']['#access'] = FALSE; hides the form element. When saving the form Drupal will not touch the field. That means the initial value will remain. Maybe interesting: Other users can do with that node's field what ever they want. You saving the node will not undo their changes, although you only know about the old value of that field.

hide($form['picture']); is afaik used in theme functions only. It can cause forms to break because after saving a form this field is expected to have a value but is not even rendered. So use this with care. When you hide a field that is important make sure to manually render it later. Calling hide will also hide all child elements of the element you hide. A use case could be: Render child elements of a form manully and call hide on those elements. When you now render the parent element only those child elements are rendered that did not go through hide.

unset($form['picture']); should not be used. It just drops parts of an array. It is better to use one of the other three functions that keep the data consistent and not delete stuff Drupal expects.

$form['account']['mail']['#disabled'] = TRUE; will not hide the element but just make it unchangable (greyed dropdowns, etc.). It acts like a normal field but the initial value cannot be changed by the user.

In addition to what you asked, Clive added something:

Form elements are commonly converted to array('#type' => 'value', '#value' => 'foo') to remove them from the form but keep them server side, and not mess up the submission process. It's the best of all available methods IMO

Hope that help although I am not completely sure about the consequences of unset.

cheers, j

| improve this answer | |
  • 1
    Good answer - the only thing that's missing is a mention of value type elements: form elements are commonly converted to array('#type' => 'value', '#value' => 'foo') to remove them from the form but keep them server side, and not mess up the submission process. It's the best of all available methods IMO – Clive Feb 17 '15 at 16:14
  • It might be good to be aware of this bug in Drupal related to #access. – mickadoo Sep 28 '17 at 11:25
0

I think you should try

$form['field_name']['#access'] = FALSE;

Because you want to retain the values in the form after form save. It just hide your field.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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