function CacheableMetadata::createFromRenderArray

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

Creates a CacheableMetadata object with values taken from a render array.

Parameters

array $build: A render array.

Return value

static

20 calls to CacheableMetadata::createFromRenderArray()
BlockComponentRenderArray::onBuildRender in core/modules/layout_builder/src/EventSubscriber/BlockComponentRenderArray.php
Builds render arrays for block plugins and sets it on the event.
BlockPageVariant::build in core/modules/block/src/Plugin/DisplayVariant/BlockPageVariant.php
CacheableMetadataTest::testCreateFromRenderArray in core/tests/Drupal/Tests/Core/Cache/CacheableMetadataTest.php
@covers ::createFromRenderArray[[api-linebreak]] @dataProvider providerTestCreateFromRenderArray
content_translation_page_attachments in core/modules/content_translation/content_translation.module
Implements hook_page_attachments().
EntityReferenceFormatterTest::testLabelFormatter in core/modules/field/tests/src/Kernel/EntityReference/EntityReferenceFormatterTest.php
Tests the label formatter.

... See full list

File

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

Class

CacheableMetadata
Defines a generic class for passing cacheability metadata.

Namespace

Drupal\Core\Cache

Code

public static function createFromRenderArray(array $build) {
  $meta = new static();
  $meta->cacheContexts = isset($build['#cache']['contexts']) ? $build['#cache']['contexts'] : [];
  $meta->cacheTags = isset($build['#cache']['tags']) ? $build['#cache']['tags'] : [];
  $meta->cacheMaxAge = isset($build['#cache']['max-age']) ? $build['#cache']['max-age'] : Cache::PERMANENT;
  return $meta;
}

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