Example #1
0
 /**
  * {@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;
 }
 /**
  * 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);
         }
     }
 }
Example #3
0
 /**
  * {@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);
 }
Example #5
0
 /**
  * {@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];
 }
Example #6
0
 /**
  * {@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;
 }
Example #7
0
 /**
  * {@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;
 }
 /**
  * {@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;
 }