You are here

function multiform_preprocess_post in Multiple forms 7.2

Preprocess _POST and set triggering submits according to submits map.

1 call to multiform_preprocess_post()
multiform_get_form in ./multiform.module
Returns a form containing a number of other forms.

File

./multiform.inc, line 197

Code

function multiform_preprocess_post(&$post, $settings) {
  if (isset($post['multiform']['multiform_submits_form'])) {
    if (!empty($post['multiform']['multiform_submits_form']['op'])) {
      if (empty($post['multiform_settings_build_id'])) {
        return;
      }
      $multiform_settings_build_id = $post['multiform_settings_build_id'];
      $cache = cache_get($multiform_settings_build_id, 'cache');
      if (empty($cache)) {
        return;
      }
      $cached_settings = $cache->data;

      // #name of auxiliary submit.
      $name = $post['multiform']['multiform_submits_form']['op'];

      // Identify array key of the triggering submit, if any.
      foreach ($settings['#submit_map']['#submits'] as $k => $data) {
        if ($name == $data['#value']) {
          $key = $k;
          break;
        }
      }
      if (!empty($key)) {
        foreach ($settings['#subforms'] as $k => $data) {
          if (!empty($data['#map'])) {
            $form_id = $data['form_id'];
            foreach ($data['#map'] as $m => $map) {
              if (in_array($key, $map['#triggering_submit'])) {
                $parents = $map['#parents'];
                if (!empty($cached_settings['#subforms'][$k]['#map'][$m]['#element'])) {
                  $element = $cached_settings['#subforms'][$k]['#map'][$m]['#element'];

                  // Set mapped submit's value.
                  $post['multiform'][$form_id][$element['#name']] = $element['#value'];
                }
              }
            }
          }
        }
      }
    }

    // @todo: Check that there is no subform (added by user)
    // with form_id == 'multiform_submits_form' (since it is reserved for submits).
    // Always unset this subform.
    unset($post['multiform']['multiform_submits_form']);
  }
}