简体   繁体   中英

Drupal 7 hook_form_alter and submitting the form

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

First, it's values not value.

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 ..

You're not passing in an entity_id, and your database schema isn't defining a default one. The database expects one, and so it is failing, and it is failing on insert - which is your lines 37 and 38. It has absolutely nothing to do with your first name part.

Basically, Drupal (technically MySQL, or whatever your database backend is) doesn't know what entity you're trying to associate that field with.

Please look at the table that you're actually putting the information into and ensure that the schema either has a default set

Something like:

db_insert('field_revision_field_first_name')
        ->fields(array(
            'field_first_name_value' => $form_state['values']['field_first_name'],
'entity_id' => 1,
            ))->execute();

Would probably work better for you. There may be other required fields. I recommend taking a look at your database schema.

You also shouldn't have to define a custom form for this - you can add fields to users, and also there is profile2 as another poster mentioned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM