本文整理汇总了PHP中Drupal\Core\Render\BubbleableMetadata::setCacheMaxAge方法的典型用法代码示例。如果您正苦于以下问题:PHP BubbleableMetadata::setCacheMaxAge方法的具体用法?PHP BubbleableMetadata::setCacheMaxAge怎么用?PHP BubbleableMetadata::setCacheMaxAge使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Render\BubbleableMetadata
的用法示例。
在下文中一共展示了BubbleableMetadata::setCacheMaxAge方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testReplaceWithHookTokensAlterWithBubbleableMetadata
/**
* @covers ::replace
* @covers ::replace
*/
public function testReplaceWithHookTokensAlterWithBubbleableMetadata()
{
$this->moduleHandler->expects($this->any())->method('invokeAll')->willReturn([]);
$this->moduleHandler->expects($this->any())->method('alter')->willReturnCallback(function ($hook_name, array &$replacements, array $context, BubbleableMetadata $bubbleable_metadata) {
$replacements['[node:title]'] = 'hello world';
$bubbleable_metadata->addCacheContexts(['custom_context']);
$bubbleable_metadata->addCacheTags(['node:1']);
$bubbleable_metadata->setCacheMaxAge(10);
});
$node = $this->prophesize('Drupal\\node\\NodeInterface');
$node->getCacheContexts()->willReturn([]);
$node->getCacheTags()->willReturn([]);
$node->getCacheMaxAge()->willReturn(14);
$node = $node->reveal();
$bubbleable_metadata = new BubbleableMetadata();
$bubbleable_metadata->setCacheContexts(['current_user']);
$bubbleable_metadata->setCacheMaxAge(12);
$result = $this->token->replace('[node:title]', ['node' => $node], [], $bubbleable_metadata);
$this->assertEquals('hello world', $result);
$this->assertEquals(['node:1'], $bubbleable_metadata->getCacheTags());
$this->assertEquals(['current_user', 'custom_context'], $bubbleable_metadata->getCacheContexts());
$this->assertEquals(10, $bubbleable_metadata->getCacheMaxAge());
}
示例2: testDomain
/**
* Test domain language negotiation and outbound path processing.
*
* @dataProvider providerTestDomain
*/
public function testDomain($http_host, $domains, $expected_langcode)
{
$this->languageManager->expects($this->any())->method('getCurrentLanguage')->will($this->returnValue($this->languages['en']));
$config = $this->getConfigFactoryStub(['language.negotiation' => ['url' => ['source' => LanguageNegotiationUrl::CONFIG_DOMAIN, 'domains' => $domains]]]);
$request = Request::create('', 'GET', array(), array(), array(), array('HTTP_HOST' => $http_host));
$method = new LanguageNegotiationUrl();
$method->setLanguageManager($this->languageManager);
$method->setConfig($config);
$method->setCurrentUser($this->user);
$this->assertEquals($expected_langcode, $method->getLangcode($request));
$cacheability = new BubbleableMetadata();
$options = [];
$this->assertSame('foo', $method->processOutbound('foo', $options, $request, $cacheability));
$expected_cacheability = new BubbleableMetadata();
if ($expected_langcode !== FALSE && count($domains) > 1) {
$expected_cacheability->setCacheMaxAge(Cache::PERMANENT)->setCacheContexts(['languages:' . LanguageInterface::TYPE_URL, 'url.site']);
}
$this->assertEquals($expected_cacheability, $cacheability);
}