class EarlyRenderingTestController

Same name and namespace in other branches
  1. 11.x core/modules/system/tests/modules/early_rendering_controller_test/src/EarlyRenderingTestController.php \Drupal\early_rendering_controller_test\EarlyRenderingTestController

Controller routines for early_rendering_test routes.

The methods on this controller each correspond to a route for this module, each of which exist solely for test cases in EarlyRenderingControllerTest; see that test for documentation.

Hierarchy

Expanded class hierarchy of EarlyRenderingTestController

See also

core/modules/early_rendering_controller_test/early_rendering_controller_test.routing.yml

\Drupal\system\Tests\Common\EarlyRenderingControllerTest::testEarlyRendering()

File

core/modules/system/tests/modules/early_rendering_controller_test/src/EarlyRenderingTestController.php, line 22

Namespace

Drupal\early_rendering_controller_test
View source
class EarlyRenderingTestController extends ControllerBase {
  
  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;
  
  /**
   * Constructs an EarlyRenderingTestController.
   *
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer.
   */
  public function __construct(RendererInterface $renderer) {
    $this->renderer = $renderer;
  }
  
  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container->get('renderer'));
  }
  protected function earlyRenderContent() {
    return [
      '#markup' => 'Hello world!',
      '#cache' => [
        'tags' => [
          'foo',
        ],
      ],
    ];
  }
  public function renderArray() {
    return [
      '#pre_render' => [
        function () {
          $elements = $this->earlyRenderContent();
          return $elements;
        },
      ],
    ];
  }
  public function renderArrayEarly() {
    $render_array = $this->earlyRenderContent();
    return [
      '#markup' => $this->renderer
        ->render($render_array),
    ];
  }
  public function ajaxResponse() {
    $response = new AjaxResponse();
    $response->addCommand(new InsertCommand(NULL, $this->renderArray()));
    return $response;
  }
  public function ajaxResponseEarly() {
    $response = new AjaxResponse();
    $response->addCommand(new InsertCommand(NULL, $this->renderArrayEarly()));
    return $response;
  }
  public function response() {
    return new Response('Hello world!');
  }
  public function responseEarly() {
    $render_array = $this->earlyRenderContent();
    return new Response($this->renderer
      ->render($render_array));
  }
  public function responseWithAttachments() {
    return new AttachmentsTestResponse('Hello world!');
  }
  public function responseWithAttachmentsEarly() {
    $render_array = $this->earlyRenderContent();
    return new AttachmentsTestResponse($this->renderer
      ->render($render_array));
  }
  public function cacheableResponse() {
    return new CacheableTestResponse('Hello world!');
  }
  public function cacheableResponseEarly() {
    $render_array = $this->earlyRenderContent();
    return new CacheableTestResponse($this->renderer
      ->render($render_array));
  }
  public function domainObject() {
    return new TestDomainObject();
  }
  public function domainObjectEarly() {
    $render_array = $this->earlyRenderContent();
    $this->renderer
      ->render($render_array);
    return new TestDomainObject();
  }
  public function domainObjectWithAttachments() {
    return new AttachmentsTestDomainObject();
  }
  public function domainObjectWithAttachmentsEarly() {
    $render_array = $this->earlyRenderContent();
    $this->renderer
      ->render($render_array);
    return new AttachmentsTestDomainObject();
  }
  public function cacheableDomainObject() {
    return new CacheableTestDomainObject();
  }
  public function cacheableDomainObjectEarly() {
    $render_array = $this->earlyRenderContent();
    $this->renderer
      ->render($render_array);
    return new CacheableTestDomainObject();
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.