2

I added some custom field so that i can get more information when someone registers into the website but i want to alter one field on the form. I created a module which has a hook_form_alter function so that i can alter the field_first_name of the registration form. here is the code

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';
   }

}

I also created a the function which handles the form submission.

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['value']['field_first_name'],
        ))->execute();
    drupal_set_message(t('your form entry has been added'));
}

My problem is that when i submit the form and i check the user details. i find that the first name details does not exist in the database or when i login as the administrator and click on the 'people' link. i find that all information is are submitted except the first name field which i am trying to alter. I also tried to submit the form without the form submit function but it still doesn't work.

and i get the following error message if i add the form_submit function

Notice: Undefined index: value in user_registration_form_submit() (line 37 of /var/www/html/lite/sites/all/modules/user_regestration/user_registration.module).

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] => ) in user_registration_form_submit() (line 38 of /var/www/html/lite/sites/all/modules/user_regestration/user_registration.module).

THIS IS LINE 37 AND 38 OF MY CODE

'field_first_name_value' => $form_state['value']['field_first_name'],
))->execute();

I am creating the module on localhost first before i push it to the live website

2

There's a typo, change value to values

To get the user id use:

<?php
global $user;
print $user->uid;
?>
| improve this answer | |
  • Thanks for that correction but know the error has changed and its as follows ................."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)." – Ian Nato Jun 14 '16 at 8:37
  • so - what entity are you trying to insert ? a field revision ???? something seems strange to me here .. – rémy Jun 14 '16 at 9:49
  • I looked into my database and found that drupal had made a table for the field first_name so i want to insert data into the first_name field but drupal is giving me an error of entity_id does not have a default value. the entity_id field is in that table. – Ian Nato Jun 14 '16 at 10:17
  • the entity in this case refers to the user. so the entity_id will be the userid, see my edit to get the current user's id – rémy Jun 14 '16 at 12:11
  • It could be that the user is not created in your hook ... – rémy Jun 14 '16 at 12:12

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.