- Issue created by @erik.erskine
- Status changed to Needs review
10 months ago 7:16pm 27 May 2024
We have a use case for making a context-aware @Mgv
plugin, like the following, which shows the title of the "current" node.
/**
* @Mgv(
* id = "current_node_title",
* context_definitions = {
* "node" = @ContextDefinition("entity:node", label = @Translation("Node"), required = FALSE),
* },
* );
*/
class CurrentNodeTitle extends GlobalVariable implements ContextAwarePluginInterface {
public function getValue(): string {
$node = $this->getContextValue('node');
...
}
// MGV plugins aren't configurable, so we hardcode the mapping here.
// In this case we always want the "Node from URL" context value.
public function getContextMapping() {
return [
'node' => '@node.node_route_context:node',
];
}
}
At the moment this is impossible because MgvPluginManager
does not anticipate @Mgv
plugins being context-aware, and doesn't pass the necessary values to them.
Try something like the above. $node
will always be NULL
.
Add the following to MgvPluginManager::getVariables()
: (based on what BlockViewBuilder
does for block plugins)
if ($variable instanceof ContextAwarePluginInterface) {
$contexts = \Drupal::service('context.repository')->getRuntimeContexts($variable->getContextMapping());
\Drupal::service('context.handler')->applyContextMapping($variable, $contexts);
}
Patch, test, get feedback.
None.
None. Existing plugins are unaffected.
None.
Needs review
2.2
Code