You are here

class AmpHtmlResponseAttachmentsProcessor in Accelerated Mobile Pages (AMP) 8.2

Same name and namespace in other branches
  1. 8.3 src/Render/AmpHtmlResponseAttachmentsProcessor.php \Drupal\amp\Render\AmpHtmlResponseAttachmentsProcessor
  2. 8 src/Render/AmpHtmlResponseAttachmentsProcessor.php \Drupal\amp\Render\AmpHtmlResponseAttachmentsProcessor

Processes attachments of AMP HTML responses.

This class is used by the rendering service to process the #attached part of the render array, for AMP HTML responses.

To render attachments to HTML for testing without a controller, use the 'bare_html_page_renderer' service to generate a Drupal\Core\Render\HtmlResponse object. Then use its getContent(), getStatusCode(), and/or the headers property to access the result.

Hierarchy

Expanded class hierarchy of AmpHtmlResponseAttachmentsProcessor

See also

template_preprocess_html()

\Drupal\Core\Render\AttachmentsResponseProcessorInterface

\Drupal\Core\Render\BareHtmlPageRenderer

\Drupal\Core\Render\HtmlResponse

\Drupal\Core\Render\MainContent\HtmlRenderer

1 string reference to 'AmpHtmlResponseAttachmentsProcessor'
amp.services.yml in ./amp.services.yml
amp.services.yml
1 service uses AmpHtmlResponseAttachmentsProcessor
html_response.attachments_processor in ./amp.services.yml
Drupal\amp\Render\AmpHtmlResponseAttachmentsProcessor

File

src/Render/AmpHtmlResponseAttachmentsProcessor.php, line 32

Namespace

Drupal\amp\Render
View source
class AmpHtmlResponseAttachmentsProcessor extends HtmlResponseAttachmentsProcessor {

  /**
   * The route amp context to determine whether a route is an amp one.
   *
   * @var \Drupal\amp\Routing\AmpContext
   */
  protected $ampContext;

  /**
   * Constructs a HtmlResponseAttachmentsProcessor object.
   *
   * @param \Drupal\Core\Asset\AssetResolverInterface $asset_resolver
   *   An asset resolver.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   A config factory for retrieving required config objects.
   * @param \Drupal\Core\Asset\AssetCollectionRendererInterface $css_collection_renderer
   *   The CSS asset collection renderer.
   * @param \Drupal\Core\Asset\AssetCollectionRendererInterface $js_collection_renderer
   *   The JS asset collection renderer.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler service.
   * @param \Drupal\amp\Routing\AmpContext $amp_context
   *   The route amp context to determine whether the route is an amp one.
   */
  public function __construct(AssetResolverInterface $asset_resolver, ConfigFactoryInterface $config_factory, AssetCollectionRendererInterface $css_collection_renderer, AssetCollectionRendererInterface $js_collection_renderer, RequestStack $request_stack, RendererInterface $renderer, ModuleHandlerInterface $module_handler, AmpContext $amp_context) {
    parent::__construct($asset_resolver, $config_factory, $css_collection_renderer, $js_collection_renderer, $request_stack, $renderer, $module_handler);
    $this->ampContext = $amp_context;
  }

  /**
   * Processes asset libraries into render arrays.
   *
   * @param \Drupal\Core\Asset\AttachedAssetsInterface $assets
   *   The attached assets collection for the current response.
   * @param array $placeholders
   *   The placeholders that exist in the response.
   *
   * @return array
   *   An array keyed by asset type, with keys:
   *     - styles
   *     - scripts
   *     - scripts_bottom
   */
  protected function processAssetLibraries(AttachedAssetsInterface $assets, array $placeholders) {
    $variables = [];
    if ($this->ampContext
      ->isAmpRoute()) {
      foreach ($assets->libraries as $delta => $library) {
        if (strpos($library, 'amp/') === FALSE) {
          unset($assets->libraries[$delta]);
        }
      }

      // Print amp scripts - if any are present.
      if (isset($placeholders['scripts']) || isset($placeholders['scripts_bottom'])) {

        // Do not optimize JS.
        $optimize_js = FALSE;
        list($js_assets_header, $js_assets_footer) = $this->assetResolver
          ->getJsAssets($assets, $optimize_js);
        $variables['scripts'] = $this->jsCollectionRenderer
          ->render($js_assets_header);
      }
    }
    else {
      $variables = parent::processAssetLibraries($assets, $placeholders);
    }
    return $variables;
  }

  /**
   * 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.
   *
   * @param 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 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
   */
  protected function processHtmlHeadLink(array $html_head_link) {
    $attached = parent::processHtmlHeadLink($html_head_link);
    if (array_key_exists('http_header', $attached)) {

      // Find the amphtml link and flag it to be displayed as a HTTP header.
      foreach ($attached['http_header'] as $key => $value) {
        if (strpos($value[1], 'rel="amphtml"') !== FALSE) {
          $new_value = str_replace(';', '', $value[1]);
          $attached['http_header'][$key] = [
            'Link',
            $new_value,
            TRUE,
          ];
        }
      }
    }
    return $attached;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AmpHtmlResponseAttachmentsProcessor::$ampContext protected property The route amp context to determine whether a route is an amp one.
AmpHtmlResponseAttachmentsProcessor::processAssetLibraries protected function Processes asset libraries into render arrays. Overrides HtmlResponseAttachmentsProcessor::processAssetLibraries
AmpHtmlResponseAttachmentsProcessor::processHtmlHeadLink protected function Transform a html_head_link array into html_head and http_header arrays. Overrides HtmlResponseAttachmentsProcessor::processHtmlHeadLink
AmpHtmlResponseAttachmentsProcessor::__construct public function Constructs a HtmlResponseAttachmentsProcessor object. Overrides HtmlResponseAttachmentsProcessor::__construct
HtmlResponseAttachmentsProcessor::$assetResolver protected property The asset resolver service.
HtmlResponseAttachmentsProcessor::$config protected property A config object for the system performance configuration.
HtmlResponseAttachmentsProcessor::$cssCollectionRenderer protected property The CSS asset collection renderer service.
HtmlResponseAttachmentsProcessor::$jsCollectionRenderer protected property The JS asset collection renderer service.
HtmlResponseAttachmentsProcessor::$moduleHandler protected property The module handler service.
HtmlResponseAttachmentsProcessor::$renderer protected property The renderer.
HtmlResponseAttachmentsProcessor::$requestStack protected property The request stack.
HtmlResponseAttachmentsProcessor::formatHttpHeaderAttributes public static function Formats an attribute string for an HTTP header.
HtmlResponseAttachmentsProcessor::processAttachments public function Processes the attachments of a response that has attachments. Overrides AttachmentsResponseProcessorInterface::processAttachments 1
HtmlResponseAttachmentsProcessor::processFeed protected function Transform a 'feed' attachment into an 'html_head_link' attachment.
HtmlResponseAttachmentsProcessor::processHtmlHead protected function Ensure proper key/data order and defaults for renderable head items.
HtmlResponseAttachmentsProcessor::renderHtmlResponseAttachmentPlaceholders protected function Renders HTML response attachment placeholders.
HtmlResponseAttachmentsProcessor::renderPlaceholders protected function Renders placeholders (#attached['placeholders']).
HtmlResponseAttachmentsProcessor::setHeaders protected function Sets headers on a response object.