function CacheableMetadata::merge

Same name in other branches
  1. 9 core/lib/Drupal/Core/Cache/CacheableMetadata.php \Drupal\Core\Cache\CacheableMetadata::merge()
  2. 10 core/lib/Drupal/Core/Cache/CacheableMetadata.php \Drupal\Core\Cache\CacheableMetadata::merge()
  3. 11.x core/lib/Drupal/Core/Cache/CacheableMetadata.php \Drupal\Core\Cache\CacheableMetadata::merge()

Merges the values of another CacheableMetadata object with this one.

Parameters

\Drupal\Core\Cache\CacheableMetadata $other: The other CacheableMetadata object.

Return value

static A new CacheableMetadata object, with the merged data.

1 call to CacheableMetadata::merge()
BubbleableMetadata::merge in core/lib/Drupal/Core/Render/BubbleableMetadata.php
Creates a new bubbleable metadata object by merging this one with another.
1 method overrides CacheableMetadata::merge()
BubbleableMetadata::merge in core/lib/Drupal/Core/Render/BubbleableMetadata.php
Creates a new bubbleable metadata object by merging this one with another.

File

core/lib/Drupal/Core/Cache/CacheableMetadata.php, line 92

Class

CacheableMetadata
Defines a generic class for passing cacheability metadata.

Namespace

Drupal\Core\Cache

Code

public function merge(CacheableMetadata $other) {
    $result = clone $this;
    // This is called many times per request, so avoid merging unless absolutely
    // necessary.
    if (empty($this->cacheContexts)) {
        $result->cacheContexts = $other->cacheContexts;
    }
    elseif (empty($other->cacheContexts)) {
        $result->cacheContexts = $this->cacheContexts;
    }
    else {
        $result->cacheContexts = Cache::mergeContexts($this->cacheContexts, $other->cacheContexts);
    }
    if (empty($this->cacheTags)) {
        $result->cacheTags = $other->cacheTags;
    }
    elseif (empty($other->cacheTags)) {
        $result->cacheTags = $this->cacheTags;
    }
    else {
        $result->cacheTags = Cache::mergeTags($this->cacheTags, $other->cacheTags);
    }
    if ($this->cacheMaxAge === Cache::PERMANENT) {
        $result->cacheMaxAge = $other->cacheMaxAge;
    }
    elseif ($other->cacheMaxAge === Cache::PERMANENT) {
        $result->cacheMaxAge = $this->cacheMaxAge;
    }
    else {
        $result->cacheMaxAge = Cache::mergeMaxAges($this->cacheMaxAge, $other->cacheMaxAge);
    }
    return $result;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.