Errors when displaying Entity Reference field as a block in block layout

Created on 16 January 2025, 7 days ago

Problem/Motivation

I have recently started using this field as a lightweight replacement for paragraphs, when I need a collection of fields with values that go together. When I placed a field that had an entity reference field in it through block layout. I began receiving the following error:

The website encountered an unexpected error. Try again later.

LogicException: Render context is empty, because render() was called outside of a renderRoot() or renderInIsolation() call. Use renderInIsolation()/renderRoot() or #lazy_builder/#pre_render instead. in Drupal\Core\Render\Renderer->doRender() (line 303 of core/lib/Drupal/Core/Render/Renderer.php).
Drupal\Core\Render\Renderer->render(Array) (Line: 100)
Drupal\custom_field\Plugin\CustomField\FieldFormatter\EntityReferenceLabelFormatter->formatValue(Object, Object, Array) (Line: 334)
Drupal\custom_field\Plugin\Field\FieldFormatter\BaseFormatter->getFormattedValues(Object, 'en') (Line: 35)
Drupal\custom_field\Plugin\Field\FieldFormatter\CustomFormatter->viewValue(Object, 'en') (Line: 227)
Drupal\custom_field\Plugin\Field\FieldFormatter\BaseFormatter->viewElements(Object, 'en') (Line: 91)
Drupal\Core\Field\FormatterBase->view(Object, 'en') (Line: 268)
Drupal\Core\Entity\Entity\EntityViewDisplay->buildMultiple(Array) (Line: 282)
Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay->buildMultiple(Array) (Line: 226)
Drupal\Core\Entity\Entity\EntityViewDisplay->build(Object) (Line: 461)
Drupal\Core\Entity\EntityViewBuilder->viewField(Object, Array) (Line: 243)
Drupal\Core\Field\FieldItemList->view(Array) (Line: 174)
Drupal\ctools_block\Plugin\Block\EntityField->blockAccess(Object) (Line: 127)
Drupal\Core\Block\BlockBase->access(Object, 1) (Line: 124)
Drupal\block\BlockAccessControlHandler->checkAccess(Object, 'view', Object) (Line: 109)
Drupal\Core\Entity\EntityAccessControlHandler->access(Object, 'view', Object, 1) (Line: 329)
Drupal\Core\Entity\EntityBase->access('view', NULL, 1) (Line: 63)
Drupal\block\BlockRepository->getVisibleBlocksPerRegion(Array) (Line: 138)
Drupal\block\Plugin\DisplayVariant\BlockPageVariant->build() (Line: 270)
Drupal\Core\Render\MainContent\HtmlRenderer->prepare(Array, Object, Object) (Line: 128)
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(Object, 'kernel.view') (Line: 186)
Symfony\Component\HttpKernel\HttpKernel->handleRaw(Object, 1) (Line: 76)
Symfony\Component\HttpKernel\HttpKernel->handle(Object, 1, 1) (Line: 53)
Drupal\Core\StackMiddleware\Session->handle(Object, 1, 1) (Line: 48)
Drupal\Core\StackMiddleware\KernelPreHandle->handle(Object, 1, 1) (Line: 28)
Drupal\Core\StackMiddleware\ContentLength->handle(Object, 1, 1) (Line: 32)
Drupal\big_pipe\StackMiddleware\ContentLength->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: 48)
Drupal\Core\StackMiddleware\ReverseProxyMiddleware->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\NegotiationMiddleware->handle(Object, 1, 1) (Line: 36)
Drupal\Core\StackMiddleware\AjaxPageState->handle(Object, 1, 1) (Line: 51)
Drupal\Core\StackMiddleware\StackedHttpKernel->handle(Object, 1, 1) (Line: 741)
Drupal\Core\DrupalKernel->handle(Object) (Line: 19)

The advice in the error was sound. Replacing return $this->renderer->render($build); in src/Plugin/CustomField/FieldFormatter/EntityReferenceLabelFormatter.php with return $this->renderer->renderInIsolation($build); fixed the issue, and display worked as expected. With that said, I have only tested in this particular use case. I haven't retested placing the field in layout builder or through a regular default node display.

πŸ› Bug report
Status

Active

Version

3.0

Component

Code

Created by

πŸ‡―πŸ‡΅Japan ultrabob Japan

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

Merge Requests

Comments & Activities

  • Issue created by @ultrabob
  • πŸ‡―πŸ‡΅Japan ultrabob Japan

    I've created a merge request with the suggested change in place.

  • Pipeline finished with Success
    7 days ago
    Total: 556s
    #397240
  • πŸ‡―πŸ‡΅Japan ultrabob Japan

    I realized after submitting this that there is a new 3.1 release, so I tried installing that to see if it fixed my issue. It did not, but the patch applies to the new version and still works, but now I get a bunch of Warnings such as

    Warning: Undefined array key "wrappers" in Drupal\custom_field\Plugin\Field\FieldFormatter\BaseFormatter->getFormattedValues() (line 477 of modules/contrib/custom_field/src/Plugin/Field/FieldFormatter/BaseFormatter.php).

    and

    Warning: Trying to access array offset on value of type null in template_preprocess_custom_field_item() (line 119 of modules/contrib/custom_field/custom_field.module).

    so I've reverted to 3.0.x

  • πŸ‡ΊπŸ‡ΈUnited States apmsooner

    Thank you for the patch and insight on the wrappers issue. The latter can probably be resolved with just saving the manage display settings as theres a new setting in there but I will resolve it anyway. I will try to get this reviewed and merged in the next day or 2. I know there are other similar deprecations for some of the other field plugins so i will want to take care of them all.

  • πŸ‡ΊπŸ‡ΈUnited States apmsooner

    I'm unsure on this suggested fix. According to docs, renderInIsolation sounds like it should be used as a replacement for deprecated renderPlain(). In this case, it sounds like it could affect the #cache tags: https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Render%21...

    The other issue is that renderInIsolation is was introduced in Drupal 10.3 so this wouldn't be backwards compatible. The suggested example for backward compatibility is here: https://www.drupal.org/node/3407994#comment-15540719 β†’

    I'm no expert on layout builder nor caching as it can get a bit complex in these various scenarios but I just want to be careful to not break other things. I think lets keep the dialog open on this issue to help make a confident approach.

  • πŸ‡ΊπŸ‡ΈUnited States apmsooner
  • πŸ‡―πŸ‡΅Japan ultrabob Japan

    That all makes sense to me. I'm also not an expert on all the caching and context details here. I just know that when I tried to place the field as a block in block layout, I started getting the DWSOD. Given that this was happening with an Entity Reference field, I wonder if the core Entity Reference Field Formatter has an example we can learn from. Maybe the solution is to detect whether there is a render context, and only renderInIsolation or renderPlain when it isn't present. I appreciate you being responsive on this. It is a very cool module, that I became familiar with due to a talk at PNW Drupal Summit and then again a bit later at BADCamp last year.

  • πŸ‡ΊπŸ‡ΈUnited States justcaldwell Austin, Texas

    Hello! Just here to confirm that we're seeing the wrapper/offset warnings described in #4 after updating to 3.1.0. Re-saving manage display settings didn't help.

    I haven't had time to investigate further. We've reverted to 3.0.x for now.

  • πŸ‡ΊπŸ‡ΈUnited States apmsooner

    Thanks for the confirm on wrappers. Subscribe to this bug and I'll get a patch up shortly to test out: https://www.drupal.org/project/custom_field/issues/3501250 πŸ› Warning: Undefined array key "wrappers" Active

  • πŸ‡ΊπŸ‡ΈUnited States apmsooner

    There's a patch now in that issue ^. Just saving manage display wouldn't have resolved it. You would need to actually edit the field settings form in manage display and then save. There's essentially a new fieldset in the field settings there that allows you to set html wrapper tags and classes to the rendered output. It's similar to the fences module but of course for these subfields.

Production build 0.71.5 2024