t('Ajax Forms Settings'), 'description' => t('Controls which forms should use AJAX Submissions.'), 'page callback' => 'drupal_get_form', 'page arguments' => array('zzzz_ajax_admin'), 'access arguments' => array('access administration pages'), 'type' => MENU_NORMAL_ITEM, ); return $items; } function zzzz_ajax_types() { return array_merge( zzzz_ajax_types_node(), zzzz_ajax_types_module()); } function zzzz_ajax_types_node() { $out = array(); $types = node_get_types(); foreach ($types as $k => $v) { $out[$k . '_node_form'] = ucwords('Content Type ' . $v->name); } return $out; } function zzzz_ajax_types_module() { $out = array(); $funcs = get_defined_functions(); foreach ($funcs['user'] as $f) { if (preg_match("/^([a-zA-Z0-9_]+?)_submit$/", $f, $func_name)) { $out[$func_name[1]] = ucwords(str_replace('_', ' ', $func_name[1])); } } return $out; } function zzzz_ajax_admin() { $form = array(); $form['ajax_types'] = array( '#type' => 'select', '#multiple' => true, '#options' => zzzz_ajax_types(), '#title' => t('Forms to Use AJAX Submissions'), '#default_value' => variable_get('ajax_types', array()), '#size' => 20, '#description' => t( "Select the forms you wish to use AJAX submissions.") ); return system_settings_form($form); } function zzzz_ajax_init() { drupal_add_js(drupal_get_path('module', 'zzzz_ajax') . '/Ajax.js', 'module'); } function zzzz_ajax_istype($form_id) { return array_key_exists($form_id, variable_get('ajax_types', array())); } function zzzz_ajax_form_alter(&$form, $form_state, $form_id) { if (zzzz_ajax_isType($form_id)) { $found = false; zzzz_ajax_setvalidator($form); zzzz_ajax_findsubmitter($form, $found); zzzz_ajax_setsubmitter($form, $found); //print "
";
    //print_r($form);
    //print "
"; } return true; } function zzzz_ajax_setvalidator(&$form) { $form['#validate'][] = 'zzzz_ajax_validator'; } function zzzz_ajax_setsubmitter(&$form, $found) { if (!$found) { $form['#submit'][] = 'zzzz_ajax_submitter'; } } function zzzz_ajax_findsubmitter(&$form, &$found) { foreach ($form as $form_key => $form_val) { if (is_array($form[$form_key])) { //submitter button if (array_key_exists('#type', $form[$form_key]) && $form[$form_key]['#type'] === 'submit' && $form_key === 'submit') { $form[$form_key]['#attributes'] = array('onclick' => 'return Ajax.go(this);'); if (array_key_exists('#submit', $form[$form_key]) && !empty($form[$form_key]['#submit'])) { $form[$form_key]['#submit'][] = 'zzzz_ajax_submitter'; $found = true; } } //nested else { zzzz_ajax_findsubmitter($form[$form_key], $found); } } } return true; } function zzzz_ajax_submitter($form, &$form_state) { $data = array(); // Go to redirection page if (array_key_exists('redirect', $form_state) && $form_state['redirect']) { $data['redirect'] = $form_state['redirect']; } // Display messages internally without redirect else { $messages = drupal_get_messages(null, true); if (array_key_exists('status', $messages)) { $data['messages_status'] = $messages['status']; } if (array_key_exists('warning', $messages)) { $data['messages_warning'] = $messages['warning']; } } $out = zzzz_ajax_buildOutput($data); zzzz_ajax_out($out); } function zzzz_ajax_validator() { if (array_key_exists('ajax', $_REQUEST)) { drupal_get_messages(null, true); $data = zzzz_ajax_buildOutput(array( 'messages_error' => form_get_errors() )); if (!$data['status']) { zzzz_ajax_out($data); } } } function zzzz_ajax_out($data) { header("Content-Type: application/json; charset=UTF-8"); print drupal_to_js($data); exit; } function zzzz_ajax_cleanid($field_id) { //taken from form_clean_id return str_replace(array('][', '_', ' '), '-', $field_id); } function zzzz_ajax_buildOutput($data) { $out = array( 'status' => true, 'messages_error' => array(), 'messages_status' => array(), 'messages_warning' => array(), 'redirect' => null ); // MESSAGE:ERROR if (array_key_exists('messages_error', $data) && $data['messages_error'] !== null) { $out['status'] = false; foreach ($data['messages_error'] as $k => $v) { $out['messages_error'][] = array( 'id' => zzzz_ajax_cleanid("edit-" . $k), 'value' => $v ); } } // MESSAGE:STATUS if (array_key_exists('messages_status', $data) && $data['messages_status'] !== null) { foreach ($data['messages_status'] as $k => $v) { $out['messages_status'][] = array( 'id' => (int)$k, 'value' => $v ); } } // MESSAGE:WARNING if (array_key_exists('messages_warning', $data) && $data['messages_warning'] !== null) { foreach ($data['messages_warning'] as $k => $v) { $out['messages_warning'][] = array( 'id' => (int)$k, 'value' => $v ); } } // Redirect if (array_key_exists('redirect', $data) && $data['redirect'] !== null) { $out['redirect'] = $data['redirect']; } return $out; } ?>