You are here

class FormassemblyHtmlResponseAttachmentsProcessor in FormAssembly 8

Processes attachments of HTML responses with fa_form attachments.

@author Shawn P. Duncan <code@sd.shawnduncan.org>

Copyright 2019 by Shawn P. Duncan. This code is released under the GNU General Public License. Which means that it is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. http://www.gnu.org/licenses/gpl.html

Hierarchy

Expanded class hierarchy of FormassemblyHtmlResponseAttachmentsProcessor

See also

\Drupal\Core\Render\HtmlResponseAttachmentsProcessor

\Drupal\formassembly\Entity\FormAssemblyEntityViewBuilder

1 file declares its use of FormassemblyHtmlResponseAttachmentsProcessor
FormassemblyHtmlResponseAttachmentsProcessorTest.php in tests/src/Unit/FormassemblyHtmlResponseAttachmentsProcessorTest.php
1 string reference to 'FormassemblyHtmlResponseAttachmentsProcessor'
formassembly.services.yml in ./formassembly.services.yml
formassembly.services.yml
1 service uses FormassemblyHtmlResponseAttachmentsProcessor
formassembly.html_response.attachments_processor in ./formassembly.services.yml
\Drupal\formassembly\Component\Render\FormassemblyHtmlResponseAttachmentsProcessor

File

src/Component/Render/FormassemblyHtmlResponseAttachmentsProcessor.php, line 33

Namespace

Drupal\formassembly\Component\Render
View source
class FormassemblyHtmlResponseAttachmentsProcessor extends HtmlResponseAttachmentsProcessor {

  /**
   * The HTML response attachments processor service.
   *
   * @var \Drupal\Core\Render\AttachmentsResponseProcessorInterface
   */
  protected $htmlResponseAttachmentsProcessor;

  /**
   * The configuration factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Constructs a DfpResponseAttachmentsProcessor object.
   *
   * @param \Drupal\Core\Render\AttachmentsResponseProcessorInterface $html_response_attachments_processor
   *   The HTML response attachments processor service.
   * @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.
   */
  public function __construct(AttachmentsResponseProcessorInterface $html_response_attachments_processor, AssetResolverInterface $asset_resolver, ConfigFactoryInterface $config_factory, AssetCollectionRendererInterface $css_collection_renderer, AssetCollectionRendererInterface $js_collection_renderer, RequestStack $request_stack, RendererInterface $renderer, ModuleHandlerInterface $module_handler) {
    $this->htmlResponseAttachmentsProcessor = $html_response_attachments_processor;
    $this->configFactory = $config_factory;
    parent::__construct($asset_resolver, $config_factory, $css_collection_renderer, $js_collection_renderer, $request_stack, $renderer, $module_handler);
  }

  /**
   * {@inheritdoc}
   */
  public function processAttachments(AttachmentsInterface $response) {
    assert($response instanceof HtmlResponse, new \InvalidArgumentException('\\Drupal\\Core\\Render\\HtmlResponse instance expected.'));

    // First, render the actual placeholders. This may add attachments so this
    // is a bit of unfortunate but necessary duplication.
    // This is copied verbatim from
    // \Drupal\Core\Render\HtmlResponseAttachmentsProcessor::processAttachments.
    try {
      $response = $this
        ->renderPlaceholders($response);
    } catch (EnforcedResponseException $e) {
      return $e
        ->getResponse();
    }

    // Extract fa_form_attachments--HtmlResponseAttachmentsProcessor does not
    // know (nor need to know) how to process those.
    $attachments = $response
      ->getAttachments();
    if (isset($attachments['fa_form_attachments'])) {
      uasort($attachments['fa_form_attachments'], '\\Drupal\\Component\\Utility\\SortArray::sortByWeightProperty');
      foreach ($attachments['fa_form_attachments'] as $attachment) {
        switch ($attachment['#type']) {
          case 'fa_form_external_js':
            $attachments['html_head'][] = [
              [
                // Use a fake #type to prevent
                // HtmlResponseAttachmentsProcessor::processHead() adding one.
                '#type' => 'fa_form_external_js',
                '#theme' => 'fa_form__external_js',
                '#src' => $attachment['#src'],
              ],
              'fa-form-attachment-' . $attachment['#weight'],
            ];
            break;
          case 'fa_form_inline_js':
            $attachments['html_head'][] = [
              [
                // Use a fake #type to prevent
                // HtmlResponseAttachmentsProcessor::processHead() adding one.
                '#type' => 'fa_form_inline_js',
                '#theme' => 'fa_form__inline_js',
                '#value' => $attachment['#value'],
              ],
              'fa-form-attachment-' . $attachment['#weight'],
            ];
            break;
          case 'fa_form_external_css':
            $attachments['html_head'][] = [
              [
                // Use a fake #type to prevent
                // HtmlResponseAttachmentsProcessor::processHead() adding one.
                '#type' => 'fa_form_external_css',
                '#theme' => 'fa_form__external_css',
                '#rel' => $attachment['#rel'],
                '#href' => $attachment['#href'],
              ],
              'fa-form-attachment-' . $attachment['#weight'],
            ];
            break;
        }
      }
      unset($attachments['fa_form_attachments']);
    }
    $response
      ->setAttachments($attachments);

    // Call HtmlResponseAttachmentsProcessor to process all other attachments.
    return $this->htmlResponseAttachmentsProcessor
      ->processAttachments($response);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FormassemblyHtmlResponseAttachmentsProcessor::$configFactory protected property The configuration factory.
FormassemblyHtmlResponseAttachmentsProcessor::$htmlResponseAttachmentsProcessor protected property The HTML response attachments processor service.
FormassemblyHtmlResponseAttachmentsProcessor::processAttachments public function Processes the attachments of a response that has attachments. Overrides HtmlResponseAttachmentsProcessor::processAttachments
FormassemblyHtmlResponseAttachmentsProcessor::__construct public function Constructs a DfpResponseAttachmentsProcessor 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::processAssetLibraries protected function Processes asset libraries into render arrays.
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::processHtmlHeadLink protected function Transform a html_head_link array into html_head and http_header arrays.
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.