I am trying to update form fields based on an uploaded file. Specifically, the text that is displayed on the webpage. If the user then presses a submit
button, these values are then passed to hook_node_submit()
and saved in the database. I'm using hook_form_alter()
to perform these updates, and I have found that I can change the default_value
of a field when the webpage is first loaded. However, when the upload
button is pressed by a user, and hook_form_alter()
is called again, the same operation does not work. For instance,
function mymodule_form_alter(&$form, &$form_state, $form_id){
if ($form_id == 'mymodule_node_form'){
// I can change the default value here
$form['field_myfield']['und'][0]['value']['#default_value'] = "Some Value";
// Check to see if the upload button has been pressed
if (array_key_exists('clicked_button', $form_state))){
$trigger = $form_state['clicked_button']['#value'];
if($trigger == 'Upload'){
// Try to change the default value here. The value is changed in $form,
// but the field is not updated on the website.
$form['field_myfield']['und'][0]['value']['#default_value'] = "New Value";
// try changing the value after_build
$form['#after_build'][] = 'mymodule_after_build';
}
}
}
}
function mymodule_after_build($form, &$form_state){
$form['field_type']['und'][0]['value']['#value'] = 'New Value';
$form_state['values']['field_type']['und'][0]['value'] = 'New Value';
return $form;
}
Do I need to perform some sort of refresh on the form? I briefly looked at AHAH, is this something that I need to use?
I am new to Drupal so I apologize in advance if there is something fundamentally wrong with this approach. Any help is greatly appreciated.
Edit:
I added some code trying to use an after_build
function to change the cck values. This still doesn't change the value that is rendered on the website.