Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions og.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
80 changes: 80 additions & 0 deletions src/ContextProvider/OgRouteContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace Drupal\og\ContextProvider;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\Context\ContextProviderInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\node\NodeInterface;
use Drupal\og\GroupManager;
use Drupal\og\OgGroupAudienceHelper;

class OgRouteContext implements ContextProviderInterface {

use StringTranslationTrait;

/**
* The route match object.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
protected $routeMatch;

/**
* @var \Drupal\og\GroupManager
*/
protected $groupManager;

/**
* Constructs a new NodeRouteContext.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match object.
*/
public function __construct(RouteMatchInterface $route_match, GroupManager $group_manager) {
$this->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) {

@chx chx Aug 1, 2016

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything before $node instanceof NodeInterface is core code, copy-paste from NodeRouteContext, and so are the parts including and after $cacheability. Reviewing those are pointless. Speaking of pointless, propably checking whether $node instanceof NodeInterface is pointless but it's cheap enough...

@pfrenssen pfrenssen Aug 4, 2016

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would you limit this to nodes? To me the idea of using a node as a group type is odd. I know it was common to do it like this in D7, but to me it makes more sense to use a custom Group entity type.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are a D7 port and this PR needs to reuse #252 anyways which unhardwires node. I will probably do it during Drupalaton (are you coming?)

if ($this->groupManager->isGroup('node', $node->bundle())) {
$value = $node;
}
foreach (array_keys(OgGroupAudienceHelper::getAllGroupAudienceFields('node', $node->bundle(), 'node')) as $field_name) {

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we want this in an else { } block.

if ($node->hasField($field_name) && !$node->$field_name->isEmpty()) {

@chx chx Aug 1, 2016

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the hasField is not necessary? I was a bit unsure.

$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')));

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$this->t('(Containing) group from URL') This is the only non-core piece of code in this method.

return ['node' => $context];
}

}