0

I'm trying to compute a user profile field using values submitted in a custom user profile edit form. I know that the callback is firing, but it won't set the new value in the field. I've tried every incantation I could think of to set it (right now I'm just trying to flat out set it, never mind the calculation). To debug it, I've tried putting a print_r statement in the callback for both $form and $form_state, but it doesn't print anything.

What's the right syntax? Or is it a bigger problem because it's the user profile, or because I'm mixing a module and a .tpl, or something entirely different? The other form fields are all being set as expected.

This is Drupal 7, btw.

My code, way simplified:

In template.php:

function my_theme_theme() {
    return array(
        'user_profile_form' => array(
        'arguments' => array('form' => NULL),
        'render element' => 'form',
        'template' => 'templates/user-profile-edit'
    ),
  );
}

In my_module.module:

function my_module_form_alter(&$form, &$form_state, $form_id) {     
    if ($form_id == 'user_profile_form') {
    $form['#submit'][] = 'my_module_compute_values';
}   
}
function my_module_compute_values (&$form, &$form_state) {
$form_state['field_age']=300;
}
  • Have you tried "preprocess functions" for the user_profile_form in the hook theme? – Stone Vo Jun 16 '13 at 16:45
  • Thanks a million! I think that's put me on the right road. I put this in my template.php: function my_theme_preprocess_user_profile_form(&$variables) { $variables['form']['field_age']['#default_value']=1; $variables['form']['field_age']['und'][0]['value']['#value']=2; } Now I just have to do the calculations. I get why I have to use ['und'][0]['value'] in the value so that it doesn't wipe out my label. I still don't understand why I can set the default without it, though. I also have to learn how to shut off the enter key as submit in here too :-). – auntvanilla Jun 18 '13 at 16:07
  • I usually put the "preprocess functions" in the custom module rather than in template.php from I thought It'll be easy for maintenance and development. – Stone Vo Jun 19 '13 at 7:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.