Reviewed & tested by the community
Project:
Multifield
Version:
7.x-1.x-dev
Component:
User interface
Priority:
Normal
Category:
Bug report
Assigned:
Unassigned
Reporter:
Created:
11 Aug 2016 at 15:30 UTC
Updated:
19 Aug 2024 at 17:44 UTC
Jump to comment: Most recent, Most recent file

Comments
Comment #2
elijah lynnComment #3
elijah lynnComment #4
elijah lynnComment #5
elijah lynnSo, I've been working on looking at all the linked issues I just added and starting to refactor those updates into Multifield. Not trivial and there are many patches stacked on top of each other in the current state of Field Collections but I do think that those issues hold the answer to this.
Comment #6
elijah lynnJust throwing notes in here:
The 'Added a remove button to field submit widget' was added with http://cgit.drupalcode.org/multifield/commit/?id=15683eb1b68f75a37df4a2e... on 2013-07-10 22:57:55 which consists of a copied field_collection_remove_submit() and field_collection_remove_js() but it was not precisely a copy and appears to have been modified a little bit when comparing against the field_collection code base around that time.
Comment #7
elijah lynnI did a git history for selection in PhpStorm on just the functions field_collection_remove_js() & field_collection_remove_submit() and these are the changes between the time these functions were copied from FC, 2013-07-10 and now:
Comment #8
elijah lynnSo, a while ago I did reapply all of these patches but that did not resolve this issue.
Field Collection does still work fine, these seems like an off by 1 issue.
Comment #9
elijah lynnJust so I don't lose the work and maybe it will be useful, here are the patches in #7 backported to Multifield. https://github.com/ElijahLynn/drupal-multifield/commits/7.x-1.x
I don't remember why #2416237: Deleting field collection items from node, then adding more creates multiple revisions of same field collection entity on save isn't there.
update: Oh yeah, because it is trying to get $field_state['entity'] and that appears to be something Field Collection puts there. I don't think that patch is valid for multifield but maybe the concept is in a different way.
Comment #10
elijah lynnOkay, so I deep dove into this and I have a working patch.
I tried not to but ended up doing mostly what Field Collection is doing. FC is managing the field state with $form_state['field']['entity'] where they update it on the remove_submit() handler and rebuild the form based off those values. I couldn't find any other way.
This patch introduces $form_state['field']['state'] (I didn't want to call it entity). It is initialized in multifield_field_widget_form() and then updates the state in multifield_field_widget_item_remove_submit().
The remove (delete) button functionality was originally introduced in this patch in this issue #1234574: Add a delete button on the field-collection item edit form which is also the source of field_collection_remove_submit() that we copied into multifield_field_widget_item_remove_submit(). The issue came across the same issue this issue is about (https://www.drupal.org/node/1234574#comment-5205538). I think we didn't copy the parts that said 'entity' because it made sense we didn't have to deal with that. But although it was for entities, the field state saving part is what really mattered. In my patch I don't have entities but enough information to be useful within multifield_field_widget_form().
Comment #11
bleen commentedIt looks like this logic will only work when you remove the (n-1)th item. Does this still work if you have 10 items and you remove number 3?
2 nits: There is still room in teh 80 char limit for "This is so" to be on the previous line AND ressurects should be ressurect
Did you mean to finish these comments?
Comment #12
elijah lynnThanks for the review.
1. Yeah, it would delete the 3rd item. The logic took me a bit to figure out. $i is actually set from a $delta earlier on. When that submit handler is called it is passed the delta of the item being removed. So the for loop starts with $i being the item being removed. Here is the full context:
2. Yeah, sometimes I leave comments some breathing room. The standards say cannot be more than 80 characters but don't say that I must use all 80 characters when possible ;).
3. Yeah, those comments should actually be todos. But it was super late and I definitely just left them in there. If we re-roll this patch it would be nice to turn them into real "why" comments. There is a lot of "why" missing from this code.
Comment #13
zekvyrin commentedGreat job Elijah!
Yes, patch #10 works as described.
I tried to fix that about a week ago (based on your notes), but didn't have much success (or time to follow your analysis exactly to figure this out), so this suits me perfectly just in time. Thanks :)
Can't wait to get this committed :)
Comment #14
greendemiurge commentedLooks like I'm a bit late to the party on this, but I'll put in my two cents since my solution resolves this with only a few lines of code and may prove helpful:
My diagnosis of the root cause here is that the current code does not manage to overwrite the #default_value keys in the form array when there are no values in the
$itemsarray. My approach was to usemultifield_field_widget_remove_item_submit()to leave a note in formstate that a multifield needed to take its values from$form_state['input']rather than$itemswhen the form was rebuilt. This allows the existing code to properly blank and/or remove elements from the form array when needed. I'm attaching my personal patch for reference. Hope it is useful to the discussion!Thanks everyone for looking into this!
Comment #15
larynWe came across this on a Backdrop site using multifield and the last patch here was successful. It's been merged into the Backdrop version and will be included in the next release there. Marking RTBC here as well.
https://github.com/backdrop-contrib/multifield/commit/dfbef95c3d23d92dbc...