function Cache::mergeTags

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

Merges arrays of cache tags and removes duplicates.

The cache tags array is returned in a format that is valid for \Drupal\Core\Cache\CacheBackendInterface::set().

When caching elements, it is necessary to collect all cache tags into a single array, from both the element itself and all child elements. This allows items to be invalidated based on all tags attached to the content they're constituted from.

Parameters

array $a: Cache tags array to merge.

array $b: Cache tags array to merge.

Return value

string[] The merged array of cache tags.

35 calls to Cache::mergeTags()
AggregatorFeedBlock::getCacheTags in core/modules/aggregator/src/Plugin/Block/AggregatorFeedBlock.php
The cache tags associated with this object.
AssertPageCacheContextsAndTagsTrait::assertCacheTags in core/modules/system/src/Tests/Cache/AssertPageCacheContextsAndTagsTrait.php
Ensures that some cache tags are present in the current response.
BlockContentHalJsonAnonTest::getExpectedCacheContexts in core/modules/block_content/tests/src/Functional/Hal/BlockContentHalJsonAnonTest.php
The expected cache contexts for the GET/HEAD response of the test entity.
BlockViewBuilder::viewMultiple in core/modules/block/src/BlockViewBuilder.php
Builds the render array for the provided entities.
BlockViewBuilderTest::assertBlockRenderedWithExpectedCacheability in core/modules/block/tests/src/Kernel/BlockViewBuilderTest.php
Asserts that a block is built/rendered/cached with expected cacheability.

... See full list

File

core/lib/Drupal/Core/Cache/Cache.php, line 57

Class

Cache
Helper methods for cache.

Namespace

Drupal\Core\Cache

Code

public static function mergeTags(array $a = [], array $b = []) {
  assert(Inspector::assertAllStrings($a) && Inspector::assertAllStrings($b), 'Cache tags must be valid strings');
  $cache_tags = array_unique(array_merge($a, $b));
  sort($cache_tags);
  return $cache_tags;
}

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