hook_user():将额外的字段插入数据库而不仅仅是表单

发布于 2024-09-11 06:07:37 字数 470 浏览 11 评论 0原文

我可以在注册中添加一个额外的字段。我需要知道的是我需要采取什么步骤来获取该输入并将其插入到 drupal 的用户表中。下面的代码位于我的模块中,它仅向表单添加一个字段,但是当提交时,它不会对数据执行任何操作。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷荒 2024-09-18 06:07:37

正如 hook_user() 文档中所述:

$op 正在执行什么类型的操作。可能的值(按字母顺序排列):
- “插入”:正在添加用户帐户。模块应将其对用户对象的自定义添加内容保存到数据库中,并在 $edit 中将保存的字段设置为 NULL。
- “更新”:正在更改用户帐户。模块应将其对用户对象的自定义添加内容保存到数据库中,并在 $edit 中将保存的字段设置为 NULL。
- “验证”:用户帐户即将被修改。该模块应该验证其对用户对象的自定义添加,并根据需要注册错误。

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

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

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文