PROBLEM:
In older versions of Webform, it was possible to change the default value of a webform component using the Drupal Forms API and modifying the relevant #default_value.
In the current version of Webform, it appears this is not possible.
EXAMPLE:
For example, if you have a component with a default value of 'testing123' ... and you want to change it under a specific set of conditions, the following code does not work when using hook_form_alter:
$form['#node']->webform['components'][1]['value'] = 'testing456';
QUESTION:
Is this a bug in the current version of Webform? If this is not a bug, but instead the intended behavior, is there a way to change the default value of a component *before* webform renders the client form?
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | Screen Shot 2012-07-26 at 4.45.54 PM.png | 47.72 KB | Dentorat |
Comments
Comment #1
quicksketchBy the time you're tinkering with hook_form_alter(), most changes are going to be very iffy on upgrades. I'm actually surprised that the code you posted would ever work, unless you were on a multi-page form. If you're going to be setting the default value for a field, you probably should just set the value on the FAPI array directly, rather than trying to adjust the $node properties.
So something like this:
Comment #2
dreftymac commentedIs this supported using the webform API? There does not appear to be a way to intercept the FAPI array after webform_client_form gets called from webform.module.
Once webform_client_form gets called, it appears changes to $form['submitted']['my_field']['#default_value'] have no effect on the actual default value that the user sees populated in the form, webform just uses whatever value was initially set at weborm design-stage.
Comment #3
quicksketchWebform doesn't do anything special to its values after the form has been built. You should be able to use hook_form_alter() on it just like any other form in Drupal. If you're making the changes and not seeing the effect (especially in Firefox), make sure you hit enter in the URL bar to reload the page instead of using Reload, as Firefox and other browsers intentionally remember form values when reloading the page.
Comment #4
dreftymac commentedOK, that makes sense. Thanks very very much for the help and support.
Comment #5
Dentorat commentedNevermind
Comment #6
Dentorat commentedComment #7
Dentorat commentedI realized my (spelling) mistake