diff --git a/og.services.yml b/og.services.yml index af2aea97b..a04303416 100644 --- a/og.services.yml +++ b/og.services.yml @@ -22,3 +22,8 @@ services: plugin.manager.og.fields: class: Drupal\og\OgFieldsPluginManager parent: default_plugin_manager + og.node_route_context: + class: Drupal\og\ContextProvider\OgRouteContext + arguments: ['@current_route_match', '@og.group.manager'] + tags: + - { name: 'context_provider' } diff --git a/src/ContextProvider/OgRouteContext.php b/src/ContextProvider/OgRouteContext.php new file mode 100755 index 000000000..518dc5691 --- /dev/null +++ b/src/ContextProvider/OgRouteContext.php @@ -0,0 +1,80 @@ +routeMatch = $route_match; + $this->groupManager = $group_manager; + } + + /** + * @inheritDoc + */ + public function getRuntimeContexts(array $unqualified_context_ids) { + $result = []; + $context_definition = new ContextDefinition('entity:node', NULL, FALSE); + $value = NULL; + if (($route_object = $this->routeMatch->getRouteObject()) && ($route_contexts = $route_object->getOption('parameters')) && isset($route_contexts['node'])) { + if (($node = $this->routeMatch->getParameter('node')) && $node instanceof NodeInterface) { + if ($this->groupManager->isGroup('node', $node->bundle())) { + $value = $node; + } + foreach (array_keys(OgGroupAudienceHelper::getAllGroupAudienceFields('node', $node->bundle(), 'node')) as $field_name) { + if ($node->hasField($field_name) && !$node->$field_name->isEmpty()) { + $value = $node->$field_name->entity; + break; + } + } + } + } + $cacheability = new CacheableMetadata(); + $cacheability->setCacheContexts(['route']); + + $context = new Context($context_definition, $value); + $context->addCacheableDependency($cacheability); + $result['node'] = $context; + + return $result; + } + + /** + * @inheritDoc + */ + public function getAvailableContexts() { + $context = new Context(new ContextDefinition('entity:node', $this->t('(Containing) group from URL'))); + return ['node' => $context]; + } + +}