/** * 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); } } }
/** * {@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; }
/** * {@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; }
/** * {@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; }
/** * 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); }
/** * {@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]; }
/** * {@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; }
/** * {@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; }
/** * {@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; }
/** * {@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; }
/** * 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); }
/** * 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; }
/** * {@inheritdoc} */ public function getCacheableMetadata($name) { $metadata = new CacheableMetadata(); $metadata->setCacheContexts(['pirate_day'])->setCacheTags(['pirate-day-tag'])->setCacheMaxAge(PirateDayCacheContext::PIRATE_DAY_MAX_AGE); return $metadata; }
/** * {@inheritdoc} */ public function getCacheableMetadata($name) { $metadata = new CacheableMetadata(); if ($this->language) { $metadata->setCacheContexts(['languages:language_interface']); } return $metadata; }
/** * {@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; }