Problem/Motivation

Would be nice if an element could be removed with a single click through a Delete button, especially if you have multiple form elements.

Steps to reproduce

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#19 3185539_and_3199298_support.patch791 bytesstrykaizer
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

johnwebdev created an issue. See original summary.

dieterholvoet’s picture

The Field API doesn't support this either. It's being worked on though, I added the relevant issue to the related issues.

jane_irwin’s picture

There's a module that would serve this purpose, and it works with some other contributed modules. I've tried pairing it with Multi-value Form Element, but it doesn't work. Is that something that might be possible?

kakoum’s picture

This module is really nice, and adding a "Delete" button would make it really useful.

p.kasianov’s picture

I did add 'remove' button. Will add patch after testing and code review on our project. Currently it was tested on D10 only.

hmdnawaz’s picture

@t1mm1

Can you please add the patch or describe how you did it?

samaphp changed the visibility of the branch 3185539-add-a-delete to hidden.

samaphp changed the visibility of the branch 3185539-add-a-delete to active.

stijn.blomme made their first commit to this issue’s fork.

stijn.blomme’s picture

Status: Active » Needs review

I have altered the code from the above core issue and added it to the multivalue form element.

Please test

stijn.blomme changed the visibility of the branch 3185539-add-a-delete to hidden.

stijn.blomme changed the visibility of the branch 3185539-add-a-delete to active.

super_romeo’s picture

Need to replace
unset($delta_values['_weight']);
with
unset($delta_values['_weight'], $delta_values['_actions']);
To make the search for empty fields work properly.

joevagyok’s picture

Status: Needs review » Needs work
Issue tags: +Needs tests

Thank you for the proposal! Test coverage for this would be much appreciated!

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

hitchshock’s picture

Created a new MR, with a clear ticket branch.
Fixed value bug, `_actions` element must be removed from the result, otherwise the empty element is determined incorrectly.

strykaizer’s picture

StatusFileSize
new791 bytes

If you use the MR from this issue and you use the MR from #3199298: Don't add an empty element in some cases together, you need some extra code to make them work together.
Else the delete button will not be disabled if you have set '#add_empty' => 0,

Added extra patch for those using both patches. (This obviously wont be needed anymore once at least one of both issues is fixed).

darvanen’s picture

Version: 1.0.x-dev » 2.0.x-dev
Assigned: Unassigned » darvanen
darvanen’s picture

darvanen’s picture

darvanen’s picture

Assigned: darvanen » Unassigned
Status: Needs work » Needs review

I've opened a fresh MR against 2.x building on the existing work in this issue (thank you!)

  • Removing a row drops just that row and keeps the other rows' values, instead of shifting values up onto the wrong rows. Weights are renumbered so the remaining order stays the same, including after a drag reorder.
  • Required elements always keep at least one row. The Remove button is hidden when only one row is left, so you can't delete your way past the requirement and submit an empty required field.
  • Each button has an aria-label ("Remove row 1", "Remove row 2", ...) so screen reader users can tell them apart. The number follows the field order, not the on-screen order after dragging, which is the same behaviour as the existing "Weight for row N" labels.

Tests (functional and JavaScript):

  • Remove a row and confirm the other rows keep their own values, and the submitted values are correct.
  • Reorder rows, then remove one, and confirm the order is preserved.
  • Remove via the AJAX button and confirm the row disappears without a page reload.
  • A required element shows no Remove button at one row, shows it once a second row is added, hides it again back at one row, and still reports the field as required on submit.
darvanen’s picture

I found some further issues while I had this installed in a project, I've pushed a small update to the MR.