Issue #3585946: Fix hide-when-empty interfering with first "Add another item" click

Reproduces and fixes the bug reported in #3585946.

Root cause

hook_field_widget_complete_form_alter() checks $items->count() > $delta_min to decide whether to hide the trailing empty widget. Core's WidgetBase::formSingleElement() appends empty items to $items so its count matches the number of rendered widgets — not the number of populated values. So after the first click on "Add another item" on an empty field (items_count: 0 → 1, two widgets rendered), $items->count() is 2 and the module hides the freshly added empty widget, making the click appear to have no effect.

Fix

Switch the gating condition to a count of populated items (\!$item->isEmpty()), computed by iterating $items:

  • always — always hide the trailing empty widget (unchanged).
  • not_empty — hide only when there is ≥1 populated value.
  • not_required — hide when there is ≥1 populated value or the field is not required; keep the widget visible on a required, still-empty field so the user can enter the required first value.

Test coverage

Adds a FunctionalJavascript test (tests/src/FunctionalJavascript/MultiValueAddHiderTest.php) covering the three hiding modes on both empty and populated states, plus a regression case for the original bug. All 6 tests pass locally (ChromeDriver in Docker).

Tugboat

Also adds a .tugboat/ configuration (config.yml + setup.php) that reproduces the scenario in a fresh Drupal 11 preview.

Edited by Frank Mably

Merge request reports

Loading