4

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?

3 Answers 3

3

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

4
  • Daniel, how do I "move" the field to the fieldset? Jun 26, 2011 at 1:46
  • 1
    If you have your pre_render function just do $form['fieldset']['field'] = $form['field']; unset($form['field']); Jun 26, 2011 at 8:01
  • Hi Charlie, can you please share the code or output? i have to integrate the same functionality.
    – Ananth
    May 29, 2014 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. Dec 10, 2015 at 17:04
5

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.
    );
1

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;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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