You are here

function subform_parent_after_submit in Subform 7

Submit handler to be run after all other submit handlers.

Registers this callback.

See also

subform_parent_after_build()

File

./subform.module, line 570
Defines a subform element type.

Code

function subform_parent_after_submit(&$form, &$form_state) {
  if (!empty($form_state['temporary']['subform'])) {
    $redirect = empty($form_state['programmed']) && empty($form_state['rebuild']) && empty($form_state['no_redirect']) && (!isset($form_state['redirect']) || $form_state['redirect'] !== FALSE);
    foreach ($form_state['temporary']['subform'] as $subform_name => &$subform_state) {
      if (!$subform_state['executed']) {

        // Not executed subforms need to be rebuild if the parent form does too.
        if ($form_state['rebuild']) {
          $subform_state['rebuild'] = TRUE;
        }
      }

      // Clear subform's cache if parent is redirecting or subform is executed.
      if ($redirect || $subform_state['executed']) {

        // We'll clear out the cached copies of the form and its stored data
        // here, as we've finished with them. The in-memory copies are still
        // here, though.
        if (!variable_get('cache', 0) && !empty($subform_state['values']['form_build_id'])) {
          cache_clear_all('form_' . $subform_state['values']['form_build_id'], 'cache_form');
          cache_clear_all('form_state_' . $subform_state['values']['form_build_id'], 'cache_form');
        }
      }

      // Prevent subforms from rebuilding while they shouldn't.
      if (isset($form_state['rebuild']) && !$subform_state['rebuild']) {

        // Reset subform state.
        $subform_state = form_state_defaults();

        // Remove subform input, including input for nested subforms.
        foreach ($form_state['input'] as $name => $value) {
          if ($name == $subform_name || strpos($name, $subform_name . '-') === 0) {
            unset($form_state['input'][$name]);
          }
        }
      }

      // Cache subform if neccesary. Caching the subforms here allows for making
      // changes to subform states in any submit handler.
      if (!$redirect && empty($form_state['rebuild']) && empty($form_state['programmed']) && empty($subform_state['rebuild']) && !empty($subform_state['cache']) && empty($subform_state['no_cache'])) {
        form_set_cache($subform_state['values']['form_build_id'], $subform_state['temporary']['subform_unprocessed'], $subform_state);
      }
    }
  }
}