Issue #2358603 by Berdir: ViewsAjaxController results in fatal error for empty optional arguments

8.0.x
Nathaniel Catchpole 2014-11-26 10:39:50 +00:00
parent 0bfb32b55b
commit 5738f39ccd
2 changed files with 31 additions and 2 deletions

View File

@ -79,6 +79,13 @@ class ViewAjaxController implements ContainerInjectionInterface {
if (isset($name) && isset($display_id)) {
$args = $request->request->get('view_args');
$args = isset($args) && $args !== '' ? explode('/', $args) : array();
// Arguments can be empty, make sure they are passed on as NULL so that
// argument validation is not triggered.
$args = array_map(function ($arg) {
return ($arg == '' ? NULL : $arg);
}, $args);
$path = $request->request->get('view_path');
$dom_id = $request->request->get('view_dom_id');
$dom_id = isset($dom_id) ? preg_replace('/[^a-zA-Z0-9_-]+/', '-', $dom_id) : NULL;
@ -130,8 +137,9 @@ class ViewAjaxController implements ContainerInjectionInterface {
// Reuse the same DOM id so it matches that in drupalSettings.
$view->dom_id = $dom_id;
$preview = $view->preview($display_id, $args);
$response->addCommand(new ReplaceCommand(".view-dom-id-$dom_id", $this->drupalRender($preview)));
if ($preview = $view->preview($display_id, $args)) {
$response->addCommand(new ReplaceCommand(".view-dom-id-$dom_id", $this->drupalRender($preview)));
}
return $response;
}
else {

View File

@ -166,6 +166,27 @@ class ViewAjaxControllerTest extends UnitTestCase {
$this->assertViewResultCommand($response);
}
/**
* Tests a valid view with arguments.
*/
public function testAjaxViewWithEmptyArguments() {
$request = new Request();
$request->request->set('view_name', 'test_view');
$request->request->set('view_display_id', 'page_1');
// Simulate a request that has a second, empty argument.
$request->request->set('view_args', 'arg1/');
list($view, $executable) = $this->setupValidMocks();
$executable->expects($this->once())
->method('preview')
->with('page_1', $this->identicalTo(array('arg1', NULL)));
$response = $this->viewAjaxController->ajaxView($request);
$this->assertTrue($response instanceof ViewAjaxResponse);
$this->assertViewResultCommand($response);
}
/**
* Tests a valid view with a pager.
*/