0

I have a user profile form. I'm having trouble modifying a variable before it saves to the database. I have these hooks in my template.php but the value going into my database is still the same value that was submitted in the form post.

function mytheme_user_profile_form_presave()
{
  if($form['#id'] == 'user-profile-form') {
    if($_POST)
    {
            $ec = 'fooo';
            $form['field_last_name']['und'][0]['value']['#default_value'] = $ec;
            $form['field_last_name']['und'][0]['value']['#entity']->field_last_name['und'][0]['value'] = $ec;
            $form['field_last_name']['und'][0]['value']['#entity']->field_last_name['und'][0]['safe_value'] = $ec;
            $form['field_last_name']['und'][0]['#entity']->field_last_name['und'][0]['value'] = $ec;
            $form['field_last_name']['und'][0]['#entity']->field_last_name['und'][0]['safe_value'] = $ec;
            form_set_value($form['field_last_name'],array('und' => array(0 => array('value' => $foo))),$form_state);

    }
  }
    return '';
}

function mytheme_preprocess_user_profile_form()
{
  if($form['#id'] == 'user-profile-form') {
    if($_POST)
    {
            $ec = 'fooo';
            $form['field_last_name']['und'][0]['value']['#default_value'] = $ec;
            $form['field_last_name']['und'][0]['value']['#entity']->field_last_name['und'][0]['value'] = $ec;
            $form['field_last_name']['und'][0]['value']['#entity']->field_last_name['und'][0]['safe_value'] = $ec;
            $form['field_last_name']['und'][0]['#entity']->field_last_name['und'][0]['value'] = $ec;
            $form['field_last_name']['und'][0]['#entity']->field_last_name['und'][0]['safe_value'] = $ec;
            form_set_value($form['field_last_name'],array('und' => array(0 => array('value' => $foo))),$form_state);

    }
  }
    return '';
}

function mytheme_user_profile_form_validate()
{
  if($form['#id'] == 'user-profile-form') {
    if($_POST)
    {
            $ec = 'fooo';
            $form['field_last_name']['und'][0]['value']['#default_value'] = $ec;
            $form['field_last_name']['und'][0]['value']['#entity']->field_last_name['und'][0]['value'] = $ec;
            $form['field_last_name']['und'][0]['value']['#entity']->field_last_name['und'][0]['safe_value'] = $ec;
            $form['field_last_name']['und'][0]['#entity']->field_last_name['und'][0]['value'] = $ec;
            $form['field_last_name']['und'][0]['#entity']->field_last_name['und'][0]['safe_value'] = $ec;
            form_set_value($form['field_last_name'],array('und' => array(0 => array('value' => $foo))),$form_state);

    }
  }
    return '';
}

what am i doing wrong?

UPDATE

Ok, I added this in my template.php

function mytheme_form_alter(&$form, &$form_state, $form_id) {
  if($form['#id'] == 'user-profile-form') {
    $form['#validate'][] = 'mymodule_foo_data';
  }
}

Then I added this to mymodule.module

function mymodule_foo_data(&$form,&$form_state)
{

        $form['field_last_name']['und'][0]['value'] = 'foo';
}

The function mymodule_foo_data gets fired on save. But the value foo doesn't get written to my database. Instead, the value that's in the $_POST array is what gets written to the database. how do I get foo into the databasE?

0

I don't think your approach is the best. You need to do the following:

  1. Add a custom module.
  2. Implement hook_form_alter in your module and and alter the user profile form. Form id: user-profile-form.
  3. Add a validation function i. e. $form['#validate'][] = 'my_module_custom_form_validate';.
  4. add an implementation for the function in your module and modify the form values as you wish.

Also, you don't need to use the $_POST variable if you are following the drupal form api.

  • thanks i was able to get my_module_custom_form_validate to fire. However, the values are going straight into my database without the field modifications I intended. I added my small bits of code showing this in my question. – John Jun 13 '14 at 20:50
  • You should alter $form_state not $form. Inside validate handlers you should use form_set_value to alter the $form_state – wolverine Jun 13 '14 at 21:04

Your Answer

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

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