You are here

protected function AmpHtmlResponseAttachmentsProcessor::processHtmlHeadLink in Accelerated Mobile Pages (AMP) 8

Same name and namespace in other branches
  1. 8.3 src/Render/AmpHtmlResponseAttachmentsProcessor.php \Drupal\amp\Render\AmpHtmlResponseAttachmentsProcessor::processHtmlHeadLink()
  2. 8.2 src/Render/AmpHtmlResponseAttachmentsProcessor.php \Drupal\amp\Render\AmpHtmlResponseAttachmentsProcessor::processHtmlHeadLink()

Transform a html_head_link array into html_head and http_header arrays.

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
1 call to AmpHtmlResponseAttachmentsProcessor::processHtmlHeadLink()
AmpHtmlResponseAttachmentsProcessor::processAttachments in src/Render/AmpHtmlResponseAttachmentsProcessor.php
Processes the attachments of a response that has attachments.

File

src/Render/AmpHtmlResponseAttachmentsProcessor.php, line 442
Contains \Drupal\amp\Render\AmpHtmlResponseAttachmentsProcessor.

Class

AmpHtmlResponseAttachmentsProcessor
Processes attachments of AMP HTML responses.

Namespace

Drupal\amp\Render

Code

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

      // Also add a HTTP header "Link:".
      $href = '<' . Html::escape($attributes['href']) . '>';
      unset($attributes['href']);
      $attached['http_header'][] = [
        'Link',
        $href . drupal_http_header_attributes($attributes),
        TRUE,
      ];
    }
  }
  return $attached;
}