You are here

public function FormassemblyHtmlResponseAttachmentsProcessor::processAttachments in FormAssembly 8

Processes the attachments of a response that has attachments.

Libraries, JavaScript settings, feeds, HTML <head> tags, HTML <head> links, HTTP headers, and the HTTP status code are attached to render arrays using the #attached property. The #attached property is an associative array, where the keys are the attachment types and the values are the attached data. For example:

$build['#attached']['library'][] = [
  'library' => [
    'core/jquery',
  ],
];
$build['#attached']['http_header'] = [
  [
    'Content-Type',
    'application/rss+xml; charset=utf-8',
  ],
];

The available keys are:

  • 'library' (asset libraries)
  • 'drupalSettings' (JavaScript settings)
  • 'feed' (RSS feeds)
  • 'html_head' (tags in HTML <head>)
  • 'html_head_link' (<link> tags in HTML <head>)
  • 'http_header' (HTTP headers and status code)

Parameters

\Drupal\Core\Render\AttachmentsInterface $response: The response to process.

Return value

\Drupal\Core\Render\AttachmentsInterface The processed response, with the attachments updated to reflect their final values.

Throws

\InvalidArgumentException Thrown when the $response parameter is not the type of response object the processor expects.

Overrides HtmlResponseAttachmentsProcessor::processAttachments

File

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

Class

FormassemblyHtmlResponseAttachmentsProcessor
Processes attachments of HTML responses with fa_form attachments.

Namespace

Drupal\formassembly\Component\Render

Code

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);
}