0

I have added extra fields on the drupal 7 user registration form and created a module with a hook_form_alter function so that i can alter one field in the registration form. in the same file i also added the hook_form_submit function to handle the submission of that input box.

function user_registration_form_alter(&$form, &$form_state, $form_id){
if ($form_id == 'user_register_form'){

    $form['field_first_name'] = array(
        '#type' => 'textfield',
        '#title' => t('First Name'),
        '#description' => t('Enter your first name.'),
        '#maxlength' => 255,
        '#required' => TRUE,
        );


}

if (isset($form['actions']['submit'])) {
        $form['actions']['submit']['#submit'][] = 'user_registration_form_submit';
       }
}


function user_registration_form_submit(&$form, &$form_state)
    {
        $fe_id = db_insert('field_revision_field_first_name')
        ->fields(array(
            'field_first_name_value' => $form_state['values']['field_first_name'],
            ))->execute();
        drupal_set_message(t('your form entry has been added'));
    }

When i click on the submit button i get the following error message

PDOException: SQLSTATE[HY000]: General error: 1364 Field 'entity_id' doesn't have a default value: INSERT INTO {field_revision_field_first_name} (field_first_name_value) VALUES (:db_insert_placeholder_0); Array ( [:db_insert_placeholder_0] => john ) in user_registration_form_submit() (line 38 of /var/www/html/lite/sites/all/modules/user_regestration/user_registration.module)‌​‌​

I noticed the error is generated because the"entity_id doesn't have a default value" when the insert query is executed. My question is how do i include the entity_id default value.

  • here entity id is user id, get uid from $form_state – bhupendraosd Jun 14 '16 at 10:14
  • I am new to module development in drupal. how can I get the uid when i am registering user. – Ian Nato Jun 14 '16 at 10:57
  • do you use devel module to debug? if yes dpm($form_state); you will get full structure of $form_state array variable in hook_submit($f, $f). simply you need to install devel module, contains mainy debug function use dpm($var) to get printed whole variable after page reload, or you can use watchdog(); too. google for watchdog debug. – bhupendraosd Jun 14 '16 at 11:03
1

Why don't you use a module providing this functionality, like profile2 ?

The user is not created in your submithandler, so either you save it:

function foo_user_register_form_submit($form, &$form_state){
    $edit = array(
          'name' => $form_state['values']['name'], 
          'pass' => user_password(),
          'mail' => $form_state['values']['mail'],
          'init' => $form_state['values']['mail'], 
          'status' => 1, 
          'access' => REQUEST_TIME,
          'field_first_name' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_first_name']))),
    );
    user_save(drupal_anonymous_user(), $edit);
}

For more details see the docs.

Or you could try to add a custom submit handler, which would be fired after the user is created, in form_alter:

$form['#submit'][] = 'your_custom_submit_handler_function';

In there the user should already be created. so:

function your_custom_submit_handler_function(&$form, &$form_state)
    {
        $fe_id = db_insert('field_revision_field_first_name')
        ->fields(array(
            'field_first_name_value' => $form_state['values']['field_first_name'],
            ))->execute();
        drupal_set_message(t('your form entry has been added'));
    }

But keep in mind, that this custom inserting maybe needs some more code, here you only add the revision entry ..

  • Thank you very much. The first solution worked and it was exactly what i was looking for. – Ian Nato Jun 15 '16 at 18:38

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.