国外设计欣赏网站 - DOOOOR.com

 找回密码
 注册

QQ登录

只需一步,快速开始

扫一扫,微信登陆

搜索

[Drupal教程] Drupal:用户注册表单自定义设置方法

[复制链接]
发表于 2-16-2012 23:33 | 显示全部楼层 |阅读模式

drupal默认用户注册表单中只有用户名称,帐号密码,邮箱等字段,如果想对用户做一些好的交互,必须要用到用户一些稍微详细的信息,而drupal的hook_user可以很方便的让我们添加这些信息,比如我的站点要给用户加入性别、详细地址和个人简介,我们可以实现user钩子如下(我的模块叫snippet):


! `- I) W+ y& Q( {' \* c

注:本人对于字符串都没有加t函数做翻译,是为了提高速度,需要的用户可以适当修改。


" o% n8 h$ J0 @4 f% ]0 M1 R5 C6 s7 H

/**

*Implementation of hook_user().

*/

function snippet_user($op, &$edit, &$user, $category = NULL) {

  switch ($op) {

  // User is registering.

  case ‘register’:

  // Add a field set to the user registration form.

  $fields['personal_profile'] = array(

    ‘#type’ => ‘fieldset’,

    ‘#title’ => ‘个人资料(可选)’,

  );

  $fields['personal_profile']['sex'] = array(

    ‘#title’ => ‘性别’,

    ‘#type’ => ‘radios’,

    ‘#default_value’ => 0,

    ‘#options’ => array(’男’ , ‘女’)

  );

  $fields['personal_profile']['address'] = array(

    ‘#type’ => ‘textfield’,

    ‘#title’ => ‘现居住地址’,

  );

7 I6 L8 K, G9 e/ N  l, _

$fields['personal_profile']['introduction'] = array(’#title’ => ‘个人介绍’, ‘#type’ => ‘textarea’, ‘#rows’ => 5, ‘#cols’ => 50);


* g3 o& h# B$ n( F$ h2 }- A

  return $fields;

% w/ q% x2 Y4 ~, O

  case ‘form’:

  $fields['personal_profile'] = array(

    ‘#type’ => ‘fieldset’,

    ‘#title’ => ‘个人资料(可选)’,

    ‘#weight’ => 1,

  );

  $fields['personal_profile']['sex'] = array(

    ‘#title’ => ‘性别’,

    ‘#type’ => ‘radios’,

    ‘#default_value’ => 0,

    ‘#options’ => array(’男’ , ‘女’),

    ‘#default_value’ => $user->sex,

  );

  $fields['personal_profile']['address'] = array(

    ‘#type’ => ‘textfield’,

    ‘#title’ => ‘现居住地址’,

    ‘#default_value’ => $user->address,

  );


' z9 W$ _! `% T' H

  $fields['personal_profile']['introduction'] = array(

    ‘#title’ => ‘个人介绍’,

    ‘#type’ => ‘textarea’,

    ‘#rows’ => 5,

    ‘#cols’ => 50,

    ‘#default_value’ => $user->introduction

  );

  return $fields;

}

}


! T% k( d3 I8 p8 [0 x6 A' p" y1 S
# ^, d) K; E1 x' W$ \" B( H; A

其中的register这个op控制用户注册表单,而form这个op控制用户编辑自己个人资料的表单。form只是比register加入了一些默认值而已,而这些默认值是从登录用户的user对象中读取的,很简单吧。

! ?0 }9 ]  _+ S" W( P

最后,如果你只是一个drupal使用者,不妨可以使用drupal内置的profile模块,手动配置和添加,更方便。


6 V+ ?7 L2 ?2 C% _$ U' b( r9 T
! r5 @3 |, }  U  Y2 d

本文选自代码云,谢谢!!!

1 {- O' s. k$ f$ D

9 U) w! }* J' b! H( M" z* u+ T% z7 D& H4 i- J# W7 Q: U

|2011-2026-版权声明|平台(网站)公约|手机版|手机版|DOOOOR 设计网 ( 吉ICP备2022003869号 )

GMT+8, 12-3-2025 12:59 , Processed in 0.232414 second(s), 22 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表