Commit 5e2ad8e4 authored by webchick's avatar webchick

#360128 by quicksketch: Create centralized ahah callback to reduce duplicate code.

parent f12b1b63
......@@ -1758,6 +1758,39 @@ function weight_value(&$form) {
}
}
/**
* Menu callback for AHAH callbacks through the #ahah['callback'] FAPI property.
*/
function form_ahah_callback() {
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
// Get the form from the cache.
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
// We will run some of the submit handlers so we need to disable redirecting.
$form['#redirect'] = FALSE;
// We need to process the form, prepare for that by setting a few internals
// variables.
$form['#post'] = $_POST;
$form['#programmed'] = FALSE;
$form_state['post'] = $_POST;
// Build, validate and if possible, submit the form.
drupal_process_form($form_id, $form, $form_state);
// This call recreates the form relying solely on the form_state that the
// drupal_process_form set up.
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
// Get the callback function from the clicked button.
$callback = $form_state['clicked_button']['#ahah']['callback'];
$callback($form, $form_state);
}
/**
* Roll out a single radios element to a list of radios,
* using the options array as index.
......@@ -1875,7 +1908,7 @@ function form_process_input_format($element) {
function form_process_ahah($element) {
static $js_added = array();
// Add a reasonable default event handler if none specified.
if (isset($element['#ahah']['path']) && !isset($element['#ahah']['event'])) {
if (isset($element['#ahah']) && !isset($element['#ahah']['event'])) {
switch ($element['#type']) {
case 'submit':
case 'button':
......@@ -1905,12 +1938,12 @@ function form_process_ahah($element) {
// Adding the same javascript settings twice will cause a recursion error,
// we avoid the problem by checking if the javascript has already been added.
if (isset($element['#ahah']['path']) && isset($element['#ahah']['event']) && !isset($js_added[$element['#id']])) {
if ((isset($element['#ahah']['callback']) || isset($element['#ahah']['path'])) && isset($element['#ahah']['event']) && !isset($js_added[$element['#id']])) {
drupal_add_js('misc/jquery.form.js', array('weight' => JS_LIBRARY));
drupal_add_js('misc/ahah.js');
$ahah_binding = array(
'url' => url($element['#ahah']['path']),
'url' => isset($element['#ahah']['callback']) ? url('system/ahah') : url($element['#ahah']['path']),
'event' => $element['#ahah']['event'],
'keypress' => empty($element['#ahah']['keypress']) ? NULL : $element['#ahah']['keypress'],
'wrapper' => empty($element['#ahah']['wrapper']) ? NULL : $element['#ahah']['wrapper'],
......
......@@ -118,13 +118,6 @@ function poll_menu() {
'type' => MENU_LOCAL_TASK,
);
$items['poll/js'] = array(
'title' => 'Javascript Choice Form',
'page callback' => 'poll_choice_js',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
......@@ -266,7 +259,7 @@ function poll_form(&$node, $form_state) {
'#weight' => 1,
'#submit' => array('poll_more_choices_submit'), // If no javascript action.
'#ahah' => array(
'path' => 'poll/js',
'callback' => 'poll_choice_js',
'wrapper' => 'poll-choices',
'method' => 'replace',
'effect' => 'fade',
......@@ -315,7 +308,7 @@ function poll_more_choices_submit($form, &$form_state) {
// Make the changes we want to the form state.
if ($form_state['values']['poll_more']) {
$n = $_GET['q'] == 'poll/js' ? 1 : 5;
$n = $_GET['q'] == 'system/ahah' ? 1 : 5;
$form_state['choice_count'] = count($form_state['values']['choice']) + $n;
}
}
......@@ -361,36 +354,13 @@ function _poll_choice_form($key, $chid = NULL, $value = '', $votes = 0, $weight
}
/**
* Menu callback for AHAH additions.
* Menu callback for AHAH additions. Render the new poll choices.
*/
function poll_choice_js() {
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
// Get the form from the cache.
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
// We will run some of the submit handlers so we need to disable redirecting.
$form['#redirect'] = FALSE;
// We need to process the form, prepare for that by setting a few internals
// variables.
$form['#post'] = $_POST;
$form['#programmed'] = FALSE;
$form_state['post'] = $_POST;
// Build, validate and if possible, submit the form.
drupal_process_form($form_id, $form, $form_state);
// This call recreates the form relying solely on the form_state that the
// drupal_process_form set up.
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
// Render the new output.
function poll_choice_js($form, $form_state) {
$choice_form = $form['choice_wrapper']['choice'];
unset($choice_form['#prefix'], $choice_form['#suffix']); // Prevent duplicate wrappers.
// Prevent duplicate wrappers.
unset($choice_form['#prefix'], $choice_form['#suffix']);
$output = theme('status_messages') . drupal_render($choice_form);
drupal_json(array('status' => TRUE, 'data' => $output));
......
......@@ -187,7 +187,7 @@ class PollJSAddChoice extends DrupalWebTestCase {
// @TODO: the framework should make it possible to submit a form to a
// different URL than its action or the current. For now, we can just force
// it.
$this->additionalCurlOptions[CURLOPT_URL] = url('poll/js', array('absolute' => TRUE));
$this->additionalCurlOptions[CURLOPT_URL] = url('system/ahah', array('absolute' => TRUE));
$this->drupalPost(NULL, $edit, t('More choices'));
unset($this->additionalCurlOptions[CURLOPT_URL]);
......
......@@ -408,6 +408,12 @@ function system_menu() {
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['system/ahah'] = array(
'title' => 'AHAH callback',
'page callback' => 'form_ahah_callback',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['system/timezone'] = array(
'title' => 'Time zone',
'page callback' => 'system_timezone',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment