Commit 1bca2d9b authored by catch's avatar catch
Browse files

Issue #3303874 by alecsmrekar, Wim Leers, smustgrave: Early rendering issue in...

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

(cherry picked from commit d6d5051f)
parent 0217ee0a
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -281,7 +281,9 @@ protected function prepare(array $main_content, Request $request, RouteMatchInte
    }

    // Allow hooks to add attachments to $page['#attached'].
    $this->renderer->executeInRenderContext(new RenderContext(), function () use (&$page) {
      $this->invokePageAttachmentHooks($page);
    });

    return [$page, $title];
  }
+6 −0
Original line number Diff line number Diff line
@@ -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);
  }
}

/**
+8 −0
Original line number Diff line number Diff line
@@ -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'
+16 −0
Original line number Diff line number Diff line
@@ -92,4 +92,20 @@ public function destination() {
    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());
  }

}
+16 −0
Original line number Diff line number Diff line
@@ -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 @@ public function assertPageRenderHookExceptions(string $module, string $hook): vo
    \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());
  }

}