I created some blocks programmatically that use a context in their annotation to retrieve the current node:

 *   context = {
 *     "node" = @ContextDefinition(
 *       "entity:node",
 *       label = @Translation("Current Node")
 *     )
 *   }

I then access it like so:

  public function build() {

    $node = $this->getContextValue('node');

Having them placed in a theme's region causes no error. Having them placed inside a Block Group triggers the following fatal on all non-nodes (block ID is 'quicknav'), for example on /user/login:

The website encountered an unexpected error. Please try again later.</br></br><em class="placeholder">Drupal\Component\Plugin\Exception\ContextException</em>: Required contexts without a value: node. in <em class="placeholder">Drupal\Core\Plugin\Context\ContextHandler-&gt;applyContextMapping()</em> (line <em class="placeholder">112</em> of <em class="placeholder">core/lib/Drupal/Core/Plugin/Context/ContextHandler.php</em>). <pre class="backtrace">Drupal\block\BlockViewBuilder::buildPreRenderableBlock(Object, Object) (Line: 190)
Drupal\block\BlockViewBuilder::lazyBuilder(&#039;quicknav&#039;, &#039;full&#039;, NULL)
call_user_func_array(&#039;Drupal\block\BlockViewBuilder::lazyBuilder&#039;, Array) (Line: 358)
Drupal\Core\Render\Renderer-&gt;doRender(Array) (Line: 450)
Drupal\Core\Render\Renderer-&gt;doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer-&gt;render(Array) (Line: 90)
Drupal\blockgroup\Plugin\Block\BlockGroup-&gt;build() (Line: 203)
Drupal\block\BlockViewBuilder::preRender(Array)
call_user_func(&#039;Drupal\block\BlockViewBuilder::preRender&#039;, Array) (Line: 378)
Drupal\Core\Render\Renderer-&gt;doRender(Array) (Line: 450)
Drupal\Core\Render\Renderer-&gt;doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer-&gt;render(Array) (Line: 490)
Drupal\Core\Template\TwigExtension-&gt;escapeFilter(Object, Array, &#039;html&#039;, NULL, 1) (Line: 55)
__TwigTemplate_8509f64d3425b305f4dc8bb74dd6076fd34d970d30d7c5fd8080ea23b38be2dd-&gt;doDisplay(Array, Array) (Line: 432)
Twig_Template-&gt;displayWithErrorHandling(Array, Array) (Line: 403)
Twig_Template-&gt;display(Array) (Line: 411)
Twig_Template-&gt;render(Array) (Line: 64)
twig_render_template(&#039;themes/contrib/neato/templates/base/page.html.twig&#039;, Array) (Line: 384)
Drupal\Core\Theme\ThemeManager-&gt;render(&#039;page&#039;, Array) (Line: 437)
Drupal\Core\Render\Renderer-&gt;doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer-&gt;render(Array) (Line: 490)
Drupal\Core\Template\TwigExtension-&gt;escapeFilter(Object, Array, &#039;html&#039;, NULL, 1) (Line: 101)
__TwigTemplate_a272349a35001468c338ca41d6f9ae26cb6bc72bd30bd493adb79fe2c0fddc47-&gt;doDisplay(Array, Array) (Line: 432)
Twig_Template-&gt;displayWithErrorHandling(Array, Array) (Line: 403)
Twig_Template-&gt;display(Array) (Line: 411)
Twig_Template-&gt;render(Array) (Line: 64)
twig_render_template(&#039;themes/contrib/neato/templates/base/html.html.twig&#039;, Array) (Line: 384)
Drupal\Core\Theme\ThemeManager-&gt;render(&#039;html&#039;, Array) (Line: 437)
Drupal\Core\Render\Renderer-&gt;doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer-&gt;render(Array) (Line: 147)
Drupal\Core\Render\MainContent\HtmlRenderer-&gt;Drupal\Core\Render\MainContent\{closure}() (Line: 576)
Drupal\Core\Render\Renderer-&gt;executeInRenderContext(Object, Object) (Line: 148)
Drupal\Core\Render\MainContent\HtmlRenderer-&gt;renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber-&gt;onViewRenderArray(Object, &#039;kernel.view&#039;, Object) (Line: 76)
Drupal\webprofiler\EventDispatcher\TraceableEventDispatcher-&gt;dispatch(&#039;kernel.view&#039;, Object) (Line: 158)
Symfony\Component\HttpKernel\HttpKernel-&gt;handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel-&gt;handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session-&gt;handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle-&gt;handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware-&gt;handle(Object, 1, 1) (Line: 38)
Drupal\webprofiler\StackMiddleware\WebprofilerMiddleware-&gt;handle(Object, 1, 1) (Line: 50)
Drupal\Core\StackMiddleware\NegotiationMiddleware-&gt;handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel-&gt;handle(Object, 1, 1) (Line: 657)
Drupal\Core\DrupalKernel-&gt;handle(Object) (Line: 19)
</pre>

I can workaround this by restricting the block group visibility to certain node types through the block UI.

Is there a way programmatically to make the block group aware of that context and pass it along?

Comments

leymannx created an issue. See original summary.

leymannx’s picture

Issue summary: View changes
leymannx’s picture

Issue summary: View changes
leymannx’s picture

Sorry, my mistake. I simply had to un-require the context of the blocks I've put into the field group.

 *   context = {
 *     "node" = @ContextDefinition(
 *       "entity:node",
 *       label = @Translation("Current Node"),
 *       required = FALSE,
 *     )
 *   }
leymannx’s picture

Status: Active » Closed (works as designed)