0

In my implementation of hook_form_alter() I am using the following code; it executes mymodule_user_form_submit() before user_save().

array_unshift($form['#submit'], 'mymodule_user_form_submit');

user_save() is not executing after that.

What is the problem with this code?

I want to execute user_save() after mymodule_user_form_submit().

function mymodule_form_alter(&$form, &$form_state, $form_id) {

  if ($form_id == 'user_register_form') {
    drupal_set_message("Form ID is : " . $form_id);
    //$form['#submit'][] = 'custom_user_form_submit';
    array_unshift($form['#submit'], 'mymodule_user_register_submit');
  }

}

function mymodule_user_register_submit(&$form, &$form_state) {
//code goes here
$form_state['field_magentopass']['und'][0]['value'] = $form_state['values']['pass'];
}

if the code is wrong how can i assign the value to this variable $form_state['field_magentopass']['und'][0]['value']

or this entire method is wrong? should i go for any other alternatives?

  • Hello, and welcome to Drupal Answers. To which form are you attaching your submission handler? How do you know user_save() is not executed? – kiamlaluno Jan 2 '13 at 11:05
  • Also, may you show the code you are using? – kiamlaluno Jan 2 '13 at 12:42
  • @kiamlaluno: i have added the code in question.It is actually a follow up of this thread [link]drupal.stackexchange.com/questions/53855/… – zamil Jan 7 '13 at 6:04
1

In your code, your submit handler ( mymodule_user_register_submit ) and actual function name ( custom_user_form_submit ) are different.

Also, your submit handler is using

$form_state['field_magentopass'] 

instead of

$form_state['values']['field_magentopass']...

It should be something like

/**
 * Implements hook_form_FORM_ID_alter
 * User register form id: user_register_form
 */
function mymodule_form_user_register_form_alter(&$form, &$form_state, $form_id) {

  drupal_set_message("Form ID is : " . $form_id);
  array_unshift($form['#submit'], 'mymodule_user_register_submit');

}

function mymodule_user_register_submit(&$form, &$form_state) {
  $form_state['values']['field_magentopass'][LANGUAGE_NONE][0]['value'] = $form_state['values']['pass'];
}

UPDATE: OP changed their question to use matching function names. Apparently new $user is saved, but field_magentopass wasn't updated.

Lastly, an alternative approach, based on form_set_value and the #validate handler

/**
 * Implements hook_form_FORM_ID_alter
 * User register form id: user_register_form
 */
function mymodule_form_user_register_form_alter(&$form, &$form_state, $form_id) {

  drupal_set_message("Form ID is : " . $form_id);
  $form['#validate'][] = 'mymodule_user_register_validate';

}

function mymodule_user_register_validate($form, &$form_state){
  $form['field_magentopass']['#parents'] = array('field_magentopass'); 
  form_set_value($form['field_magentopass'], array(LANGUAGE_NONE => array(0 => array('value' => $form_state['values']['pass']))), $form_state);
}
  • sorry its a typo.i corrected it now – zamil Jan 7 '13 at 6:28
  • OK, what is the code in your submit handler? If that fails, the rest of the code will fail to run. – David Thomas Jan 7 '13 at 6:29
  • I have a field in registration from which is hidden,and i want to assign the user submitted password from form to that variable so that it stores in table field_data_field_magentopass in drupal database – zamil Jan 7 '13 at 6:52
  • Any error messages in logs? – David Thomas Jan 7 '13 at 6:55
  • no errors. it creates a drupal user. but it will not insert data to the table field_data_field_magentopass – zamil Jan 7 '13 at 7:00
-1

You have to generate an user object in your submit function. pass this object in user_save within same function only.

  • can u make it a bit more clear – zamil Jan 2 '13 at 10:47

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.