function HtmlResponseAttachmentsProcessor::processHtmlHeadLink

Same name in other branches
  1. 9 core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php \Drupal\Core\Render\HtmlResponseAttachmentsProcessor::processHtmlHeadLink()
  2. 8.9.x core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php \Drupal\Core\Render\HtmlResponseAttachmentsProcessor::processHtmlHeadLink()
  3. 10 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. The 'href' and 'rel' attributes are required, and the 'href' attribute is expected to be a percent-encoded URI for proper serialization in the Link: HTTP header, as specified by RFC 8288.
  • 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
1 call to HtmlResponseAttachmentsProcessor::processHtmlHeadLink()
HtmlResponseAttachmentsProcessor::processAttachments in core/lib/Drupal/Core/Render/HtmlResponseAttachmentsProcessor.php
Processes the attachments of a response that has attachments.

File

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

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 = '<' . $attributes['href'] . '>';
            unset($attributes['href']);
            if ($param = static::formatHttpHeaderAttributes($attributes)) {
                $href .= ';' . $param;
            }
            $attached['http_header'][] = [
                'Link',
                $href,
                FALSE,
            ];
        }
    }
    return $attached;
}

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