Issue #3303874 by alecsmrekar, Wim Leers, smustgrave: Early rendering issue in big_pipe_page_attachments() for controllers returning Response objects

(cherry picked from commit d6d5051fb9)
merge-requests/3618/head
catch 2023-03-08 12:02:16 +00:00
parent 0217ee0ae4
commit 1bca2d9b64
5 changed files with 49 additions and 1 deletions

View File

@ -281,7 +281,9 @@ class HtmlRenderer implements MainContentRendererInterface {
}
// Allow hooks to add attachments to $page['#attached'].
$this->invokePageAttachmentHooks($page);
$this->renderer->executeInRenderContext(new RenderContext(), function () use (&$page) {
$this->invokePageAttachmentHooks($page);
});
return [$page, $title];
}

View File

@ -217,6 +217,12 @@ function common_test_page_attachments(array &$page) {
'#markup' => 'test',
];
}
if (\Drupal::state()->get('common_test.hook_page_attachments.early_rendering', FALSE)) {
// Do some early rendering.
$element = ['#markup' => '123'];
\Drupal::service('renderer')->render($element);
}
}
/**

View File

@ -20,3 +20,11 @@ common_test.js_and_css_querystring:
_controller: '\Drupal\common_test\Controller\CommonTestController::jsAndCssQuerystring'
requirements:
_access: 'TRUE'
common_test.page_attachments:
path: '/common/attachments-test'
defaults:
_title: 'Attachments test'
_controller: '\Drupal\common_test\Controller\CommonTestController::attachments'
requirements:
_access: 'TRUE'

View File

@ -92,4 +92,20 @@ class CommonTestController {
return new Response($output);
}
/**
* Returns a sample response with some early rendering in
* common_test_page_attachments.
*
* @return \Symfony\Component\HttpFoundation\Response
* A new Response object.
*/
public function attachments() {
\Drupal::state()->set('common_test.hook_page_attachments.early_rendering', TRUE);
$build = [
'#title' => 'A title',
'content' => ['#markup' => 'Some content'],
];
return \Drupal::service('main_content_renderer.html')->renderResponse($build, \Drupal::requestStack()->getCurrentRequest(), \Drupal::routeMatch());
}
}

View File

@ -3,6 +3,8 @@
namespace Drupal\Tests\system\Kernel\Common;
use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
/**
* Test page rendering hooks.
@ -77,4 +79,18 @@ class PageRenderTest extends KernelTestBase {
\Drupal::state()->set($module . '.' . $hook . '.render_array', FALSE);
}
/**
* Assert that HtmlRenderer::invokePageAttachmentHooks is called in a render
* context.
*/
public function testHtmlRendererAttachmentsRenderContext(): void {
$this->enableModules(['common_test', 'system']);
\Drupal::state()->set('common_test.hook_page_attachments.render_url', TRUE);
$uri = '/common/attachments-test';
$request = new Request([], [], [], [], [], ['REQUEST_URI' => $uri, 'SCRIPT_NAME' => $uri]);
$response = \Drupal::service('http_kernel')->handle($request);
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
}
}