Commit 5738f39c authored by catch's avatar catch

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

parent 0bfb32b5
......@@ -79,6 +79,13 @@ public function ajaxView(Request $request) {
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 @@ public function ajaxView(Request $request) {
// 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 {
......
......@@ -166,6 +166,27 @@ public function testAjaxViewWithArguments() {
$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.
*/
......
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