Problem/Motivation

When the multivalue element is disabled, input is impossible, so no empty item should be added to the element.

Steps to reproduce

Add a disabled multivalue element to a form.

Added in third commit:
Add an extra option #add_empty (supposed to be 1 or TRUE by default) which can be set to 0 or FALSE to not add extra items, also when the element isn't disabled.

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

DieterHolvoet created an issue. See original summary.

dieterholvoet’s picture

Status: Active » Needs review
roderik’s picture

Title: Don't add an empty element when the multivalue element is disabled » Don't add an empty element in some cases
Issue summary: View changes

I wanted to create a patch but it's probably best to add to this one, because functionalities overlap.

I have a feature request to optionally not add an empty item. In my use case, I have a big textarea element for a configuration value that is multi-value in theory, but most often has just one value. So I need the 'add another item' button, but I'd like to not display the empty second textarea by default.

So I added support for

  • #add_empty = FALSE to not add the empty item... unless the user added 0 items (which means this edit screen is probably 'new'), then add it.
  • #add_empty = 0 to not add the empty item, period.

I can understand if you don't like the values FALSE or 0 being different. Just one thing: with this setup it would be trivial to support #add_empty = 2, 3, ... to add several extra empty items by default (with 1 being the default #add_empty value).

--

Also:

  • Before changing this, I changed $max to $nr_items in another commit. This doesn't change anything - but the way we are populating $max with a value and then adding ($max + 1) extra items IMHO made the code less readable.
  • Made some minor Drupal style fixes in the next commit.
roderik’s picture

Fixed a bug I introduced in the interaction between the "add new item" button + #add_empty=FALSE.
(If the element has no values yet, it was adding 1 element to the display. But after pressing "add new item" for the first time, it was still displaying 1 element.)

Questions:

These are possibly dumb questions -- but If I'm missing something re. workload, and in practice only people who work at DIGIT can get things pushed through... maybe I should write a job application before writing automated tests ;-)

sardara’s picture

I will take a look today or tomorrow at this PR. I've been thinking about the original scope of the issue already, but to be honest I tried to keep the form element as close as the core functionality as possible.

Regarding PRs, as I wrote in the other issue will we fix soon (hopefully) the tests to be run. This module has full (again, hopefully) test coverage, so any PR will have to provide test coverage too here.
No need for Github PRs, we will sync them back there.

roderik’s picture

Thank you. Feel free to cut off my commits (maybe except the first) if it's easier to commit just the initial scope. I'll be doing some other things before writing tests for this.

[edit] but/and I'll also wait for your opinion on whether #add_empty can/should be added to this module at all.

(Re. closeness to Core functionality: I did indeed see that the standard functionality matches that - and didn't change the default behavior,)

hnln’s picture

This patch does apply on beta6, but throws a notice:

Warning: Undefined variable $max in Drupal\multivalue_form_element\Element\MultiValue::processMultiValue() (line 234 of modules/contrib/multivalue_form_element/src/Element/MultiValue.php).

super_romeo’s picture

Same thing with dev-version.

aaron.ferris’s picture

Im seeing the same notice, although the Delta feels a bit odd in its original state, doesnt this mean every element _weight array has the same delta?

strykaizer made their first commit to this issue’s fork.

strykaizer’s picture

Fixed issue with reference to $max variable

Fixed issue for required multivalue fields, to ensure we always keep at least one element, if the multivalue field is set as required.

darvanen’s picture

Status: Needs review » Needs work

There are some CI failures.