function CacheableMetadata::createFromObject

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

Creates a CacheableMetadata object from a depended object.

Parameters

\Drupal\Core\Cache\CacheableDependencyInterface|mixed $object: The object whose cacheability metadata to retrieve. If it implements CacheableDependencyInterface, its cacheability metadata will be used, otherwise, the passed in object must be assumed to be uncacheable, so max-age 0 is set.

Return value

static

21 calls to CacheableMetadata::createFromObject()
BlockRepository::getVisibleBlocksPerRegion in core/modules/block/src/BlockRepository.php
Returns an array of regions and their block entities.
BubbleableMetadata::createFromObject in core/lib/Drupal/Core/Render/BubbleableMetadata.php
Creates a bubbleable metadata object from a depended object.
CacheableMetadataTest::testCreateFromObject in core/tests/Drupal/Tests/Core/Cache/CacheableMetadataTest.php
@covers ::createFromObject @dataProvider providerTestCreateFromObject
CacheableResponseTrait::addCacheableDependency in core/lib/Drupal/Core/Cache/CacheableResponseTrait.php
ConfigEntityStorage::doLoadMultiple in core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php
Performs storage-specific loading of entities.

... See full list

1 method overrides CacheableMetadata::createFromObject()
BubbleableMetadata::createFromObject in core/lib/Drupal/Core/Render/BubbleableMetadata.php
Creates a bubbleable metadata object from a depended object.

File

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

Class

CacheableMetadata
Defines a generic class for passing cacheability metadata.

Namespace

Drupal\Core\Cache

Code

public static function createFromObject($object) {
    if ($object instanceof CacheableDependencyInterface) {
        $meta = new static();
        $meta->cacheContexts = $object->getCacheContexts();
        $meta->cacheTags = $object->getCacheTags();
        $meta->cacheMaxAge = $object->getCacheMaxAge();
        return $meta;
    }
    // Objects that don't implement CacheableDependencyInterface must be assumed
    // to be uncacheable, so set max-age 0.
    $meta = new static();
    $meta->cacheMaxAge = 0;
    return $meta;
}

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