3 namespace Drupal\early_rendering_controller_test;
5 use Drupal\Core\Ajax\AjaxResponse;
6 use Drupal\Core\Ajax\InsertCommand;
7 use Drupal\Core\Controller\ControllerBase;
8 use Drupal\Core\Render\RendererInterface;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10 use Symfony\Component\HttpFoundation\Response;
13 * Controller routines for early_rendering_test routes.
15 * The methods on this controller each correspond to a route for this module,
16 * each of which exist solely for test cases in EarlyRenderingControllerTest;
17 * see that test for documentation.
19 * @see core/modules/early_rendering_controller_test/early_rendering_controller_test.routing.yml
20 * @see \Drupal\system\Tests\Common\EarlyRenderingControllerTest::testEarlyRendering()
22 class EarlyRenderingTestController extends ControllerBase {
27 * @var \Drupal\Core\Render\RendererInterface
32 * Constructs a EarlyRenderingTestController.
34 * @param \Drupal\Core\Render\RendererInterface $renderer
36 public function __construct(RendererInterface $renderer) {
37 $this->renderer = $renderer;
43 public static function create(ContainerInterface $container) {
45 $container->get('renderer')
49 protected function earlyRenderContent() {
51 '#markup' => 'Hello world!',
60 public function renderArray() {
62 '#pre_render' => [function () {
63 $elements = $this->earlyRenderContent();
69 public function renderArrayEarly() {
70 $render_array = $this->earlyRenderContent();
72 '#markup' => $this->renderer->render($render_array),
76 public function ajaxResponse() {
77 $response = new AjaxResponse();
78 $response->addCommand(new InsertCommand(NULL, $this->renderArray()));
82 public function ajaxResponseEarly() {
83 $response = new AjaxResponse();
84 $response->addCommand(new InsertCommand(NULL, $this->renderArrayEarly()));
88 public function response() {
89 return new Response('Hello world!');
92 public function responseEarly() {
93 $render_array = $this->earlyRenderContent();
94 return new Response($this->renderer->render($render_array));
97 public function responseWithAttachments() {
98 return new AttachmentsTestResponse('Hello world!');
101 public function responseWithAttachmentsEarly() {
102 $render_array = $this->earlyRenderContent();
103 return new AttachmentsTestResponse($this->renderer->render($render_array));
106 public function cacheableResponse() {
107 return new CacheableTestResponse('Hello world!');
110 public function cacheableResponseEarly() {
111 $render_array = $this->earlyRenderContent();
112 return new CacheableTestResponse($this->renderer->render($render_array));
115 public function domainObject() {
116 return new TestDomainObject();
119 public function domainObjectEarly() {
120 $render_array = $this->earlyRenderContent();
121 $this->renderer->render($render_array);
122 return new TestDomainObject();
125 public function domainObjectWithAttachments() {
126 return new AttachmentsTestDomainObject();
129 public function domainObjectWithAttachmentsEarly() {
130 $render_array = $this->earlyRenderContent();
131 $this->renderer->render($render_array);
132 return new AttachmentsTestDomainObject();
135 public function cacheableDomainObject() {
136 return new CacheableTestDomainObject();
139 public function cacheableDomainObjectEarly() {
140 $render_array = $this->earlyRenderContent();
141 $this->renderer->render($render_array);
142 return new CacheableTestDomainObject();