hook_user(): inserting extra field into database not just form

十年热恋 提交于 2019-12-07 15:31:11

问题


I can add an extra field to the registration. What I need to know is what step do I need to take to then grab that input and insert it into the user table of drupal. The code below is in my module this adds just a field to the form, but when its submitted it doesnt do anything with the data.

function perscriptions_user($op, &$edit, &$account, $category = NULL){
  if ($op == 'register') {
    $form['surgery_address'] = array (
      '#type' => 'textarea',
      '#title' => t('Surgery Address'),
      '#required' => TRUE,
    );      

     return $form;     
  }

  if ($op == 'update') {
    // …
  }
}

回答1:


As reported in hook_user() documentation:

$op What kind of action is being performed. Possible values (in alphabetical order):
- "insert": The user account is being added. The module should save its custom additions to the user object into the database and set the saved fields to NULL in $edit.
- "update": The user account is being changed. The module should save its custom additions to the user object into the database and set the saved fields to NULL in $edit.
- "validate": The user account is about to be modified. The module should validate its custom additions to the user object, registering errors as necessary.

The module needs to create its own database table in hook_install().

hook_user() could be implemented with the following code, for example:

function perscriptions_user($op, &$edit, &$account, $category = NULL){
  if ($op == 'register' || ($op == 'form' && $category = 'account')) {
    $form['surgery_address'] = array (
      '#type' => 'textarea',
      '#title' => t('Surgery Address'),
      '#required' => TRUE,
    );

    return $form;     
  }

  if ($op == 'insert' || $op == 'update') {
    prescriptions_save_user_profile($account->uid, $edit['surgery_address']);
  }
  if ($op == 'validate' && $category == 'account') {
    // Verify the entered values are valid.
    // In this example, the value is contained in $edit['surgery_address'].
  }
}

prescriptions_save_user_profile() is the function that saves the user profile values in the database. The code checks the category to avoid to show the same form fields in all the tabs shown in the user profile edit form.



来源:https://stackoverflow.com/questions/3343783/hook-user-inserting-extra-field-into-database-not-just-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!