How to alter a form element to include it inside a fieldset 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?
This code might help you out.

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

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

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