Loading core/lib/Drupal/Core/Render/MainContent/AjaxRenderer.php +1 −1 Original line number Diff line number Diff line Loading @@ -49,7 +49,7 @@ public function renderResponse(array $main_content, Request $request, RouteMatch $response = new AjaxResponse(); $html = $this->renderer->renderRoot($main_content); $response->setAttachments($main_content['#attached']); $response->setAttachments($main_content['#attached'] ?? []); // The selector for the insert command is NULL as the new content will // replace the element making the Ajax call. The default 'replaceWith' Loading core/tests/Drupal/Tests/Core/Controller/AjaxRendererTest.php +34 −3 Original line number Diff line number Diff line Loading @@ -41,9 +41,8 @@ protected function setUp(): void { parent::setUp(); $element_info_manager = $this->createStub(ElementInfoManagerInterface::class); $renderer = $this->createMock(RendererInterface::class); $renderer->expects($this->atLeastOnce()) ->method('renderRoot') $renderer = $this->createStub(RendererInterface::class); $renderer->method('renderRoot') ->willReturnCallback(function (array &$elements, $is_root_call = FALSE) { $elements += ['#attached' => []]; if (isset($elements['#markup'])) { Loading @@ -60,6 +59,38 @@ protected function setUp(): void { $this->ajaxRenderer = new AjaxRenderer($element_info_manager, $renderer); } /** * Tests renderResponse() when the render array has no #attached key. */ public function testRenderWithoutAttached(): void { $element_info_manager = $this->createStub(ElementInfoManagerInterface::class); $renderer = $this->createStub(RendererInterface::class); $renderer->method('renderRoot') ->willReturnCallback(function (array &$elements, $is_root_call = FALSE) { if (isset($elements['#markup'])) { return $elements['#markup']; } elseif (isset($elements['#type'])) { return $elements['#type']; } else { return 'Markup'; } }); $ajax_renderer = new AjaxRenderer($element_info_manager, $renderer); $main_content = ['#markup' => 'example content']; $request = new Request(); $route_match = $this->createStub(RouteMatchInterface::class); $result = $ajax_renderer->renderResponse($main_content, $request, $route_match); $this->assertInstanceOf('Drupal\Core\Ajax\AjaxResponse', $result); $commands = $result->getCommands(); $this->assertEquals('insert', $commands[0]['command']); $this->assertEquals('example content', $commands[0]['data']); } /** * Tests the content method. * Loading Loading
core/lib/Drupal/Core/Render/MainContent/AjaxRenderer.php +1 −1 Original line number Diff line number Diff line Loading @@ -49,7 +49,7 @@ public function renderResponse(array $main_content, Request $request, RouteMatch $response = new AjaxResponse(); $html = $this->renderer->renderRoot($main_content); $response->setAttachments($main_content['#attached']); $response->setAttachments($main_content['#attached'] ?? []); // The selector for the insert command is NULL as the new content will // replace the element making the Ajax call. The default 'replaceWith' Loading
core/tests/Drupal/Tests/Core/Controller/AjaxRendererTest.php +34 −3 Original line number Diff line number Diff line Loading @@ -41,9 +41,8 @@ protected function setUp(): void { parent::setUp(); $element_info_manager = $this->createStub(ElementInfoManagerInterface::class); $renderer = $this->createMock(RendererInterface::class); $renderer->expects($this->atLeastOnce()) ->method('renderRoot') $renderer = $this->createStub(RendererInterface::class); $renderer->method('renderRoot') ->willReturnCallback(function (array &$elements, $is_root_call = FALSE) { $elements += ['#attached' => []]; if (isset($elements['#markup'])) { Loading @@ -60,6 +59,38 @@ protected function setUp(): void { $this->ajaxRenderer = new AjaxRenderer($element_info_manager, $renderer); } /** * Tests renderResponse() when the render array has no #attached key. */ public function testRenderWithoutAttached(): void { $element_info_manager = $this->createStub(ElementInfoManagerInterface::class); $renderer = $this->createStub(RendererInterface::class); $renderer->method('renderRoot') ->willReturnCallback(function (array &$elements, $is_root_call = FALSE) { if (isset($elements['#markup'])) { return $elements['#markup']; } elseif (isset($elements['#type'])) { return $elements['#type']; } else { return 'Markup'; } }); $ajax_renderer = new AjaxRenderer($element_info_manager, $renderer); $main_content = ['#markup' => 'example content']; $request = new Request(); $route_match = $this->createStub(RouteMatchInterface::class); $result = $ajax_renderer->renderResponse($main_content, $request, $route_match); $this->assertInstanceOf('Drupal\Core\Ajax\AjaxResponse', $result); $commands = $result->getCommands(); $this->assertEquals('insert', $commands[0]['command']); $this->assertEquals('example content', $commands[0]['data']); } /** * Tests the content method. * Loading