You are here

public function WebformBubbleableMetadata::appendTo in Webform 8.5

Same name and namespace in other branches
  1. 6.x src/Cache/WebformBubbleableMetadata.php \Drupal\webform\Cache\WebformBubbleableMetadata::appendTo()

Appends the values of this bubbleable metadata object to a render array.

We can't use \Drupal\Core\Render\BubbleableMetadata::applyTo because it replaces all existing cache contexts and tags.

Parameters

array $build: A render array

See also

\Drupal\Core\Render\BubbleableMetadata::applyTo

\Drupal\webform\WebformSubmissionForm::buildForm

\Drupal\webform\Plugin\WebformElementBase::replaceTokens

File

src/Cache/WebformBubbleableMetadata.php, line 26

Class

WebformBubbleableMetadata
Value object used for bubbleable rendering metadata for webforms.

Namespace

Drupal\webform\Cache

Code

public function appendTo(array &$build) {
  $contexts = $this
    ->getCacheContexts();
  $tags = $this
    ->getCacheTags();
  $max_age = $this
    ->getCacheMaxAge();
  $attachments = $this
    ->getAttachments();

  // Make sure cache metadata has been set.
  if (empty($contexts) && empty($tags) && empty($attachments) && $max_age === Cache::PERMANENT) {
    return;
  }

  // The below code is copied from Renderer::mergeBubbleableMetadata.
  // @see \Drupal\Core\Render\Renderer::mergeBubbleableMetadata
  $meta_a = BubbleableMetadata::createFromRenderArray($build);
  $meta_b = BubbleableMetadata::createFromRenderArray([
    '#cache' => [
      'contexts' => $contexts,
      'tags' => $tags,
      'max-age' => $max_age,
      'attachments' => $attachments,
    ],
  ]);
  $meta_a
    ->merge($meta_b)
    ->applyTo($build);
}