drupaldrupal-7form-apidrupal-fieldshook-form-alter

Why can't I move a field into a fieldset in a Drupal form - fails to pick up current value


I have a node form in Drupal 7, in order to simplify it for the user I want to break it up into sections using the vertical tabs feature.

Using hook_form_FORMID_alter() I can move the fields without difficulty. When the node is saved, it writes the values correctly, and they appear in the node view.

But when I re-edit the node any value for a moved field is not set so I effectively lose the data. I've tried various options including changing the array_parents value in form_state['fields'][field][langcode].

(I wondered whether it would be better to move the fields during pre_render instead.)

Any ideas?


Solution

  • Field API fields by default are placed into a container field type. If you want to convert them to a fieldset in the vertical tabs, you can do the following:

    $form['field_tags']['#type'] = 'fieldset';
    $form['field_tags']['#title'] = 'Tags';
    $form['field_tags']['#group'] = 'additional_settings';
    

    A better solution would be to use the new Field Group module so you can make these modifications through the UI, rather than in code.