You are here

class WebformBubbleableMetadata in Webform 8.5

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

Value object used for bubbleable rendering metadata for webforms.

Hierarchy

Expanded class hierarchy of WebformBubbleableMetadata

3 files declare their use of WebformBubbleableMetadata
WebformBubbleableMetadataTest.php in tests/src/Unit/Cache/WebformBubbleableMetadataTest.php
WebformElementBase.php in src/Plugin/WebformElementBase.php
WebformSubmissionForm.php in src/WebformSubmissionForm.php

File

src/Cache/WebformBubbleableMetadata.php, line 11

Namespace

Drupal\webform\Cache
View source
class WebformBubbleableMetadata extends BubbleableMetadata {

  /**
   * 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.
   *
   * @param array $build
   *   A render array
   *
   * @see \Drupal\Core\Render\BubbleableMetadata::applyTo
   * @see \Drupal\webform\WebformSubmissionForm::buildForm
   * @see \Drupal\webform\Plugin\WebformElementBase::replaceTokens
   */
  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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AttachmentsTrait::$attachments protected property The attachments for this response.
AttachmentsTrait::addAttachments public function
AttachmentsTrait::getAttachments public function
AttachmentsTrait::setAttachments public function
BubbleableMetadata::addCacheableDependency public function Overrides RefinableCacheableDependencyTrait::addCacheableDependency
BubbleableMetadata::applyTo public function Applies the values of this bubbleable metadata object to a render array. Overrides CacheableMetadata::applyTo
BubbleableMetadata::createFromObject public static function Creates a bubbleable metadata object from a depended object. Overrides CacheableMetadata::createFromObject
BubbleableMetadata::createFromRenderArray public static function Creates a bubbleable metadata object with values taken from a render array. Overrides CacheableMetadata::createFromRenderArray
BubbleableMetadata::merge public function Creates a new bubbleable metadata object by merging this one with another. Overrides CacheableMetadata::merge
BubbleableMetadata::mergeAttachments public static function Merges two attachments arrays (which live under the '#attached' key).
CacheableDependencyTrait::$cacheContexts protected property Cache contexts.
CacheableDependencyTrait::$cacheMaxAge protected property Cache max-age.
CacheableDependencyTrait::$cacheTags protected property Cache tags.
CacheableDependencyTrait::setCacheability protected function Sets cacheability; useful for value object constructors.
CacheableMetadata::getCacheContexts public function The cache contexts associated with this object. Overrides CacheableDependencyTrait::getCacheContexts
CacheableMetadata::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides CacheableDependencyTrait::getCacheMaxAge
CacheableMetadata::getCacheTags public function The cache tags associated with this object. Overrides CacheableDependencyTrait::getCacheTags
CacheableMetadata::setCacheContexts public function Sets cache contexts.
CacheableMetadata::setCacheMaxAge public function Sets the maximum age (in seconds).
CacheableMetadata::setCacheTags public function Sets cache tags.
RefinableCacheableDependencyTrait::addCacheContexts public function
RefinableCacheableDependencyTrait::addCacheTags public function
RefinableCacheableDependencyTrait::mergeCacheMaxAge public function
WebformBubbleableMetadata::appendTo public function Appends the values of this bubbleable metadata object to a render array.