You are here

class RenderAttachedTestController in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/system/tests/modules/render_attached_test/src/Controller/RenderAttachedTestController.php \Drupal\render_attached_test\Controller\RenderAttachedTestController

Controller for various permutations of #attached in the render array.

Hierarchy

Expanded class hierarchy of RenderAttachedTestController

1 file declares its use of RenderAttachedTestController
AttachedRenderingBlock.php in core/modules/system/tests/modules/render_attached_test/src/Plugin/Block/AttachedRenderingBlock.php

File

core/modules/system/tests/modules/render_attached_test/src/Controller/RenderAttachedTestController.php, line 8

Namespace

Drupal\render_attached_test\Controller
View source
class RenderAttachedTestController {

  /**
   * Tests special header and status code rendering.
   *
   * @return array
   *   A render array using features of the 'http_header' directive.
   */
  public function teapotHeaderStatus() {
    $render = [];
    $render['#attached']['http_header'][] = [
      'Status',
      418,
    ];
    return $render;
  }

  /**
   * Tests attached HTML head rendering.
   *
   * @return array
   *   A render array using the 'http_head' directive.
   */
  public function header() {
    $render = [];
    $render['#attached']['http_header'][] = [
      'X-Test-Teapot-Replace',
      'This value gets replaced',
    ];
    $render['#attached']['http_header'][] = [
      'X-Test-Teapot-Replace',
      'Teapot replaced',
      TRUE,
    ];
    $render['#attached']['http_header'][] = [
      'X-Test-Teapot-No-Replace',
      'This value is not replaced',
    ];
    $render['#attached']['http_header'][] = [
      'X-Test-Teapot-No-Replace',
      'This one is added',
      FALSE,
    ];
    $render['#attached']['http_header'][] = [
      'X-Test-Teapot',
      'Teapot Mode Active',
    ];
    return $render;
  }

  /**
   * Tests attached HTML head rendering.
   *
   * @return array
   *   A render array using the 'html_head' directive.
   */
  public function head() {
    $head = [
      [
        '#tag' => 'meta',
        '#attributes' => [
          'test-attribute' => 'testvalue',
        ],
      ],
      'test_head_attribute',
    ];
    $render = [];
    $render['#attached']['html_head'][] = $head;
    return $render;
  }

  /**
   * Tests attached feed rendering.
   *
   * @return array
   *   A render array using the 'feed' directive.
   */
  public function feed() {
    $render = [];
    $render['#attached']['feed'][] = [
      'test://url',
      'Your RSS feed.',
    ];
    return $render;
  }

  /**
   * Tests HTTP header rendering for link.
   *
   * @return array
   *   A render array using the 'html_head_link' directive.
   */
  public function htmlHeaderLink() {
    $render = [];
    $render['#attached']['html_head_link'][] = [
      [
        'href' => '/foo?bar=<baz>&baz=false',
        'rel' => 'alternate',
      ],
      TRUE,
    ];
    $render['#attached']['html_head_link'][] = [
      [
        'href' => '/not-added-to-http-headers',
        'rel' => 'alternate',
      ],
      FALSE,
    ];
    $render['#attached']['html_head_link'][] = [
      [
        'href' => '/foo/bar',
        'hreflang' => 'nl',
        'rel' => 'alternate',
      ],
      TRUE,
    ];
    return $render;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RenderAttachedTestController::feed public function Tests attached feed rendering.
RenderAttachedTestController::head public function Tests attached HTML head rendering.
RenderAttachedTestController::header public function Tests attached HTML head rendering.
RenderAttachedTestController::htmlHeaderLink public function Tests HTTP header rendering for link.
RenderAttachedTestController::teapotHeaderStatus public function Tests special header and status code rendering.