-
Notifications
You must be signed in to change notification settings - Fork 16
add a context provider #282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 8.x-1.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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) { | ||
| if ($this->groupManager->isGroup('node', $node->bundle())) { | ||
| $value = $node; | ||
| } | ||
| foreach (array_keys(OgGroupAudienceHelper::getAllGroupAudienceFields('node', $node->bundle(), 'node')) as $field_name) { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps we want this in an |
||
| if ($node->hasField($field_name) && !$node->$field_name->isEmpty()) { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess the |
||
| $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'))); | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| return ['node' => $context]; | ||
| } | ||
|
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Everything before
$node instanceof NodeInterfaceis core code, copy-paste fromNodeRouteContext, and so are the parts including and after$cacheability. Reviewing those are pointless. Speaking of pointless, propably checking whether$node instanceof NodeInterfaceis pointless but it's cheap enough...Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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
Groupentity type.There was a problem hiding this comment.
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?)