Multi-value fields not working properly within Layout Builder

Created on 7 August 2023, 11 months ago
Updated 9 August 2023, 11 months ago

Problem/Motivation

It doesn't appear that adding multiple field values within layout builder's inline blocks is working properly. I was able to track down the problem to this code:

  /**
   * {@inheritdoc}
   */
  protected function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state) {
    $element = parent::formMultipleElements($items, $form, $form_state);

    // If we're using unlimited cardinality we don't display one empty item.
    // Form validation will kick in if left empty which essentially means
    // people won't be able to submit without filling required fields for
    // another value.
    if (!$form_state->isSubmitted() && $element['#cardinality'] == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED && $element['#max_delta'] > 0) {
      $max = $element['#max_delta'];
      unset($element[$max]);
      $element['#max_delta'] = $max - 1;
      $items->removeItem($max);
      // Decrement the items count.
      $field_name = $element['#field_name'];
      $parents = $element[0]['#field_parents'];
      $field_state = static::getWidgetState($parents, $field_name, $form_state);
      $field_state['items_count']--;
      static::setWidgetState($parents, $field_name, $form_state, $field_state);
    }

    return $element;
  }

Straight-up removing this code fixes the layout builder issue, but regresses on the issue described by the comment.

I've uploaded a layout builder integration test -- pretty rough, and my local test runner refuses to work with chromdriver 115 -- so this might need some tender loving care on the test bot.

I have a semi-strong feeling that the problem is a result of how the inline block form uses a subform state to persist information -- more info to come as I dig deeper.

Steps to reproduce

Add a custom field to a block content type with unlimited cardinality.
Add an inline block to a layout
Attempt to create three items -- note that only two items are ever added to the widget UI, and only one item persists upon save.

Proposed resolution

TBD

Remaining tasks

  1. Get a failing test working to illuminate the problem.
  2. Figure out how to resolve this without regressing elsewhere.

User interface changes

None

API changes

None

Data model changes

None

πŸ› Bug report
Status

Fixed

Version

1.0

Component

Code

Created by

πŸ‡ΊπŸ‡ΈUnited States Luke.Leber Pennsylvania

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

Comments & Activities

Production build 0.69.0 2024