/** * {@inheritdoc} */ protected function determineBlockContext() { $current_user = $this->userStorage->load($this->account->id()); $context = new Context(new ContextDefinition('entity:user', $this->t('Current user'))); $context->setContextValue($current_user); $this->addContext('current_user', $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 onBlockActiveContext(BlockContextEvent $event) { $current_user = $this->userStorage->load($this->account->id()); $context = new Context(new ContextDefinition('entity:user', $this->t('Current user'))); $context->setContextValue($current_user); $event->setContext('user.current_user', $context); }
/** * 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'))); $context->setContextValue($current_user); $event->getPageExecutable()->addContext('current_user', $context); }
/** * {@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) { $current_user = $this->userStorage->load($this->account->id()); $context = new Context(new ContextDefinition('entity:user', $this->t('Current user'))); $context->setContextValue($current_user); $cacheability = new CacheableMetadata(); $cacheability->setCacheContexts(['user']); $context->addCacheableDependency($cacheability); $result = ['current_user' => $context]; return $result; }
/** * {@inheritdoc} */ public function onBlockActiveContext(BlockContextEvent $event) { // Add a context for each language type. $language_types = $this->languageManager->getLanguageTypes(); $info = $this->languageManager->getDefinedLanguageTypesInfo(); 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)); $event->setContext('language.' . $type_key, $context); } } }
/** * {@inheritdoc} */ protected function determineBlockContext() { if (($route_object = $this->routeMatch->getRouteObject()) && ($route_contexts = $route_object->getOption('parameters')) && isset($route_contexts['node'])) { $context = new Context(new ContextDefinition($route_contexts['node']['type'])); if ($node = $this->routeMatch->getParameter('node')) { $context->setContextValue($node); } $this->addContext('node', $context); } elseif ($this->routeMatch->getRouteName() == 'node.add') { $node_type = $this->routeMatch->getParameter('node_type'); $context = new Context(new ContextDefinition('entity:node')); $context->setContextValue(Node::create(array('type' => $node_type->id()))); $this->addContext('node', $context); } }
/** * 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(); $executable = $event->getPageExecutable(); $routes = $this->routeProvider->getRoutesByPattern($executable->getPage()->getPath())->all(); $route = reset($routes); if ($route_contexts = $route->getOption('parameters')) { foreach ($route_contexts as $route_context_name => $route_context) { // Skip this parameter. if ($route_context_name == 'page_manager_page') { continue; } $context_name = $this->t('{@name} from route', ['@name' => $route_context_name]); $context = new Context(new ContextDefinition($route_context['type'], $context_name)); if ($request->attributes->has($route_context_name)) { $context->setContextValue($request->attributes->get($route_context_name)); } else { // @todo Find a way to add in a fake value for configuration. } $executable->addContext($route_context_name, $context); } } }
/** * {@inheritdoc} */ protected function determineBlockContext() { $context = new Context(new ContextDefinition('language', $this->t('Current language'))); $context->setContextValue($this->languageManager->getCurrentLanguage()); $this->addContext('language', $context); }
/** * @covers ::setContextValue */ public function testSetContextValueCacheableDependency() { $container = new Container(); $cache_context_manager = $this->getMockBuilder('Drupal\\Core\\Cache\\CacheContextsManager')->disableOriginalConstructor()->getMock(); $container->set('cache_contexts_manager', $cache_context_manager); $cache_context_manager->expects($this->any())->method('validateTokens')->with(['route'])->willReturn(['route']); \Drupal::setContainer($container); $this->contextDefinition = $this->getMock('Drupal\\Core\\Plugin\\Context\\ContextDefinitionInterface'); $context = new Context($this->contextDefinition); $context->setTypedDataManager($this->typedDataManager); $cacheable_dependency = $this->getMock('Drupal\\Tests\\Core\\Plugin\\Context\\TypedDataCacheableDependencyInterface'); $cacheable_dependency->expects($this->once())->method('getCacheTags')->willReturn(['node:1']); $cacheable_dependency->expects($this->once())->method('getCacheContexts')->willReturn(['route']); $cacheable_dependency->expects($this->once())->method('getCacheMaxAge')->willReturn(60); $context->setContextValue($cacheable_dependency); $this->assertSame($cacheable_dependency, $context->getContextData()); $this->assertEquals(['node:1'], $context->getCacheTags()); $this->assertEquals(['route'], $context->getCacheContexts()); $this->assertEquals(60, $context->getCacheMaxAge()); }