Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I'm trying to create a custom form element. I'm confused as to where #value is supposed to get set.
* in the #process function? But that doesn't seem to get called when the form is submitted
* in hook_elements?
* in the theme function?
Does it have something to do with setting #children or #tree?
Comment | File | Size | Author |
---|---|---|---|
#9 | bug41023_0.module | 1.23 KB | Wesley Tanaka |
#8 | bug41023.module | 1.21 KB | Wesley Tanaka |
#5 | 41023.patch | 825 bytes | Wesley Tanaka |
Comments
Comment #1
Wesley Tanaka CreditAttribution: Wesley Tanaka commentedoh, but wait... I updated to 4.7.0-beta2 and #process *does* seem to be called when the form is submitted
Comment #2
Wesley Tanaka CreditAttribution: Wesley Tanaka commentedif I have #process => array('fxn' => array())
and
fxn($element)
{
$element['#value'] = 'xyz';
return $element;
}
that #value doesn't seem to get passed along.
Comment #3
Wesley Tanaka CreditAttribution: Wesley Tanaka commentedI'm gonna mark this as a bug, though I could be wrong about there being no way to do this.
Comment #4
Wesley Tanaka CreditAttribution: Wesley Tanaka commentedThe #process function gets called after global $form_values takes on the value in #value, so there's no way for it to affect the value without touching $form_values itself.
Comment #5
Wesley Tanaka CreditAttribution: Wesley Tanaka commentedComment #6
Wesley Tanaka CreditAttribution: Wesley Tanaka commentedComment #7
Wesley Tanaka CreditAttribution: Wesley Tanaka commentedComment #8
Wesley Tanaka CreditAttribution: Wesley Tanaka commentedhere's a module which helps explain what this bug is about, via an illustrative example.
1. Put the module in your modules directory
2. go to "admin/modules"
3. enable the "bug41023" module
4. go to "41023"
5. press "Go"
Expected behavior:
[a] is set to 'hi there'
Actual behavior:
[a] is not set
Comment #9
Wesley Tanaka CreditAttribution: Wesley Tanaka commentedmy sample code wasn't returning $element from the #process function. fixed that.
Comment #10
drewish CreditAttribution: drewish commentedwtanaka, the value gets set but _submit uses posted values. you'll need to have your process function return hidden field.
Comment #11
Wesley Tanaka CreditAttribution: Wesley Tanaka commentedyes, i'm trying to compose those posted values into a uber-combined value.
I didn't put any children in the element because then I'd also need to add a theme function to the example code. Hold on while I expand the example a bit to make it clearer.
Comment #12
chx CreditAttribution: chx commentedI am ifne with the patch.
Comment #13
Wesley Tanaka CreditAttribution: Wesley Tanaka commentedthat's a good thing, since the new example was getting pretty hard to understand.
Comment #14
Wesley Tanaka CreditAttribution: Wesley Tanaka commenteda point from the conversation with Károly
the patch shouldn't affect anything (Károly was initially concerned about it affecting previews) since all it does is move the assignment into $form_values from immediately before the #process call to immediately afterwards.
Comment #15
chx CreditAttribution: chx commentedWell, a #process callback can check for the value of the current element in #value and if for some arcane reason it needs other elements value then it can still access global form_values. Only change is that #value is not yet added to lobal form_values in #process. That's really not a problem.
Now I marked this critical because the patched behaviour is indeed the intended one.
Comment #16
drummFrom visual inspection I think this has already been applied.
Comment #17
drummFound the commit message:
"- Patch #41023 by wtanaka: custom form elements can't cleanly set a value for themselves."
Comment #18
(not verified) CreditAttribution: commented