White screen error when using a bundle-specific computed field in layout builder

Created on 26 September 2019, almost 6 years ago
Updated 19 August 2025, 5 days ago

I added a computed field to a specific node bundle like this:

/**
 * Implements hook_entity_field_storage_info().
 */
function ilr_entity_field_storage_info(EntityTypeInterface $entity_type) {
  $definitions = [];

  if ($entity_type->id() == 'node') {
    $definitions['classes'] = FieldStorageDefinition::create('entity_reference')
      ->setName('classes')
      ->setLabel(t('Classes'))
      ->setRevisionable(TRUE)
      ->setTargetEntityTypeId($entity_type->id());
  }

  return $definitions;
}

/**
 * Implements hook_entity_bundle_field_info().
 */
function ilr_entity_bundle_field_info(EntityTypeInterface $entity_type, $bundle) {
  if ($entity_type->id() == 'node' && $bundle == 'course') {
    $fields = [];

    $custom_field_storage = ilr_entity_field_storage_info($entity_type);

    $fields[$custom_field_storage['classes']->getName()] = FieldDefinition::createFromFieldStorageDefinition($custom_field_storage['classes']);

    $fields[$custom_field_storage['classes']->getName()]
      ->setLabel(t('Classes'))
      ->setComputed(TRUE)
      ->setClass('\Drupal\ilr\CourseClassItemList')
      ->setSettings([
        'handler' => 'default:node',
        'handler_settings' => [
          'target_bundles' => [
            'class' => 'class'
          ]
        ],
      ])
      ->setDisplayConfigurable('view', TRUE)
      ->setDisplayOptions('view', [
        'label' => 'hidden',
        'weight' => -5,
      ]);

    return $fields;
  }
}

Now, when I enable layout builder for this content type and click Manage layout, I get the following error:

The website encountered an unexpected error. Please try again later.</br></br><em class="placeholder">Drupal\Component\Plugin\Exception\ContextException</em>: Assigned contexts were not satisfied: entity in <em class="placeholder">Drupal\Core\Plugin\Context\ContextHandler-&gt;applyContextMapping()</em> (line <em class="placeholder">150</em> of <em class="placeholder">core/lib/Drupal/Core/Plugin/Context/ContextHandler.php</em>). <pre class="backtrace">Drupal\layout_builder\SectionComponent-&gt;getPlugin(Array) (Line: 69)
Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent-&gt;__construct(Object, Array, 1) (Line: 89)
Drupal\layout_builder\SectionComponent-&gt;toRenderArray(Array, 1) (Line: 86)
Drupal\layout_builder\Section-&gt;toRenderArray(Array, 1) (Line: 247)
Drupal\layout_builder\Element\LayoutBuilder-&gt;buildAdministrativeSection(Object, 0) (Line: 123)
Drupal\layout_builder\Element\LayoutBuilder-&gt;layout(Object) (Line: 97)
Drupal\layout_builder\Element\LayoutBuilder-&gt;preRender(Array)
call_user_func(Array, 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: 226)
Drupal\Core\Render\MainContent\HtmlRenderer-&gt;Drupal\Core\Render\MainContent\{closure}() (Line: 582)
Drupal\Core\Render\Renderer-&gt;executeInRenderContext(Object, Object) (Line: 227)
Drupal\Core\Render\MainContent\HtmlRenderer-&gt;prepare(Array, Object, Object) (Line: 117)
Drupal\Core\Render\MainContent\HtmlRenderer-&gt;renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber-&gt;onViewRenderArray(Object, &#039;kernel.view&#039;, Object)
call_user_func(Array, Object, &#039;kernel.view&#039;, Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher-&gt;dispatch(&#039;kernel.view&#039;, Object) (Line: 156)
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: 106)
Drupal\page_cache\StackMiddleware\PageCache-&gt;pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache-&gt;handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware-&gt;handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware-&gt;handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel-&gt;handle(Object, 1, 1) (Line: 693)
Drupal\Core\DrupalKernel-&gt;handle(Object) (Line: 19)
</pre>

I tried to debug the error in core/lib/Drupal/Core/Plugin/Context/ContextHandler.php and could determine that the error was triggered by the Drupal\Core\Block\Plugin\Block\Broken plugin with the configuration.id of field_block:node:course:classes, which makes me think that the issue is with my computed, bundle-specific classes field.

πŸ’¬ Support request
Status

Closed: won't fix

Version

11.0 πŸ”₯

Component

layout_builder.module

Created by

πŸ‡ΊπŸ‡ΈUnited States jeffam

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡³πŸ‡ΏNew Zealand danielveza Brisbane, AU

    This has been open for 6+ years with no update, and it's indicated in #3 that it may be a site specific issue.

    I'm going to mark this as closed. Feel free to reopen if you find a valid bug around this. Thanks!

Production build 0.71.5 2024