So I've extended the core file widget, and when I go to edit a node with multiple files attached to a field, when I click Remove, it seems to go through all of the form build steps again. I don't know when or where I'm supposed to be removing this file item.
I am using:
$elname = $form_state->getUserInput()['_triggering_element_name'];
$elval = $form_state->getUserInput()['_triggering_element_value'];
To figure out which element of which field I'm supposed to be removing, but no matter where I try to do this, it seems like html is being left behind after the ajax call has finished and the form build has finished. After this, if I click the Remove button on another one of the files, I can see that even though the item for the previous file is now empty, it is still processing it as an item, though an empty one. How am I supposed to get rid of the old item, and make the form remove all traces of it?
Waiting for the value callback to process the element that I don't want, and returning array(), or null
Using massageFormValues to remove the element that I don't want, and returning an array without it.
Writing an override for formMultipleElements and returning an array without the element I'm removing, and making sure the delta is one less than it would otherwise have been.
EDIT: I've read in some places that it seems like I'm supposed to unset the field from the form_state values and call a formRebuild with the form_state object, but what function would I be expected to do this in? I have formElement, formMultipleElements, the value and process callbacks, massageFormValues, and no matter where I think of putting it, it seems wrong. All of these functions are called multiple times during the build process of the form, so calling a rebuild before the form_state object is actually complete seems wrong. I'll post if I figure anything out.