- π·πΊRussia Chi
People keep struggling with passing contexts. I pushed the example to Cheat Sheet.
Thank you. Automatically closed - issue fixed for 2 weeks with no activity.
This might be overkill for the documentation, but in upgrading to 2.0 I ran into something that was not obvious (to me at least), so wanted to share it here and hopefully save others the time of figuring it out.
If you're using a block plugin that has a required context (in my case, a Table of Contents for a node), you need to supply a context mapping to tell the block instance where to get that context from. Otherwise, you end up with an error like:
Drupal\Component\Plugin\Exception\ContextException: Required contexts without a value: node. in Drupal\Core\Plugin\Context\ContextHandler->applyContextMapping()
Normally this context mapping gets set when you create a block config entity, but if you're using drupal_block()
to render the block that obviously doesn't apply.
I found two ways to make it work.
In your twig template you can use something like:
{{ drupal_block('node_toc', {context_mapping: {node: '@node.node_route_context:node'}}) }}
Or in your block plugin you can include a method like this:
/**
* {@inheritdoc}
*/
public function getContextMapping() {
$mapping = parent::getContextMapping();
// By default, get the node from the URL.
return $mapping ?: ['node' => '@node.node_route_context:node'];
}
Fixed
3.0
Documentation
Not all content is available!
It's likely this issue predates Contrib.social: some issue and comment data are missing.
People keep struggling with passing contexts. I pushed the example to Cheat Sheet.
Thank you.
Automatically closed - issue fixed for 2 weeks with no activity.