本文整理汇总了PHP中Drupal\Core\Cache\CacheableMetadata::setCacheContexts方法的典型用法代码示例。如果您正苦于以下问题:PHP CacheableMetadata::setCacheContexts方法的具体用法?PHP CacheableMetadata::setCacheContexts怎么用?PHP CacheableMetadata::setCacheContexts使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Cache\CacheableMetadata
的用法示例。
在下文中一共展示了CacheableMetadata::setCacheContexts方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: onPageContext
/**
* Adds in the current user as a context.
*
* @param \Drupal\page_manager\Event\PageManagerContextEvent $event
* The page entity context event.
*/
public function onPageContext(PageManagerContextEvent $event)
{
$request = $this->requestStack->getCurrentRequest();
$page = $event->getPage();
$routes = $this->routeProvider->getRoutesByPattern($page->getPath())->all();
$route = reset($routes);
if ($route && ($route_contexts = $route->getOption('parameters'))) {
foreach ($route_contexts as $route_context_name => $route_context) {
// Skip this parameter.
if ($route_context_name == 'page_manager_page_variant' || $route_context_name == 'page_manager_page') {
continue;
}
$context_name = $this->t('{@name} from route', ['@name' => $route_context_name]);
if ($request->attributes->has($route_context_name)) {
$value = $request->attributes->get($route_context_name);
} else {
// @todo Find a way to add in a fake value for configuration.
$value = NULL;
}
$cacheability = new CacheableMetadata();
$cacheability->setCacheContexts(['route']);
$context = new Context(new ContextDefinition($route_context['type'], $context_name, FALSE), $value);
$context->addCacheableDependency($cacheability);
$page->addContext($route_context_name, $context);
}
}
}
示例2: getRuntimeContexts
/**
* {@inheritdoc}
*/
public function getRuntimeContexts(array $unqualified_context_ids)
{
// Add a context for each language type.
$language_types = $this->languageManager->getLanguageTypes();
$info = $this->languageManager->getDefinedLanguageTypesInfo();
if ($unqualified_context_ids) {
foreach ($unqualified_context_ids as $unqualified_context_id) {
if (array_search($unqualified_context_id, $language_types) === FALSE) {
unset($language_types[$unqualified_context_id]);
}
}
}
$result = [];
foreach ($language_types as $type_key) {
if (isset($info[$type_key]['name'])) {
$context = new Context(new ContextDefinition('language', $info[$type_key]['name']));
$context->setContextValue($this->languageManager->getCurrentLanguage($type_key));
$cacheability = new CacheableMetadata();
$cacheability->setCacheContexts(['languages:' . $type_key]);
$context->addCacheableDependency($cacheability);
$result[$type_key] = $context;
}
}
return $result;
}
示例3: getCacheableMetadata
/**
* {@inheritdoc}
*/
public function getCacheableMetadata($name)
{
$metadata = new CacheableMetadata();
if ($name === 'block.block.config_override_test') {
$metadata->setCacheContexts(['config_override_integration_test'])->setCacheTags(['config_override_integration_test_tag']);
}
return $metadata;
}
示例4: getRuntimeContexts
/**
* {@inheritdoc}
*/
public function getRuntimeContexts(array $unqualified_context_ids)
{
$current_user = $this->userStorage->load($this->account->id());
$context = new Context(new ContextDefinition('entity:user', $this->t('Current user')), $current_user);
$cacheability = new CacheableMetadata();
$cacheability->setCacheContexts(['user']);
$context->addCacheableDependency($cacheability);
$result = ['current_user' => $context];
return $result;
}
示例5: onPageContext
/**
* Adds in the current user as a context.
*
* @param \Drupal\page_manager\Event\PageManagerContextEvent $event
* The page entity context event.
*/
public function onPageContext(PageManagerContextEvent $event)
{
$id = $this->account->id();
$current_user = $this->userStorage->load($id);
$context = new Context(new ContextDefinition('entity:user', $this->t('Current user')), $current_user);
$cacheability = new CacheableMetadata();
$cacheability->setCacheContexts(['user']);
$context->addCacheableDependency($cacheability);
$event->getPage()->addContext('current_user', $context);
}
示例6: getRuntimeContexts
/**
* {@inheritdoc}
*/
public function getRuntimeContexts(array $unqualified_context_ids)
{
$current_user = $this->userStorage->load($this->account->id());
$context1 = new Context(new ContextDefinition('entity:user', 'User 1'), $current_user);
$context2 = new Context(new ContextDefinition('entity:user', 'User 2'), $current_user);
$cacheability = new CacheableMetadata();
$cacheability->setCacheContexts(['user']);
$context1->addCacheableDependency($cacheability);
$context2->addCacheableDependency($cacheability);
return ['user1' => $context1, 'user2' => $context2];
}
示例7: getRuntimeContexts
/**
* {@inheritdoc}
*/
public function getRuntimeContexts(array $unqualified_context_ids)
{
// Load the current domain.
$current_domain = $this->negotiator->getActiveDomain();
// Set the context.
$context = new Context(new ContextDefinition('entity:domain', $this->t('Active domain')), $current_domain);
// Allow caching.
$cacheability = new CacheableMetadata();
$cacheability->setCacheContexts(['url.site']);
$context->addCacheableDependency($cacheability);
// Prepare the result.
$result = ['current_domain' => $context];
return $result;
}
示例8: getRuntimeContexts
/**
* {@inheritdoc}
*/
public function getRuntimeContexts(array $unqualified_context_ids)
{
$result = [];
$context = new Context(new ContextDefinition('entity:node', NULL, FALSE));
if (($route_object = $this->routeMatch->getRouteObject()) && ($route_contexts = $route_object->getOption('parameters')) && isset($route_contexts['node'])) {
if ($node = $this->routeMatch->getParameter('node')) {
$context->setContextValue($node);
}
} elseif ($this->routeMatch->getRouteName() == 'node.add') {
$node_type = $this->routeMatch->getParameter('node_type');
$context->setContextValue(Node::create(array('type' => $node_type->id())));
}
$cacheability = new CacheableMetadata();
$cacheability->setCacheContexts(['route']);
$context->addCacheableDependency($cacheability);
$result['node'] = $context;
return $result;
}
示例9: getRuntimeContexts
/**
* {@inheritdoc}
*/
public function getRuntimeContexts(array $unqualified_context_ids)
{
$result = [];
$context_definition = new ContextDefinition('entity:rdf_entity:asset_release', NULL, FALSE);
$value = NULL;
if (($route_object = $this->routeMatch->getRouteObject()) && ($route_contexts = $route_object->getOption('parameters')) && isset($route_contexts['rdf_entity'])) {
/** @var \Drupal\rdf_entity\RdfInterface $asset_release */
if ($asset_release = $this->routeMatch->getParameter('rdf_entity')) {
if ($asset_release->bundle() == 'asset_release') {
$value = $asset_release;
}
}
}
$cacheability = new CacheableMetadata();
$cacheability->setCacheContexts(['route']);
$context = new Context($context_definition, $value);
$context->addCacheableDependency($cacheability);
$result['asset_release'] = $context;
return $result;
}
示例10: getRuntimeContexts
/**
* {@inheritdoc}
*/
public function getRuntimeContexts(array $unqualified_context_ids)
{
$result = [];
$context_definition = new ContextDefinition('entity:support_ticket', NULL, FALSE);
$value = NULL;
if (($route_object = $this->routeMatch->getRouteObject()) && ($route_contexts = $route_object->getOption('parameters')) && isset($route_contexts['support_ticket'])) {
if ($support_ticket = $this->routeMatch->getParameter('support_ticket')) {
$value = $support_ticket;
}
} elseif ($this->routeMatch->getRouteName() == 'support_ticket.add') {
$support_ticket_type = $this->routeMatch->getParameter('support_ticket_type');
$value = SupportTicket::create(array('type' => $support_ticket_type->id()));
}
$cacheability = new CacheableMetadata();
$cacheability->setCacheContexts(['route']);
$context = new Context($context_definition, $value);
$context->addCacheableDependency($cacheability);
$result['support_ticket'] = $context;
return $result;
}
示例11: testPathPrefix
/**
* Test path prefix language negotiation and outbound path processing.
*
* @dataProvider providerTestPathPrefix
*/
public function testPathPrefix($prefix, $prefixes, $expected_langcode)
{
$this->languageManager->expects($this->any())->method('getCurrentLanguage')->will($this->returnValue($this->languages[in_array($expected_langcode, ['en', 'de']) ? $expected_langcode : 'en']));
$config = $this->getConfigFactoryStub(['language.negotiation' => ['url' => ['source' => LanguageNegotiationUrl::CONFIG_PATH_PREFIX, 'prefixes' => $prefixes]]]);
$request = Request::create('/' . $prefix . '/foo', 'GET');
$method = new LanguageNegotiationUrl();
$method->setLanguageManager($this->languageManager);
$method->setConfig($config);
$method->setCurrentUser($this->user);
$this->assertEquals($expected_langcode, $method->getLangcode($request));
$cacheability = new CacheableMetadata();
$options = [];
$method->processOutbound('foo', $options, $request, $cacheability);
$expected_cacheability = new CacheableMetadata();
if ($expected_langcode) {
$this->assertSame($prefix . '/', $options['prefix']);
$expected_cacheability->setCacheContexts(['languages:' . LanguageInterface::TYPE_URL]);
} else {
$this->assertFalse(isset($options['prefix']));
}
$this->assertEquals($expected_cacheability, $cacheability);
}
示例12: providerTestCreateFromObject
/**
* Provides test data for createFromObject().
*
* @return array
*/
public function providerTestCreateFromObject()
{
$data = [];
$empty_metadata = new CacheableMetadata();
$nonempty_metadata = new CacheableMetadata();
$nonempty_metadata->setCacheContexts(['qux'])->setCacheTags(['foo:bar'])->setCacheMaxAge(600);
$uncacheable_metadata = new CacheableMetadata();
$uncacheable_metadata->setCacheMaxAge(0);
$empty_cacheable_object = new TestCacheableDependency([], [], Cache::PERMANENT);
$nonempty_cacheable_object = new TestCacheableDependency(['qux'], ['foo:bar'], 600);
$uncacheable_object = new \stdClass();
$data[] = [$empty_cacheable_object, $empty_metadata];
$data[] = [$nonempty_cacheable_object, $nonempty_metadata];
$data[] = [$uncacheable_object, $uncacheable_metadata];
return $data;
}
示例13: getCacheableMetadata
/**
* {@inheritdoc}
*/
public function getCacheableMetadata($name)
{
$metadata = new CacheableMetadata();
$metadata->setCacheContexts(['pirate_day'])->setCacheTags(['pirate-day-tag'])->setCacheMaxAge(PirateDayCacheContext::PIRATE_DAY_MAX_AGE);
return $metadata;
}
示例14: getCacheableMetadata
/**
* {@inheritdoc}
*/
public function getCacheableMetadata($name)
{
$metadata = new CacheableMetadata();
if ($this->language) {
$metadata->setCacheContexts(['languages:language_interface']);
}
return $metadata;
}
示例15: getRuntimeContexts
/**
* {@inheritdoc}
*/
public function getRuntimeContexts(array $unqualified_context_ids)
{
$result = [];
$value = NULL;
if (($route_object = $this->routeMatch->getRouteObject()) && ($route_contexts = $route_object->getOption('parameters')) && isset($route_contexts['rdf_entity'])) {
/** @var \Drupal\rdf_entity\RdfInterface $collection */
if ($collection = $this->routeMatch->getParameter('rdf_entity')) {
if ($collection->bundle() == 'collection') {
$value = $collection;
}
}
} elseif (($route_parameters = $this->routeMatch->getParameters()) && in_array($this->routeMatch->getRouteName(), $this->getSupportedRoutes())) {
foreach ($route_parameters as $route_parameter) {
if ($route_parameter instanceof ContentEntityInterface) {
$bundle = $route_parameter->bundle();
$entity_type = $route_parameter->getEntityTypeId();
// Check if the object is a og content entity.
if (Og::isGroupContent($entity_type, $bundle) && ($groups = $this->membershipManager->getGroupIds($route_parameter, 'rdf_entity', 'collection'))) {
// A content can belong to only one rdf_entity.
// Check that the content is not an orphaned one.
if ($collection_id = reset($groups['rdf_entity'])) {
$collection = Rdf::load($collection_id);
$value = $collection;
}
}
}
}
}
$cacheability = new CacheableMetadata();
$cacheability->setCacheContexts(['route']);
$collection_context_definition = new ContextDefinition('entity', $this->t('Organic group provided by collection'), FALSE);
$context = new Context($collection_context_definition, $value);
$context->addCacheableDependency($cacheability);
$result['og'] = $context;
return $result;
}