1

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.

2 Answers 2

1

After many hours, I've found a solution! Thanks goes to Matthew Davidson, who posted this workaround on drupal.org, full post The working code looks like this:

function mymodule_form_alter(&$form, &$form_state, $form_id){
   if ($form_id == 'mymodule_node_form'){

       // create a button to populate the form
       $form['mymodule_pop_btn'] = array(
           '#type' => 'submit'.
           '#value'=>t('Populate Fields'),
           '#submit'=>array('mymodule_populate_fields'),
       );
    }
}

//function to populate the form fields
function mymodule_populate_fields($form, &$form_fields){
    $form_state['input']['my_field']['und'][0]['value'] = 'Some New Value';
    $form_state['rebuild'] = TRUE;
    $form_state['no-cache'] = TRUE;
}
0
  1. Make sure you are altering the desired form by checking $form_id value. hook_form_alter alters all the forms.
  2. You can use the submit handler of the form - function the_form_id_submit() - to add your custom functionality.
1
  • I originally omitted the code that is checking $form_id. I've added it above.
    – Castrona
    Commented Jul 9, 2013 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.