You are here

function multiform_get_form in Multiple forms 7.2

Same name and namespace in other branches
  1. 7 multiform.module \multiform_get_form()

Returns a form containing a number of other forms.

4 calls to multiform_get_form()
multiform_example_page in multiform_example/multiform_example.module
Page callback.
multiform_sharedfields_example_page in multiform_example/multiform_example.sharedfields.inc
Sharedfields example page callback.
multiform_test1_callback in tests/multiform_test.module
Callback for Test 1
multiform_test2_callback in tests/multiform_test.module
Callback for Test 2

File

./multiform.module, line 49

Code

function multiform_get_form($settings) {
  module_load_include('inc', 'multiform');
  module_load_include('inc', 'multiform', 'multiform.form');

  // Initialize multiform settings.
  $build_id_hash = multiform_init_settings($settings);

  // Set _POST values for sharedfields if any.
  multiform_sharedfields_process($settings);

  // Preprocess _POST:
  // Set submits according to submits map if any.
  // Exclude submits map subform's values from $_POST if any.
  multiform_preprocess_post($_POST, $settings);

  // Get subforms settings.
  $subforms = $settings['#subforms'];

  // Inititalize multiform form base structure and return build_id_hash.
  $build_id_hash = NULL;
  $form = multiform_init_form($build_id_hash);
  $post = !empty($_POST) ? $_POST : array();
  $rebuild = FALSE;

  // Array to store all required data for workflow.
  $multiform = array();
  foreach ($subforms as $index => $subform) {
    $subform_id = $subform['form_id'];
    $subform_args = $subform['args'];

    // @todo: Validate _POST data structure before doing anything.
    // Set _POST value as it should be in case of single form.
    if (isset($post['multiform'][$subform_id])) {
      $_POST = $post['multiform'][$subform_id];
    }

    // Initialize $subform_state.
    $subform_state = multiform_init_subform_state($subform_id, $subform_args, $settings, $subform);

    // Build and process current subform.
    $unprocessed_subform = array();
    $subform_form = multiform_drupal_build_subform($subform_id, $subform_state, $unprocessed_subform);

    // Store data for delayed submit.
    multiform_delayed_submit_prepare($multiform, $subform_form, $subform_state, $subform_id, $unprocessed_subform);

    // @todo: Temporary solution for #after_execute in multiform_delayed_submit_execute().
    $multiform['#subforms'][$subform_id]['#subform_settings'] = $subform;
    if ($subform_state['rebuild']) {
      $rebuild = TRUE;
    }
  }

  // Get form errors if any.
  $errors = form_get_errors();

  // @todo: Check for pinch points.
  // If any of subform is set to be rebuilt, rebuild all subforms.
  if ($rebuild) {
    foreach ($multiform['#subforms'] as $subform_id => $subform_prepare) {
      $multiform['#subforms'][$subform_id]['form_state']['rebuild'] = TRUE;
    }
  }

  // Log submitted data for debugging (in case anything goes wrong).
  if (empty($errors) && !empty($post['multiform']) && module_exists('multiform_log')) {

    // @todo: fill in context
    $context = array(
      'rebuild' => $rebuild,
    );
    $entry_data = array(
      'multiform_id' => !empty($settings['#multiform_id']) ? $settings['#multiform_id'] : DEFAULT_MULTIFORM_ID,
      'timestamp' => time(),
      'post_array' => $post,
      'context_data' => $context,
    );
    multiform_log_create_entry($entry_data);
  }
  foreach ($subforms as $index => $subform) {
    $subform_id = $subform['form_id'];
    $subform_form = $subform_state = array();
    multiform_delayed_submit_execute($multiform, $subform_form, $subform_state, $subform_id);

    // Do not render the <form> tags. Instead we render the <form> as a <div>.
    $subform_form['#theme_wrappers'] = array(
      'container',
    );

    // @todo: Check attributes for multiform itself
    // Unset any attributes specifics to form tags.
    $disallowed_attributes = array(
      'enctype',
      'action',
      'method',
    );
    $subform_form['#attributes'] = array_diff_key($subform_form['#attributes'], array_flip($disallowed_attributes));
    $form['multiform'][] = $subform_form;
    if (isset($subform_state['has_file_element'])) {
      $form['#attributes']['enctype'] = 'multipart/form-data';
    }
  }

  // Set theme function/template name suggestion for the form.
  if (!empty($settings['#multiform_id'])) {
    $form['#theme'] = array(
      $settings['#multiform_id'],
    );
  }

  // Prepare settings cache for submits map and hide mapped submits.
  multiform_cache_prepare($build_id_hash, $settings, $form);

  // @todo: check if it's required (since $errors was provided once earlier).
  $errors = form_get_errors();

  // Redirect if form was submitted successfully and #redirect_path is set.
  if (empty($errors) && !empty($post['multiform']) && isset($settings['#redirect_path']) && !$rebuild) {
    drupal_goto($settings['#redirect_path']);
  }

  // Set page title if it is set in multiform settings.
  if (isset($settings['#title'])) {
    drupal_set_title($settings['#title']);
  }
  return $form;
}