You are here

protected function HtmlResponseAttachmentsProcessor::processHtmlHeadLink in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php \Drupal\Core\Render\HtmlResponseAttachmentsProcessor::processHtmlHeadLink()
  2. 9 core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php \Drupal\Core\Render\HtmlResponseAttachmentsProcessor::processHtmlHeadLink()

Transform a html_head_link array into html_head and http_header arrays.

Variable html_head_link is a special case of html_head which can be present as a link item in the HTML head section, and also as a Link: HTTP header, depending on options in the render array. Processing it can add to both the html_head and http_header sections.

Parameters

array $html_head_link: The 'html_head_link' value of a render array. Each head link is specified by a two-element array:

  • An array specifying the attributes of the link.
  • A boolean specifying whether the link should also be a Link: HTTP header.

Return value

array An ['#attached'] section of a render array. This allows us to easily merge the results with other render arrays. The array could contain the following keys:

  • http_header
  • html_head

File

core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php, line 427

Class

HtmlResponseAttachmentsProcessor
Processes attachments of HTML responses.

Namespace

Drupal\Core\Render

Code

protected function processHtmlHeadLink(array $html_head_link) {
  $attached = [];
  foreach ($html_head_link as $item) {
    $attributes = $item[0];
    $should_add_header = $item[1] ?? FALSE;
    $element = [
      '#tag' => 'link',
      '#attributes' => $attributes,
    ];
    $href = $attributes['href'];
    $rel = $attributes['rel'];

    // Allow multiple hreflang tags to use the same href.
    if (isset($attributes['hreflang'])) {
      $attached['html_head'][] = [
        $element,
        'html_head_link:' . $rel . ':' . $attributes['hreflang'] . ':' . $href,
      ];
    }
    else {
      $attached['html_head'][] = [
        $element,
        'html_head_link:' . $rel . ':' . $href,
      ];
    }
    if ($should_add_header) {

      // Also add a HTTP header "Link:".
      $href = '<' . Html::escape($attributes['href']) . '>';
      unset($attributes['href']);
      if ($param = static::formatHttpHeaderAttributes($attributes)) {
        $href .= ';' . $param;
      }
      $attached['http_header'][] = [
        'Link',
        $href,
        FALSE,
      ];
    }
  }
  return $attached;
}