As explained in #414424-43: Introduce Form API #type 'text_format' and below, the current pattern used by core widgets of defining a specific FAPI #type expanded into 'common' FAPI #types (textfield, textarea, etc...) is a CCK D6 attempt at reusability that didn't fully live up to its expectations, is not in line with the directions of D7, and makes a code workflow that is both convoluted and extremely painful to interact with in form_alter().
While this approach might still make sense for some specific widgets on some specific field types and remains possible, widgets shipped in core should be done 'the simple way' (which was in fact not possible before #567064: Widgets done "the easy way" have too many limitations), to serve as example for contrib field types..
#414424: Introduce Form API #type 'text_format' is already doing this for text widgets, here's a patch for number.
Other widgets will follow.
| Comment | File | Size | Author |
|---|---|---|---|
| #12 | field_number_simplify.patch | 13.19 KB | yched |
| #6 | field_number_simplify.patch | 12.89 KB | yched |
| #4 | field_number_simplify.patch | 16.49 KB | yched |
| field_number_simplify.patch | 12.84 KB | yched |
Comments
Comment #1
sunAwesome! Much nicer code!
(minor) Typo in Substitute.
The 'value' key seems to be wrong.
s/from/form/ ?
Prefix with "Handle "?
Hmmm... Either we should build the element in 'value' upfront, or we should simply set #parents accordingly. I'd highly prefer the latter. However, it's possible that we do not setup #parents in the passed $element. So that would be something we should consider.
strtr() is faster.
We perhaps should also add an inline comment that clarifies the presumption that the 'type' without number_ prefix is supposed to be the name of a number type.
There should be a newline in between separate cases that do not belong together or include a fall-through logic.
No colon after @todo and start with an uppercase letter to form a proper sentence.
This should not happen.
I'm on crack. Are you, too?
Comment #2
sunTagging.
Comment #3
moshe weitzman commentedMuch, much better. Hurray for simpler widgets
Comment #4
yched commentedUpdated for sun's comments.
We now provide #parents in the base $element received by hook_field_widget(). This additionally makes sure that field values end up where we expect them in field_default_extract_form_values(), even if the form structure is altered for presentational purposes.
Comment #6
yched commentedI removed the #parents stuff, it has consequences that are not fully clear to me yet, required me to patch filefield as well, and I don't want to derail the overall simplification done here.
I don't think we got #567064: Widgets done "the easy way" have too many limitations fully right (see my remark in #414424-60: Introduce Form API #type 'text_format'), and the question of whether we can pass #parents in a way that makes sense can be sorted out later.
As sun pointed on IRC, field_default_form() does need to set some #parents on the wrapping containers to ensure that field_default_extract_form_values() finds submitted field values where it expects them, whatever form_alters might have been performed on the form structure, but that's for a different issue.
So, this patch fixes sun's comments in #1, except:
So my answer is:
- #parents would be something to consider, but in a followup ;-)
- I can't build the element in 'value' upfront because of the $element arg received by hook_field_widget().
If the arg was named $base or whatever, I could do
but I can't do
Comment #7
sunok, agreed, separate issue. I guess this will pass again.
Comment #8
yched commentedThanks sun ;-)
Spin-offs:
#627730: Make field forms resilient to form_alter() structure changes
#627204: Unnecessary prefix on field 'add more' $form element
Comment #9
sunmmmmh... I reviewed this once again, so I actually meant RTBC ;)
Comment #10
yched commentedYes, sorry, FF reload playing tricks on me.
Comment #11
dries commentedNice clean up. Two minor suggestions:
The naming seems inconsistent here. Should be 'separator' or 'decimal_precision' and 'decimal_scale'. Either prefix with 'decimal_' or don't at all. I'd prefer just 'separator' because it is also used for floats.
Should be "the node form's" instead of "node form", not? Slightly better English.
"and allow" should be "and to allow", I think.
Should be a quick reroll! Great work.
Comment #12
yched commentedNot really, actually. 'decimal' in 'decimal_separator' is not a prefix, it's the full name of the property: 'the decimal separator'. As such, it is also a valid name for floats.
On the display side, formatters already have 'thousand_separator' and 'decimal_separator'. We just make the name of the setting consistent between the widget side and the formatter side. Widget's don't have a 'thousand separator' because it's more a display thing than an input thing, but I'd rather have the setting formatters and widgets do have in common to have the same name.
I'd also rather avoid 'decimal_precision' and 'decimal_separator', because 'precision' and 'scale' map directly to SQL and schema API properties for decimal columns, and also because, precisely, we don't prefix the name of our settings ;-).
So I'd advocate for the names in the patch. Rerolled patch fixes the 'node form' sentence.
Bouncing back to RTBC for Dries.
PS: meanwhile, and after an nth plea, I finally learned to roll a diff -up patch on Win / Eclipse...
Comment #13
yched commented"Bouncing back to RTBC for Dries", he said.
Comment #14
yched commentedNext in line: #628188: Remove #process pattern from taxo autocomplete widget
Comment #15
dries commented@yched, fair enough. Thanks for the clarification. Committed!
Comment #16
yched commentedFollowup for the @todo added in this patch: #631048: Number rounding should not be done by widgets
Comment #17
yched commentedNext in line: #635202: Remove #process pattern from option widgets