I've been trying to figure this out for the past day or two: the correct way to pre-populate a Drupal 7 form's elements with arbitrary values once it has already been created and submitted once. During the submit handler, I have logic in there that decides whether to redisplay the form with new values (that I come up with, based on the user's input), or do something else. The scenario relevant to this post is redisplaying the form with new values, derived programmatically in the ..._submit()
function, to pre-populate some of its fields.
Sounds easy, right? Seems to be straightforward and a pretty darn common scenario that must have a clear and easy solution. So you'd think, maybe, setting the appropriate $form_state['values']
array element would do the trick? Well, no, apparently that's not what $form_state['values']
is designed for, and sure enough, it doesn't work in practice. Okay, fine. That rules out form_set_value()
, too. No problem - it does seem a bit unintuitive that form_set_value()
doesn't actually set a value in the form that will be read the next time the form is created - but that's fine, let's move on.