Coming from #1824500-16: In-place editing for Fields:
+++ b/core/modules/edit/includes/form.inc @@ -0,0 +1,147 @@ + // 'submit' in D8 is for "building the entity object", not for actual + // submission. It appears though that if there were no validation errors, it + // is submitted automatically. + field_attach_submit($entity->entityType(), $entity, $form, $form_state, $options);1. I don't understand the second sentence of this comment. What exactly is happening automatically, and is that good or bad?
2. Please open an issue for us to rename field_attach_submit(), because in both HEAD, and here, it is called from within validation, and that is very confusing.
Add a @todo comment here linking to that issue.
And my explanation:
In D8, there is no actual submit handler, the EntityFormController stuff (or whichever class it is) takes care of that. IIRC,
field_attach_submit()(just maps the form values onto the entity object (seeentity_form_submit_build_entity()andEntityFormController::buildEntity()),field_attach_validate()validates the entity object itself, and if the EntityFormController notices there are zero validation errors, then it'll just save the entity object. No submit handler is involved at all. It took me a very long time to figure out this bizarre way of handling validation/saving. I'm sure it's just an artifact of the major changes that have been going on though :)
We need to fix this :)
| Comment | File | Size | Author |
|---|---|---|---|
| #27 | field_attach_submit-1846648-27.patch | 15.58 KB | swentel |
| #17 | field_attach_submit-1846648-17.patch | 15.59 KB | swentel |
| #16 | field_attach_submit-1846648-16.patch | 15.91 KB | yched |
| #14 | field_attach_submit-1846648-14.patch | 14.77 KB | yched |
| #12 | field_attach_submit-1846648-12.patch | 14.48 KB | yched |
Comments
Comment #1
yched commentedYes, I noticed that when working on "widgets as plugins".
At some point during the EntityFormController work, field_attach_submit() calls got moved to EntityFormController::buildEntity(), which runs during validate.
Thus, I'm not sure what's the meaning of 'submit' anymore :-)
On this topic, also see #1768526: NodeFormController::validate() calls buildEntity() twice
Comment #2
wim leersHehe, exactly :)
It's like there's this alternative Field API universe where "submit" means something completely different :)
Anyway, not a big issue, but it is a big WTF for those reading the code of course. I'm sure it'll get fixed :)
Comment #3
yched commentedWell, it used to mean "submit", but then the outside world changed the moment it called the function :-)
Comment #4
yched commentedSo it seems something like field_attach_build() would be a more accurate name. It does lack a bit of 'form' context though.
[edit: also, 'form build' is yet something else for FAPI...]
Suggestions welcome :-)
Comment #5
effulgentsia commentedFWIW, the Symfony Forms component calls this bind(). So, I'm tentatively thinking field_attach_form_bind(). Without knowing Symfony's terminology, this is a little unintuitive though (at least for me). field_attach_form_bind_input(), field_attach_form_bind_values(), or field_attach_form_bind_submission() would all be more explicit, but then again, we don't add any of those suffixes for field_attach_form_validate() (though with validate, it's more clear, since what can you possibly validate other than the input/values/submission).
Comment #6
yched commentedI kind of like "bind". But, true, it would be a little isolated on the drupal side. Unless there are other parts of the form flow that could be named accordingly ?
Comment #7
effulgentsia commentedNote that there's a very important thing about buildEntity(), which is that it clones $form_state['entity'], and then calls field_attach_submit() and field_attach_form_validate() only on that clone. $form_state['entity'] is only updated during EntityFormController::submit(), which like any form submit handler, only runs if the form submission was valid. This way, invalid user input can never leak into $form_state['entity'].
EntityFormController::actions() creates a submit button and sets its #submit to its own 'submit' and 'save' methods. What's bizarre about that? It's two perfectly reasonable submit handlers, which like all submit handlers, only run if the form validated successfully.
What I do agree is bizarre though is that we name the function that copies $form element values to $entity field values (with the possibility of this not necessarily being a direct copy, but can include data transformation of some kind) "submit". Which is what this issue is trying to address.
Comment #8
effulgentsia commentedWhat about field_attach_bind_form_values()? Since we're not binding straight user input/submission: FAPI has already done that prior to setting $element['#value']/$form_state['values']. Instead, we're doing a separate process of binding form values to entity field values.
Or, field_attach_bind_from_form()?
Comment #9
yched commentedre #8:
Actually, 'extract_form_values' was an 'op' in D7 (field_default_extract_form_values(), was called in both field_attach_submit() and field_attach_validate().
So I guess, yes, reintroducing that extract_form_values denomination would be the way to go.
field_attach_submit() -> field_attach_extract_form_values()
WidgetInterface::submit() -> WidgetInterface::extractFormValues()
Or "bind" rather than "extract", but "extract" is what we have in D7, and "bind" isn't used anywhere else in core right now AFAIK.
I don't have a strong opinion here.
Comment #10
yched commentedI'll submit a patch for the rename when "edit in place" is committed.
Better to avoid a reroll there...
Comment #11
wim leers#10: Thanks for your consideration :)
Comment #12
yched commentedOK, #1824500: In-place editing for Fields got in (kudos !).
Here's a patch.
Comment #14
yched commentedEr, rather the non half-baked version, then.
Comment #16
yched commentedForgot that field_ui calls the method directly.
Comment #17
swentel commentedRerolled after removal of $entity_type parameter.
Comment #19
swentel commented#17: field_attach_submit-1846648-17.patch queued for re-testing.
Comment #20
swentel commented#17: field_attach_submit-1846648-17.patch queued for re-testing.
Comment #21
wim leersStraightforward function renaming patch that prevents confusion about what the function actually does. Tests pass (and I know several of them are very stringent, so I trust them), code comments updated.
Comment #22
effulgentsia commentedRTBC+1. It's not a bug though, so recategorizing and retitling.
Comment #23
effulgentsia commentedTrying to learn to be less verbose :)
Comment #24
dries commentedThis patch no longer seems to apply. Asking for a re-test.
Comment #25
dries commented#17: field_attach_submit-1846648-17.patch queued for re-testing.
Comment #27
swentel commentedRe-rolled, should be fine.
Comment #28
catchCommitted/pushed to 8.x, thanks!
Will need a short change notice.
Comment #29
yched commentedThanks !
Change notice created at http://drupal.org/node/1907724.
Comment #30
yched commenteduntagging
Comment #31
yched commentedAlso added a note in http://drupal.org/node/1882428
Comment #32
Tor Arne Thune commented