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->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->getPlugin(Array) (Line: 69)
Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent->__construct(Object, Array, 1) (Line: 89)
Drupal\layout_builder\SectionComponent->toRenderArray(Array, 1) (Line: 86)
Drupal\layout_builder\Section->toRenderArray(Array, 1) (Line: 247)
Drupal\layout_builder\Element\LayoutBuilder->buildAdministrativeSection(Object, 0) (Line: 123)
Drupal\layout_builder\Element\LayoutBuilder->layout(Object) (Line: 97)
Drupal\layout_builder\Element\LayoutBuilder->preRender(Array)
call_user_func(Array, Array) (Line: 378)
Drupal\Core\Render\Renderer->doRender(Array) (Line: 450)
Drupal\Core\Render\Renderer->doRender(Array, ) (Line: 195)
Drupal\Core\Render\Renderer->render(Array, ) (Line: 226)
Drupal\Core\Render\MainContent\HtmlRenderer->Drupal\Core\Render\MainContent\{closure}() (Line: 582)
Drupal\Core\Render\Renderer->executeInRenderContext(Object, Object) (Line: 227)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 117)
Drupal\Core\Render\MainContent\HtmlRenderer->renderResponse(Array, Object, Object) (Line: 90)
Drupal\Core\EventSubscriber\MainContentViewSubscriber->onViewRenderArray(Object, 'kernel.view', Object)
call_user_func(Array, Object, 'kernel.view', Object) (Line: 111)
Drupal\Component\EventDispatcher\ContainerAwareEventDispatcher->dispatch('kernel.view', Object) (Line: 156)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 68)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 57)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 106)
Drupal\page_cache\StackMiddleware\PageCache->pass(Object, 1, 1) (Line: 85)
Drupal\page_cache\StackMiddleware\PageCache->handle(Object, 1, 1) (Line: 47)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 52)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 23)
Stack\StackedHttpKernel->handle(Object, 1, 1) (Line: 693)
Drupal\Core\DrupalKernel->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.