hook_user():向数据库中插入额外的字段,而不仅仅是表格 [英] hook_user(): inserting extra field into database not just form

查看:105
本文介绍了hook_user():向数据库中插入额外的字段,而不仅仅是表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以在注册中添加一个额外的字段.我需要知道的是,我需要采取什么步骤来抓取该输入并将其插入到drupal的用户表中.下面的代码在我的模块中,这仅向表单添加了一个字段,但是在提交时,它对数据没有任何作用.

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') {
    // …
  }
}

推荐答案

hook_user()文档:

$ op正在执行哪种操作.可能的值(按字母顺序):
-插入":正在添加用户帐户.该模块应将其对用户对象的自定义添加项保存到数据库中,并在$ edit中将已保存的字段设置为NULL.
-更新":正在更改用户帐户.该模块应将其对用户对象的自定义添加项保存到数据库中,并在$ edit中将已保存的字段设置为NULL.
-验证":用户帐户将被修改.该模块应验证其对用户对象的自定义添加,并在必要时注册错误.

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

模块需要在hook_install()中创建自己的数据库表.

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

hook_user()可以用以下代码实现,例如:

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()是将用户配置文件值保存在数据库中的功能.该代码检查类别,以避免在用户个人资料编辑表单中显示的所有选项卡中显示相同的表单字段.

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.

这篇关于hook_user():向数据库中插入额外的字段,而不仅仅是表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆