I need to combine and return two forms into a single form. The code below combines my two forms...

/**
 * Form Elements - return step1 and step2
 */
function form_fullform($form, &$form_state) {
  $form = array();
  $form_step1 = drupal_get_form('form_step1');
  $form_step2 = drupal_get_form('form_step2');
  $form = drupal_array_merge_deep($form_step1, $form_step2);
  return $form;
}

...but it displays the fields are in the wrong order. Has anyone encountered this before?

Comments

jaypan’s picture

This isn't going to work for two reasons:

1) The weights are sorted before being called into drupal_get_form, and you are merging it from there, so the weights aren't likely to be obeyed.
2) You have called drupal_get_form() twice, which means that merging them won't work, since you have separate form IDs.

Contact me to contract me for D7 -> D10/11 migrations.

ooXei1sh’s picture

Thank you for the reply. I ended up using conditional statements within a single form builder function and it seems to be working.

/**
 * List of forms with associated function
 */
function myform_forms() {
  return array(
    'myform' => array(
	  'form' => 'form_myform',
    ),
    'myform_contactinfo' => array(
	  'form' => 'form_myform',
    ),
    'myform_questions' => array(
	  'form' => 'form_myform',
    ),
  );
}

/**
 * Form Builder Function
 */
function myform($form, &$form_state) {
  // init
  if (empty($form_state['current_form'])) {
	if (drupal_is_front_page()) {
	  $form_state['current_form'] = 'myform_questions';
	  $form_state['form_info'] = myform_forms();
	}
	else {
	  $form_state['current_form'] = 'myform';
	  $form_state['form_info'] = myform_forms();
	}
  }
  
  // set current form
  $current_form = &$form_state['current_form'];
  
  // set form id
  $form = array( '#id' => 'myform' );
  
  // return form elements for current form
  $form = $form_state['form_info'][$current_form]['form']($form, $form_state);

  if ($current_form == 'myform_questions') {
    // next button for steps
    $form['continue'] = array(
      '#type' => 'submit',
      '#value' => t('Continue'),
      '#name' => 'continue',
      '#submit' => array('myform_continue_submit'),
    );
  }
  else {
	// submit button
	$form['submit'] = array(
	  '#type' => 'submit',
	  '#value' => t('Send'),
	  '#submit' => array('myform_submit'),
	);
  }
  return $form;
}

/**
 * Form Elements
 */
function form_myform($form, &$form_state) {
  
  // set the current form 
  $current_form = $form_state['current_form'];
  
  if ($current_form == 'myform' || $current_form == 'myform_contactinfo') {
	$form['FirstName'] = array(
	  '#type' => 'textfield',
	  '#title' => t('First Name'),
	  '#size' => 15,
	  '#maxlength' => 30,
	  '#required' => true,
	);
	$form['LastName'] = array(
	  '#type' => 'textfield',
	  '#title' => t('Last Name'),
	  '#size' => 15,
	  '#maxlength' => 30,
	  '#required' => true,
	);
	$form['Email'] = array(
	  '#type' => 'textfield',
	  '#title' => t('Email'),
	  '#size' => 15,
	  '#maxlength' => 50,
	  '#required' => true,
    );
  }

  if ($current_form == 'myform' || $current_form == 'myform_questions') {
	$form['ColorBlue'] = array(
	  '#type' => 'radios',
	  '#title' => t('Is your favorite color blue?'),
	  '#options' => array('Yes' => t('Yes'), 'No' => t('No')),
	);
	$form['ColorRed'] = array(
	  '#type' => 'radios',
	  '#title' => t('Is your favorite color red?'),
	  '#options' => array('Yes' => t('Yes'), 'No' => t('No')),
	);
	$form['ColorGreen'] = array(
	  '#type' => 'radios',
	  '#title' => t('Is your favorite color green?'),
	  '#options' => array('Yes' => t('Yes'), 'No' => t('No')),
	);
  }
  
  return $form;
}

I'm not sure that this is the best approach. But I really want to have one set of form elements and not have to duplicate them. If anyone else is looking at this and needs to fill in the blanks of what I left out just download the 'examples' module and checkout the form_example_wizard.