Right now our #value_callback handling is a bit inconsistent. In the case that a form is being submitted, the value callback is passed $element
, $input
, and $form_state
. However in the situation that the form is not being processed, the value callback is called with just $element
.
In the #391330: File Field for Core issue, I need the ability to return a value based on the contents of the $form_state alone, since when an AHAH request is made, the form is regenerated from scratch and $form_state['input']. We need to be able to get at this value so that it can be maintained when updating via AHAH.
The patch is super-simple, just pass the $form_state into the #value_callback when getting the default value, just like we do when processing input.
Comment | File | Size | Author |
---|---|---|---|
form_value_callback_form_state.patch | 813 bytes | quicksketch | |
Comments
Comment #1
quicksketchTagging.
Comment #2
mattyoung CreditAttribution: mattyoung commentedsubscribe
Comment #3
Dries CreditAttribution: Dries commentedLooks proper to me. Committed!
Comment #4
quicksketchRock, thanks!
Comment #6
Alan D. CreditAttribution: Alan D. commentedThis needs some documentation as it changes the way the value callback is used. (I only looked at the migration guide)
The Drupal 6 example element page shows why:
Comment #7
quicksketchI'm not sure what or where such documentation lives. Where did you pull this example from? I'd say that it should be written like this instead:
Comment #8
Alan D. CreditAttribution: Alan D. commentedIn Drupal 6, that would give false positives to fields that have a value set to FALSE. Thus the check on the number of arguments.
This is from the handbook page: http://drupal.org/node/169815 Creating Custom Elements Using Drupal 6.x
Comment #9
quicksketchThat's true, but since HTML isn't capable of sending Boolean values in POST, this won't be a problem in nearly any situation. You'll only have to deal with 0 or an empty string.
Comment #10
Alan D. CreditAttribution: Alan D. commentedFor some reason, I did get this once, but for the life of me, I can not remember the set up. Probably a logical error somewhere else... I'm sure I would have used "===".
But anyway, I think this should be documented somewhere, and since I'm just not sure on the exact place / protocol, I just tagged it. I guess that the $form_state should be used to check the submit value.
Something like:
---------------------------------------------------------------------------------------
Parameters to form_type_HOOK_field_value() have changed
Value callback functions, as defined by an FAPI element #value_callback property or the elements type_HOOK_field_value() callback, now get passed the $form_state as the third parameter.
Drupal 6.x:
Drupal 7.x:
---------------------------------------------------------------------------------------
And I see that the signature for _form_builder_handle_input_element() has changed.
Which means (??) that we can update the $form_state from the value callback too! Very cool! I guess that had something to do with File & Image in core?
Comment #11
jhodgdonLooks like we are asking for something to be added to the 6/7 module update guide? Changing tag accordingly. If you want some other documentation to be done, please comment and add back the other tag again.
Comment #12
jhodgdonI added a section to the module update guide about this. I didn't think an example was all that necessary, and there's a link to this issue in it.
http://drupal.org/update/modules/6/7#value-callback-state