Commit 3ad400ec authored by webchick's avatar webchick

Issue #2187483 by Cottser, agentrickard, dawehner, jessebeach: Toolbar subtree...

Issue #2187483 by Cottser, agentrickard, dawehner, jessebeach: Toolbar subtree is broken | JsonResponse->callback now returns data in an Insert Command without any support in ajax.js to eval the callback in the client.
parent fe61b43d
......@@ -26,18 +26,15 @@ class AjaxResponseRenderer {
* An Ajax response containing the controller result.
*/
public function render($content) {
// If there is already an AjaxResponse, then return it without manipulation.
if ($content instanceof AjaxResponse && $content->isOk()) {
// If there is already a Response object, return it without manipulation.
if ($content instanceof Response && $content->isOk()) {
return $content;
}
// Allow controllers to return a HtmlFragment or a Response object directly.
// Allow controllers to return an HtmlFragment directly.
if ($content instanceof HtmlFragment) {
$content = $content->getContent();
}
elseif ($content instanceof Response) {
$content = $content->getContent();
}
// Most controllers return a render array, but some return a string.
if (!is_array($content)) {
$content = array(
......@@ -50,7 +47,7 @@ public function render($content) {
if (isset($content['#type']) && ($content['#type'] == 'ajax')) {
// Complex Ajax callbacks can return a result that contains an error
// message or a specific set of commands to send to the browser.
$content += element_info('ajax');
$content += $this->elementInfo('ajax');
$error = $content['#error'];
if (!empty($error)) {
// Fall back to some default message otherwise use the specific one.
......@@ -61,18 +58,34 @@ public function render($content) {
}
}
$html = drupal_render($content);
$html = $this->drupalRender($content);
// The selector for the insert command is NULL as the new content will
// replace the element making the Ajax call. The default 'replaceWith'
// behavior can be changed with #ajax['method'].
$response->addCommand(new InsertCommand(NULL, $html));
$status_messages = array('#theme' => 'status_messages');
$output = drupal_render($status_messages);
$output = $this->drupalRender($status_messages);
if (!empty($output)) {
$response->addCommand(new PrependCommand(NULL, $output));
}
return $response;
}
/**
* Wraps drupal_render().
*
* @todo: Remove as part of https://drupal.org/node/2182149
*/
protected function drupalRender(&$elements, $is_recursive_call = FALSE) {
return drupal_render($elements, $is_recursive_call);
}
/**
* Wraps element_info().
*/
protected function elementInfo($type) {
return element_info($type);
}
}
<?php
/**
* @file
* Contains \Drupal\Tests\Core\Ajax\AjaxResponseRendererTest.
*/
namespace Drupal\Tests\Core\Ajax;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\AjaxResponseRenderer;
use Drupal\Core\Page\HtmlFragment;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\JsonResponse;
/**
* Tests \Drupal\Core\Ajax\AjaxResponseRenderer.
*
* @coversClass \Drupal\Core\Ajax\AjaxResponseRenderer
*/
class AjaxResponseRendererTest extends UnitTestCase {
/**
* The tested ajax response renderer.
*
* @var \Drupal\Tests\Core\Ajax\TestAjaxResponseRenderer
*/
protected $ajaxResponseRenderer;
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'Tests \Drupal\Core\Ajax\AjaxResponseRenderer.',
'description' => '',
'group' => 'Ajax',
);
}
/**
* {@inheritdoc}
*/
protected function setUp() {
$this->ajaxResponseRenderer = new TestAjaxResponseRenderer();
}
/**
* Tests the render method with an HtmlFragment object.
*
* @covers \Drupal\Core\Ajax\AjaxResponseRenderer::render
*/
public function testRenderWithFragmentObject() {
$html_fragment = new HtmlFragment('example content');
/** @var \Drupal\Core\Ajax\AjaxResponse $result */
$result = $this->ajaxResponseRenderer->render($html_fragment);
$this->assertInstanceOf('Drupal\Core\Ajax\AjaxResponse', $result);
$commands = $result->getCommands();
$this->assertEquals('insert', $commands[0]['command']);
$this->assertEquals('example content', $commands[0]['data']);
$this->assertEquals('insert', $commands[1]['command']);
$this->assertEquals('status_messages', $commands[1]['data']);
}
/**
* Tests the render method with an HtmlFragment object.
*
* @covers \Drupal\Core\Ajax\AjaxResponseRenderer::render
*/
public function testRenderWithString() {
$html_fragment = 'example content';
/** @var \Drupal\Core\Ajax\AjaxResponse $result */
$result = $this->ajaxResponseRenderer->render($html_fragment);
$this->assertInstanceOf('Drupal\Core\Ajax\AjaxResponse', $result);
$commands = $result->getCommands();
$this->assertEquals('insert', $commands[0]['command']);
$this->assertEquals('example content', $commands[0]['data']);
$this->assertEquals('insert', $commands[1]['command']);
$this->assertEquals('status_messages', $commands[1]['data']);
}
/**
* Tests the render method with a response object.
*
* @covers \Drupal\Core\Ajax\AjaxResponseRenderer::render
*/
public function testRenderWithResponseObject() {
$json_response = new JsonResponse(array('foo' => 'bar'));
$this->assertSame($json_response, $this->ajaxResponseRenderer->render($json_response));
}
/**
* Tests the render method with an Ajax response object.
*
* @covers \Drupal\Core\Ajax\AjaxResponseRenderer::render
*/
public function testRenderWithAjaxResponseObject() {
$ajax_response = new AjaxResponse(array('foo' => 'bar'));
$this->assertSame($ajax_response, $this->ajaxResponseRenderer->render($ajax_response));
}
}
class TestAjaxResponseRenderer extends AjaxResponseRenderer {
/**
* {@inheritdoc}
*/
protected function drupalRender(&$elements, $is_recursive_call = FALSE) {
if (isset($elements['#markup'])) {
return $elements['#markup'];
}
elseif (isset($elements['#theme'])) {
return $elements['#theme'];
}
else {
return 'Markup';
}
}
/**
* {@inheritdoc}
*/
protected function elementInfo($type) {
if ($type == 'ajax') {
return array(
'#header' => TRUE,
'#commands' => array(),
'#error' => NULL,
);
}
else {
return array();
}
}
}
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