3

I am using field_attach_form() to display an entity's form. When I call this function, it automatically builds the form, which does not contain any fieldsets, just ungrouped form elements.

Before the form is displayed I would like to take some of the form elements and group them into fieldsets. If I have something like this:

$form array:
    form element 1
        attributes
    form element 2
        attributes
    fieldset1
        attributes
    fieldset2
        attributes

Is there a form element attribute or special function that will allow me to tell Drupal to automatically place "form element 1" into "fieldset1"?

Alternatively, can this be done at the core/entity level, not the custom module, so that when field_attach_form() is called the fieldsets are automatically created?

2

You can use $form['#pre_render'][] = 'yourfunction'; and move the fields to the fieldset in this function.

| improve this answer | |
  • Daniel, how do I "move" the field to the fieldset? – Carlos Muñiz Jun 26 '11 at 1:46
  • 1
    If you have your pre_render function just do $form['fieldset']['field'] = $form['field']; unset($form['field']); – Daniel Wehner Jun 26 '11 at 8:01
  • Hi Charlie, can you please share the code or output? i have to integrate the same functionality. – Ananth May 29 '14 at 5:28
  • Is there any reason to not just do this while creating the form rather than using pre_render? e.g. I call field_attach_form(), then immediately take the fields and move them to the fieldset array - seems to work. – james_alvarez Dec 10 '15 at 17:04
4

This code might help you out.

   $form['date_fieldset'] = array(
        '#type' => 'fieldset',
        '#title' => t('Select Start and End dates'),
        '#collapsible' => TRUE,
//        '#tree' => TRUE,
    );

    $form['date_fieldset']['date_inactive_start'] = array(
        '#title' => t('Start date'),
        '#type' => 'date_popup', // Provided by the date_popup module
        '#date_format' => 'd/m/Y',
        '#date_year_range' => '0:+1',
        '#required' => TRUE,
        '#size' => 8,
        '#default_value' => date('Y-m-d', time()), // Default value must be in 'Y-m-d' format.
    );

    $form['date_fieldset']['date_inactive_end'] = array(
        '#title' => t('End date'),
        '#type' => 'date_popup', // Provided by the date_popup module
        '#date_format' => 'd/m/Y',
        '#date_year_range' => '0:+2',
        '#required' => TRUE,
        '#size' => 8,
        '#default_value' => date('Y-m-d', time()), // Default value must be in 'Y-m-d' format.
    );
| improve this answer | |
0

If someone is looking for a complete example on how to do this:

// hook_form_BASE_FORM_ID_alter to target only article node edit forms
function my_module_form_article_node_form_alter(&$form, &$form_state, $form_id) {

    $form['#pre_render'][] = 'add_to_fieldset';

}

function add_to_fieldset($form) {
    // Adding element to the publishing options fieldset on node form

    $form['options']['element_to_add'] = $form['element_to_add'];
    unset($form['element_to_add']);

    return $form;

}
| 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.