Issue #1802072 by fubhy, Stalski: Ajax callbacks should be callbacks defined as function or class method.

8.0.x
webchick 2012-10-12 11:53:59 -07:00
parent 22ac2df4d9
commit 07ab1e8f61
3 changed files with 42 additions and 24 deletions

View File

@ -376,8 +376,8 @@ function ajax_form_callback() {
if (!empty($form_state['triggering_element'])) {
$callback = $form_state['triggering_element']['#ajax']['callback'];
}
if (!empty($callback) && function_exists($callback)) {
return $callback($form, $form_state);
if (!empty($callback) && is_callable($callback)) {
return call_user_func_array($callback, array(&$form, &$form_state));
}
}

View File

@ -5,6 +5,8 @@
* Simpletest mock module for Ajax forms testing.
*/
use Drupal\ajax_forms_test\Callbacks;
/**
* Implements hook_menu().
*/
@ -41,6 +43,8 @@ function ajax_forms_test_menu() {
* Tests form_state['values'] during callback.
*/
function ajax_forms_test_simple_form($form, &$form_state) {
$object = new Callbacks();
$form = array();
$form['select'] = array(
'#type' => 'select',
@ -49,7 +53,7 @@ function ajax_forms_test_simple_form($form, &$form_state) {
'green' => 'green',
'blue' => 'blue'),
'#ajax' => array(
'callback' => 'ajax_forms_test_simple_form_select_callback',
'callback' => array($object, 'selectCallback'),
),
'#suffix' => '<div id="ajax_selected_color">No color yet selected</div>',
);
@ -58,7 +62,7 @@ function ajax_forms_test_simple_form($form, &$form_state) {
'#type' => 'checkbox',
'#title' => t('Test checkbox'),
'#ajax' => array(
'callback' => 'ajax_forms_test_simple_form_checkbox_callback',
'callback' => array($object, 'checkboxCallback'),
),
'#suffix' => '<div id="ajax_checkbox_value">No action yet</div>',
);
@ -69,26 +73,6 @@ function ajax_forms_test_simple_form($form, &$form_state) {
return $form;
}
/**
* Ajax form callback: Selects color.
*/
function ajax_forms_test_simple_form_select_callback($form, $form_state) {
$commands = array();
$commands[] = ajax_command_html('#ajax_selected_color', $form_state['values']['select']);
$commands[] = ajax_command_data('#ajax_selected_color', 'form_state_value_select', $form_state['values']['select']);
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Ajax form callback: Selects checkbox value.
*/
function ajax_forms_test_simple_form_checkbox_callback($form, $form_state) {
$commands = array();
$commands[] = ajax_command_html('#ajax_checkbox_value', (int) $form_state['values']['checkbox']);
$commands[] = ajax_command_data('#ajax_checkbox_value', 'form_state_value_select', (int) $form_state['values']['checkbox']);
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Form constructor for the Ajax Command display form.
*/

View File

@ -0,0 +1,34 @@
<?php
/**
* @file
* Definition of Drupal\ajax_forms_test\Callbacks.
*/
namespace Drupal\ajax_forms_test;
/**
* Simple object for testing methods as Ajax callbacks.
*/
class Callbacks {
/**
* Ajax callback triggered by select.
*/
function selectCallback($form, $form_state) {
$commands = array();
$commands[] = ajax_command_html('#ajax_selected_color', $form_state['values']['select']);
$commands[] = ajax_command_data('#ajax_selected_color', 'form_state_value_select', $form_state['values']['select']);
return array('#type' => 'ajax', '#commands' => $commands);
}
/**
* Ajax callback triggered by checkbox.
*/
function checkboxCallback($form, $form_state) {
$commands = array();
$commands[] = ajax_command_html('#ajax_checkbox_value', (int) $form_state['values']['checkbox']);
$commands[] = ajax_command_data('#ajax_checkbox_value', 'form_state_value_select', (int) $form_state['values']['checkbox']);
return array('#type' => 'ajax', '#commands' => $commands);
}
}