Layout Builder prevents rendering newly added fields programatically

Created on 28 June 2023, over 1 year ago
Updated 18 July 2024, 5 months ago

Problem/Motivation

When enabling Layout Builder on the default view mode of a node type, the field display options aren't shown anymore as they are now configured in the Layout Builder layout.
However, when adding a new field after Layout Builder has been activated and configuring its display on an alternative view mode, I can't seem to render it programatically with the viewField() function of the EntityViewBuilder service.

Steps to reproduce

- Enable layout builder on the default view mode of a content type
- Add a field to the content type and configure its display on the teaser view mode
- The following code snippet will result in an empty render array:

<?php

$field = $node->field_name;
$view_builder = \Drupal::entityTypeManager()->getViewBuilder('node');
$render_array = $view_builder->viewField($field, 'teaser');

?>

Extra considerations

The field display config for the default view mode is still present in the yml config, and when manually editing it to show the new field, things seem to work. However, this breaks again after saving the default view mode through the Manage display interface.

This issue seems to only be present when Layout Builder is active. A node type with a field disabled in the default view mode has no problem rendering it in an alternative view mode if it's active in the respective view mode.

Am I missing something or is this expected behaviour from Layout Builder?

This was tested on Drupal 10.0.9

πŸ› Bug report
Status

Closed: cannot reproduce

Version

11.0 πŸ”₯

Component
Layout builderΒ  β†’

Last updated about 4 hours ago

Created by

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.

  • Issue created by @robbevb
  • Status changed to Postponed: needs info 8 months ago
  • πŸ‡³πŸ‡ΏNew Zealand danielveza Brisbane, AU

    I'm unable to replicate this issue on a fresh install of D11.

    Steps I attempted:

    1. Install a fresh D11 standard site
    2. Enable LB
    3. Add LB to the default view mode of articles
    4. Added a field called field_test
    5. Added this field to the teaser layout
    6. Created a node with content in the field_test field
    7. Ran the code below in a debugger
    8. Verified that the correct values were in the render array.

    Code ran for testing:

    $node = \Drupal\node\Entity\Node::load(1);
          $field = $node->field_test;
          $viewBuilder = \Drupal::entityTypeManager()->getViewBuilder('node');
          $renderArray = $viewBuilder->viewField($field, 'teaser');

    Marking this as postponed as we need steps to reproduce. If this issue isn't updated with steps to reproduce after 3 months it will be a candidate to be closed.

    Thanks for raising an issue!

  • Status changed to Closed: cannot reproduce 5 months ago
  • πŸ‡³πŸ‡ΏNew Zealand danielveza Brisbane, AU

    No further updates have been made on this issue and it was unable to replicated in the last comment over 3 months ago, so I'm now marking this one as closed. Thanks!

Production build 0.71.5 2024