You are here

function asaf_ajax_callback in Asaf (ajax submit for any form) 7

Same name and namespace in other branches
  1. 8 asaf.module \asaf_ajax_callback()
1 call to asaf_ajax_callback()
asaf_drupal_goto_alter in ./asaf.module
1 string reference to 'asaf_ajax_callback'
asaf_handle_buttons in ./asaf.module

File

./asaf.module, line 305

Code

function asaf_ajax_callback(&$form, &$form_state) {
  $commands = array();
  if ($form_state['asaf']['options'][ASAF_SETTINGS_STATUS_MESSAGES] == 'hide') {
    theme('status_messages');
  }
  if (isset($form_state['redirect'])) {
    if (!is_array($form_state['redirect'])) {
      $form_state['redirect'] = array(
        $form_state['redirect'],
        array(
          'query' => array(),
        ),
      );
    }
    $commands[] = asaf_ajax_command_redirect($form_state['redirect']);
  }
  else {
    $processed_areas = array();
    $targets = !empty($form_state['triggering_element']['#asaf_target_area']) ? $form_state['triggering_element']['#asaf_target_area'] : 'form';
    $targets = !is_array($targets) ? array(
      $targets,
    ) : $targets;
    $area_render_command_callback = isset($form_state['asaf']['options']['form_render_command_callback']) && $form_state['asaf']['options']['form_render_command_callback'] && function_exists($form_state['asaf']['options']['form_render_command_callback']) ? $form_state['asaf']['options']['form_render_command_callback'] : 'asaf_default_area_render_command';
    foreach ($targets as $areaName) {
      $area = asaf_find_area($areaName, $form);
      if ($area['#asaf_area'] == $areaName && !isset($processed_areas[$areaName])) {
        $area_command = $area_render_command_callback($area, $form_state);
        if (!empty($area_command)) {
          $commands[] = $area_command;
        }
        $processed_areas[$areaName] = TRUE;
      }
    }

    // Send to the client new form_buid_id when pagecache mode enabled and form rebuild partially, without whole form
    if ($form_state['asaf']['options'][ASAF_SETTINGS_PAGE_CACHE] && !isset($processed_areas['form'])) {
      $commands[] = ajax_command_invoke('#' . asaf_get_area_wrapper_id('form', $form_state) . ' input[name="form_build_id"]', 'val', array(
        $form['#build_id'],
      ));
    }
  }

  // Flag allow some command alter hooks returns different list of command for validation errors case.
  $form_state['asaf_form_status'] = form_get_errors() ? 'errors' : '';
  if ($form_state['asaf_form_status'] == 'errors' && $form_state['asaf']['options'][ASAF_SETTINGS_STATUS_MESSAGES] == 'none') {
    $form_state['asaf']['options'][ASAF_SETTINGS_STATUS_MESSAGES] = 'default';
  }
  if (isset($form_state['asaf_ajax_commands']) && is_array($form_state['asaf_ajax_commands'])) {
    $commands = array_merge($commands, $form_state['asaf_ajax_commands']);
  }
  if ($form_state['asaf']['options'][ASAF_SETTINGS_STATUS_MESSAGES] == 'default') {
    $commands[] = ajax_command_prepend(NULL, theme('status_messages'));
  }
  if ($form_state['asaf_form_status'] != 'errors' || $form_state['asaf']['options'][ASAF_SETTINGS_CALL_COMMANDS_ALTER] == ASAF_SETTINGS_CALL_COMMANDS_ALTER_ALWAYS) {
    drupal_alter('asaf_form_ajax_commands', $commands, $form, $form_state, $form['#form_id']);
    drupal_alter('asaf_form_' . $form['#form_id'] . '_ajax_commands', $commands, $form, $form_state);

    // We don't use $form_id because we need real form id, not $form['#id'] or autogenerated value
  }
  unset($form_state['asaf_ajax_commands']);
  return array(
    '#type' => 'ajax',
    '#commands' => $commands,
  );
}