diff --git a/core/modules/views/src/Controller/ViewAjaxController.php b/core/modules/views/src/Controller/ViewAjaxController.php index 840d1f243bf..401b0f96f1a 100644 --- a/core/modules/views/src/Controller/ViewAjaxController.php +++ b/core/modules/views/src/Controller/ViewAjaxController.php @@ -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 { diff --git a/core/modules/views/tests/src/Unit/Controller/ViewAjaxControllerTest.php b/core/modules/views/tests/src/Unit/Controller/ViewAjaxControllerTest.php index 8d0a3130f10..996ac1a40f9 100644 --- a/core/modules/views/tests/src/Unit/Controller/ViewAjaxControllerTest.php +++ b/core/modules/views/tests/src/Unit/Controller/ViewAjaxControllerTest.php @@ -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. */