Issue #1957590 by dawehner, mkadin: Remove remaining procedural ajax command usages.

8.0.x
catch 2013-04-09 21:30:46 +01:00
parent 90ead8f9fa
commit e287c9de95
6 changed files with 75 additions and 285 deletions

View File

@ -732,73 +732,6 @@ function ajax_command_insert($selector, $html, $settings = NULL) {
); );
} }
/**
* Creates a Drupal Ajax 'insert/replaceWith' command.
*
* The 'insert/replaceWith' command instructs the client to use jQuery's
* replaceWith() method to replace each element matched matched by the given
* selector with the given HTML.
*
* This command is implemented by Drupal.ajax.prototype.commands.insert()
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string. If the command is a response to a request from
* an #ajax form element then this value can be NULL.
* @param $html
* The data to use with the jQuery replaceWith() method.
* @param $settings
* An optional array of settings that will be used for this command only.
*
* @return
* An array suitable for use with the ajax_render() function.
*
* See
* @link http://docs.jquery.com/Manipulation/replaceWith#content jQuery replaceWith command @endlink
*/
function ajax_command_replace($selector, $html, $settings = NULL) {
return array(
'command' => 'insert',
'method' => 'replaceWith',
'selector' => $selector,
'data' => $html,
'settings' => $settings,
);
}
/**
* Creates a Drupal Ajax 'insert/html' command.
*
* The 'insert/html' command instructs the client to use jQuery's html()
* method to set the HTML content of each element matched by the given
* selector while leaving the outer tags intact.
*
* This command is implemented by Drupal.ajax.prototype.commands.insert()
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string. If the command is a response to a request from
* an #ajax form element then this value can be NULL.
* @param $html
* The data to use with the jQuery html() method.
* @param $settings
* An optional array of settings that will be used for this command only.
*
* @return
* An array suitable for use with the ajax_render() function.
*
* @see http://docs.jquery.com/Attributes/html#val
*/
function ajax_command_html($selector, $html, $settings = NULL) {
return array(
'command' => 'insert',
'method' => 'html',
'selector' => $selector,
'data' => $html,
'settings' => $settings,
);
}
/** /**
* Creates a Drupal Ajax 'insert/prepend' command. * Creates a Drupal Ajax 'insert/prepend' command.
* *
@ -865,72 +798,6 @@ function ajax_command_append($selector, $html, $settings = NULL) {
); );
} }
/**
* Creates a Drupal Ajax 'insert/after' command.
*
* The 'insert/after' command instructs the client to use jQuery's after()
* method to insert the given HTML content after each element matched by
* the given selector.
*
* This command is implemented by Drupal.ajax.prototype.commands.insert()
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string. If the command is a response to a request from
* an #ajax form element then this value can be NULL.
* @param $html
* The data to use with the jQuery after() method.
* @param $settings
* An optional array of settings that will be used for this command only.
*
* @return
* An array suitable for use with the ajax_render() function.
*
* @see http://docs.jquery.com/Manipulation/after#content
*/
function ajax_command_after($selector, $html, $settings = NULL) {
return array(
'command' => 'insert',
'method' => 'after',
'selector' => $selector,
'data' => $html,
'settings' => $settings,
);
}
/**
* Creates a Drupal Ajax 'insert/before' command.
*
* The 'insert/before' command instructs the client to use jQuery's before()
* method to insert the given HTML content before each of elements matched by
* the given selector.
*
* This command is implemented by Drupal.ajax.prototype.commands.insert()
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string. If the command is a response to a request from
* an #ajax form element then this value can be NULL.
* @param $html
* The data to use with the jQuery before() method.
* @param $settings
* An optional array of settings that will be used for this command only.
*
* @return
* An array suitable for use with the ajax_render() function.
*
* @see http://docs.jquery.com/Manipulation/before#content
*/
function ajax_command_before($selector, $html, $settings = NULL) {
return array(
'command' => 'insert',
'method' => 'before',
'selector' => $selector,
'data' => $html,
'settings' => $settings,
);
}
/** /**
* Creates a Drupal Ajax 'remove' command. * Creates a Drupal Ajax 'remove' command.
* *
@ -1043,91 +910,6 @@ function ajax_command_settings($argument, $merge = FALSE) {
); );
} }
/**
* Creates a Drupal Ajax 'data' command.
*
* The 'data' command instructs the client to attach the name=value pair of
* data to the selector via jQuery's data cache.
*
* This command is implemented by Drupal.ajax.prototype.commands.data()
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string. If the command is a response to a request from
* an #ajax form element then this value can be NULL.
* @param $name
* The name or key (in the key value pair) of the data attached to this
* selector.
* @param $value
* The value of the data. Not just limited to strings can be any format.
*
* @return
* An array suitable for use with the ajax_render() function.
*
* @see http://docs.jquery.com/Core/data#namevalue
*/
function ajax_command_data($selector, $name, $value) {
return array(
'command' => 'data',
'selector' => $selector,
'name' => $name,
'value' => $value,
);
}
/**
* Creates a Drupal Ajax 'invoke' command.
*
* The 'invoke' command will instruct the client to invoke the given jQuery
* method with the supplied arguments on the elements matched by the given
* selector. Intended for simple jQuery commands, such as attr(), addClass(),
* removeClass(), toggleClass(), etc.
*
* This command is implemented by Drupal.ajax.prototype.commands.invoke()
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string. If the command is a response to a request from
* an #ajax form element then this value can be NULL.
* @param $method
* The jQuery method to invoke.
* @param $args
* (optional) A list of arguments to the jQuery $method, if any.
*
* @return
* An array suitable for use with the ajax_render() function.
*/
function ajax_command_invoke($selector, $method, array $args = array()) {
return array(
'command' => 'invoke',
'selector' => $selector,
'method' => $method,
'args' => $args,
);
}
/**
* Creates a Drupal Ajax 'restripe' command.
*
* The 'restripe' command instructs the client to restripe a table. This is
* usually used after a table has been modified by a replace or append command.
*
* This command is implemented by Drupal.ajax.prototype.commands.restripe()
* defined in misc/ajax.js.
*
* @param $selector
* A jQuery selector string.
*
* @return
* An array suitable for use with the ajax_render() function.
*/
function ajax_command_restripe($selector) {
return array(
'command' => 'restripe',
'selector' => $selector,
);
}
/** /**
* Creates a Drupal Ajax 'add_css' command. * Creates a Drupal Ajax 'add_css' command.
* *

View File

@ -83,11 +83,6 @@ class FrameworkTest extends AjaxTestBase {
* Tests behavior of ajax_render_error(). * Tests behavior of ajax_render_error().
*/ */
function testAJAXRenderError() { function testAJAXRenderError() {
// Verify default error message.
$commands = $this->drupalGetAJAX('ajax-test/render-error');
$expected = new AlertCommand(t('An error occurred while handling the request: The server received invalid input.'));
$this->assertCommand($commands, $expected->render(), 'ajax_render_error() invokes alert command.');
// Verify custom error message. // Verify custom error message.
$edit = array( $edit = array(
'message' => 'Custom error message.', 'message' => 'Custom error message.',

View File

@ -5,6 +5,8 @@
* Admin page callbacks for the system module. * Admin page callbacks for the system module.
*/ */
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\ReplaceCommand;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
@ -2919,8 +2921,9 @@ function system_date_time_lookup($form, &$form_state) {
// Return a command instead of a string, since the Ajax framework // Return a command instead of a string, since the Ajax framework
// automatically prepends an additional empty DIV element for a string, which // automatically prepends an additional empty DIV element for a string, which
// breaks the layout. // breaks the layout.
$commands[] = ajax_command_replace('#edit-date-format-suffix', '<small id="edit-date-format-suffix">' . $format . '</small>'); $response = new AjaxResponse();
return array('#type' => 'ajax', '#commands' => $commands); $response->addCommand(new ReplaceCommand('#edit-date-format-suffix', '<small id="edit-date-format-suffix">' . $format . '</small>'));
return $response;
} }
/** /**

View File

@ -5,6 +5,8 @@
* Simpletest mock module for Ajax forms testing. * Simpletest mock module for Ajax forms testing.
*/ */
use Drupal\Core\Ajax;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\ajax_forms_test\Callbacks; use Drupal\ajax_forms_test\Callbacks;
/** /**
@ -271,18 +273,18 @@ function ajax_forms_test_ajax_commands_form($form, &$form_state) {
function ajax_forms_test_advanced_commands_after_callback($form, $form_state) { function ajax_forms_test_advanced_commands_after_callback($form, $form_state) {
$selector = '#after_div'; $selector = '#after_div';
$commands = array(); $response = new AjaxResponse();
$commands[] = ajax_command_after($selector, "This will be placed after"); $response->addCommand(new Ajax\AfterCommand($selector, "This will be placed after"));
return array('#type' => 'ajax', '#commands' => $commands); return $response;
} }
/** /**
* Ajax form callback: Selects 'alert'. * Ajax form callback: Selects 'alert'.
*/ */
function ajax_forms_test_advanced_commands_alert_callback($form, $form_state) { function ajax_forms_test_advanced_commands_alert_callback($form, $form_state) {
$commands = array(); $response = new AjaxResponse();
$commands[] = ajax_command_alert("Alert"); $response->addCommand(new Ajax\AlertCommand('Alert'));
return array('#type' => 'ajax', '#commands' => $commands); return $response;
} }
/** /**
@ -290,9 +292,9 @@ function ajax_forms_test_advanced_commands_alert_callback($form, $form_state) {
*/ */
function ajax_forms_test_advanced_commands_append_callback($form, $form_state) { function ajax_forms_test_advanced_commands_append_callback($form, $form_state) {
$selector = '#append_div'; $selector = '#append_div';
$commands = array(); $response = new AjaxResponse();
$commands[] = ajax_command_append($selector, "Appended text"); $response->addCommand(new Ajax\AppendCommand($selector, "Appended text"));
return array('#type' => 'ajax', '#commands' => $commands); return $response;
} }
/** /**
@ -300,27 +302,27 @@ function ajax_forms_test_advanced_commands_append_callback($form, $form_state) {
*/ */
function ajax_forms_test_advanced_commands_before_callback($form, $form_state) { function ajax_forms_test_advanced_commands_before_callback($form, $form_state) {
$selector = '#before_div'; $selector = '#before_div';
$response = new AjaxResponse();
$commands = array(); $response->addCommand(new Ajax\BeforeCommand($selector, "Before text"));
$commands[] = ajax_command_before($selector, "Before text"); return $response;
return array('#type' => 'ajax', '#commands' => $commands);
} }
/** /**
* Ajax form callback: Selects 'changed'. * Ajax form callback: Selects 'changed'.
*/ */
function ajax_forms_test_advanced_commands_changed_callback($form, $form_state) { function ajax_forms_test_advanced_commands_changed_callback($form, $form_state) {
$commands[] = ajax_command_changed('#changed_div'); $response = new AjaxResponse();
return array('#type' => 'ajax', '#commands' => $commands); $response->addCommand(new Ajax\ChangedCommand('#changed_div'));
return $response;
} }
/** /**
* Ajax form callback: Selects 'changed' with asterisk marking inner div. * Ajax form callback: Selects 'changed' with asterisk marking inner div.
*/ */
function ajax_forms_test_advanced_commands_changed_asterisk_callback($form, $form_state) { function ajax_forms_test_advanced_commands_changed_asterisk_callback($form, $form_state) {
$commands = array(); $response = new AjaxResponse();
$commands[] = ajax_command_changed('#changed_div', '#changed_div_mark_this'); $response->addCommand(new Ajax\ChangedCommand('#changed_div', '#changed_div_mark_this'));
return array('#type' => 'ajax', '#commands' => $commands); return $response;
} }
/** /**
@ -330,9 +332,9 @@ function ajax_forms_test_advanced_commands_css_callback($form, $form_state) {
$selector = '#css_div'; $selector = '#css_div';
$color = 'blue'; $color = 'blue';
$commands = array(); $response = new AjaxResponse();
$commands[] = ajax_command_css($selector, array('background-color' => $color)); $response->addCommand(new Ajax\CssCommand($selector, array('background-color' => $color)));
return array('#type' => 'ajax', '#commands' => $commands); return $response;
} }
/** /**
@ -340,83 +342,82 @@ function ajax_forms_test_advanced_commands_css_callback($form, $form_state) {
*/ */
function ajax_forms_test_advanced_commands_data_callback($form, $form_state) { function ajax_forms_test_advanced_commands_data_callback($form, $form_state) {
$selector = '#data_div'; $selector = '#data_div';
$response = new AjaxResponse();
$commands = array(); $response->addCommand(new Ajax\DataCommand($selector, 'testkey', 'testvalue'));
$commands[] = ajax_command_data($selector, 'testkey', 'testvalue'); return $response;
return array('#type' => 'ajax', '#commands' => $commands);
} }
/** /**
* Ajax form callback: Selects 'invoke'. * Ajax form callback: Selects 'invoke'.
*/ */
function ajax_forms_test_advanced_commands_invoke_callback($form, $form_state) { function ajax_forms_test_advanced_commands_invoke_callback($form, $form_state) {
$commands = array(); $response = new AjaxResponse();
$commands[] = ajax_command_invoke('#invoke_div', 'addClass', array('error')); $response->addCommand(new Ajax\InvokeCommand('#invoke_div', 'addClass', array('error')));
return array('#type' => 'ajax', '#commands' => $commands); return $response;
} }
/** /**
* Ajax form callback: Selects 'html'. * Ajax form callback: Selects 'html'.
*/ */
function ajax_forms_test_advanced_commands_html_callback($form, $form_state) { function ajax_forms_test_advanced_commands_html_callback($form, $form_state) {
$commands = array(); $response = new AjaxResponse();
$commands[] = ajax_command_html('#html_div', 'replacement text'); $response->addCommand(new Ajax\HtmlCommand('#html_div', 'replacement text'));
return array('#type' => 'ajax', '#commands' => $commands); return $response;
} }
/** /**
* Ajax form callback: Selects 'insert'. * Ajax form callback: Selects 'insert'.
*/ */
function ajax_forms_test_advanced_commands_insert_callback($form, $form_state) { function ajax_forms_test_advanced_commands_insert_callback($form, $form_state) {
$commands = array(); $response = new AjaxResponse();
$commands[] = ajax_command_insert('#insert_div', 'insert replacement text'); $response->addCommand(new Ajax\InsertCommand('#insert_div', 'insert replacement text'));
return array('#type' => 'ajax', '#commands' => $commands); return $response;
} }
/** /**
* Ajax form callback: Selects 'prepend'. * Ajax form callback: Selects 'prepend'.
*/ */
function ajax_forms_test_advanced_commands_prepend_callback($form, $form_state) { function ajax_forms_test_advanced_commands_prepend_callback($form, $form_state) {
$commands = array(); $response = new AjaxResponse();
$commands[] = ajax_command_prepend('#prepend_div', "prepended text"); $response->addCommand(new Ajax\PrependCommand('#prepend_div', "prepended text"));
return array('#type' => 'ajax', '#commands' => $commands); return $response;
} }
/** /**
* Ajax form callback: Selects 'remove'. * Ajax form callback: Selects 'remove'.
*/ */
function ajax_forms_test_advanced_commands_remove_callback($form, $form_state) { function ajax_forms_test_advanced_commands_remove_callback($form, $form_state) {
$commands = array(); $response = new AjaxResponse();
$commands[] = ajax_command_remove('#remove_text'); $response->addCommand(new Ajax\RemoveCommand('#remove_text'));
return array('#type' => 'ajax', '#commands' => $commands); return $response;
} }
/** /**
* Ajax form callback: Selects 'restripe'. * Ajax form callback: Selects 'restripe'.
*/ */
function ajax_forms_test_advanced_commands_restripe_callback($form, $form_state) { function ajax_forms_test_advanced_commands_restripe_callback($form, $form_state) {
$commands = array(); $response = new AjaxResponse();
$commands[] = ajax_command_restripe('#restripe_table'); $response->addCommand(new Ajax\RestripeCommand('#restripe_table'));
return array('#type' => 'ajax', '#commands' => $commands); return $response;
} }
/** /**
* Ajax form callback: Selects 'settings'. * Ajax form callback: Selects 'settings'.
*/ */
function ajax_forms_test_advanced_commands_settings_callback($form, $form_state) { function ajax_forms_test_advanced_commands_settings_callback($form, $form_state) {
$commands = array();
$setting['ajax_forms_test']['foo'] = 42; $setting['ajax_forms_test']['foo'] = 42;
$commands[] = ajax_command_settings($setting); $response = new AjaxResponse();
return array('#type' => 'ajax', '#commands' => $commands); $response->addCommand(new Ajax\SettingsCommand($setting));
return $response;
} }
/** /**
* Ajax callback for 'add_css'. * Ajax callback for 'add_css'.
*/ */
function ajax_forms_test_advanced_commands_add_css_callback($form, $form_state) { function ajax_forms_test_advanced_commands_add_css_callback($form, $form_state) {
$commands = array(); $response = new AjaxResponse();
$commands[] = ajax_command_add_css('my/file.css'); $response->addCommand(new Ajax\AddCssCommand('my/file.css'));
return array('#type' => 'ajax', '#commands' => $commands); return $response;
} }
/** /**
@ -426,7 +427,8 @@ function ajax_forms_test_advanced_commands_settings_with_merging_callback($form,
drupal_add_js(array('ajax_forms_test' => array('foo' => 42)), 'setting'); drupal_add_js(array('ajax_forms_test' => array('foo' => 42)), 'setting');
drupal_add_js(array('ajax_forms_test' => array('foo' => 9001)), 'setting'); drupal_add_js(array('ajax_forms_test' => array('foo' => 9001)), 'setting');
return array('#type' => 'ajax', '#commands' => array()); $response = new AjaxResponse();
return $response;
} }
/** /**

View File

@ -7,6 +7,10 @@
namespace Drupal\ajax_forms_test; namespace Drupal\ajax_forms_test;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\DataCommand;
use Drupal\Core\Ajax\HtmlCommand;
/** /**
* Simple object for testing methods as Ajax callbacks. * Simple object for testing methods as Ajax callbacks.
*/ */
@ -16,19 +20,19 @@ class Callbacks {
* Ajax callback triggered by select. * Ajax callback triggered by select.
*/ */
function selectCallback($form, $form_state) { function selectCallback($form, $form_state) {
$commands = array(); $response = new AjaxResponse();
$commands[] = ajax_command_html('#ajax_selected_color', $form_state['values']['select']); $response->addCommand(new HtmlCommand('#ajax_selected_color', $form_state['values']['select']));
$commands[] = ajax_command_data('#ajax_selected_color', 'form_state_value_select', $form_state['values']['select']); $response->addCommand(new DataCommand('#ajax_selected_color', 'form_state_value_select', $form_state['values']['select']));
return array('#type' => 'ajax', '#commands' => $commands); return $response;
} }
/** /**
* Ajax callback triggered by checkbox. * Ajax callback triggered by checkbox.
*/ */
function checkboxCallback($form, $form_state) { function checkboxCallback($form, $form_state) {
$commands = array(); $response = new AjaxResponse();
$commands[] = ajax_command_html('#ajax_checkbox_value', (int) $form_state['values']['checkbox']); $response->addCommand(new HtmlCommand('#ajax_checkbox_value', (int) $form_state['values']['checkbox']));
$commands[] = ajax_command_data('#ajax_checkbox_value', 'form_state_value_select', (int) $form_state['values']['checkbox']); $response->addCommand(new DataCommand('#ajax_checkbox_value', 'form_state_value_select', (int) $form_state['values']['checkbox']));
return array('#type' => 'ajax', '#commands' => $commands); return $response;
} }
} }

View File

@ -6,6 +6,7 @@
*/ */
use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\AlertCommand;
use Drupal\Core\Ajax\OpenDialogCommand; use Drupal\Core\Ajax\OpenDialogCommand;
use Drupal\Core\Ajax\OpenModalDialogCommand; use Drupal\Core\Ajax\OpenModalDialogCommand;
use Drupal\Core\Ajax\CloseDialogCommand; use Drupal\Core\Ajax\CloseDialogCommand;
@ -70,7 +71,8 @@ function ajax_test_system_theme_info() {
*/ */
function ajax_test_render() { function ajax_test_render() {
drupal_add_js(array('ajax' => 'test'), 'setting'); drupal_add_js(array('ajax' => 'test'), 'setting');
return array('#type' => 'ajax', '#commands' => array()); $response = new AjaxResponse();
return $response;
} }
/** /**
@ -102,7 +104,9 @@ function ajax_test_error() {
if (!empty($_GET['message'])) { if (!empty($_GET['message'])) {
$message = $_GET['message']; $message = $_GET['message'];
} }
return array('#type' => 'ajax', '#error' => $message); $response = new AjaxResponse();
$response->addCommand(new AlertCommand($message));
return $response;
} }
/** /**