drupal默认用户注册表单中只有用户名称,帐号密码,邮箱等字段,如果想对用户做一些好的交互,必须要用到用户一些稍微详细的信息,而drupal的hook_user可以很方便的让我们添加这些信息,比如我的站点要给用户加入性别、详细地址和个人简介,我们可以实现user钩子如下(我的模块叫snippet):
/ S+ w# C' L( L9 F注:本人对于字符串都没有加t函数做翻译,是为了提高速度,需要的用户可以适当修改。 0 B; {2 t4 j7 v; z. u
/** *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’ => ‘现居住地址’, ); 0 H& G* p* U0 ^0 G+ g1 E
$fields['personal_profile']['introduction'] = array(’#title’ => ‘个人介绍’, ‘#type’ => ‘textarea’, ‘#rows’ => 5, ‘#cols’ => 50); 1 P) _1 w8 h; j) r, x2 T
return $fields;
) H' F s& b9 ~( C* V 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, ); 7 j5 ?2 O; f% e
$fields['personal_profile']['introduction'] = array( ‘#title’ => ‘个人介绍’, ‘#type’ => ‘textarea’, ‘#rows’ => 5, ‘#cols’ => 50, ‘#default_value’ => $user->introduction ); return $fields; } } ! R0 j$ y% c. `1 Z- Y2 e# C, ^7 r
$ I! T+ j& N: s6 S. Y7 T" u- f
其中的register这个op控制用户注册表单,而form这个op控制用户编辑自己个人资料的表单。form只是比register加入了一些默认值而已,而这些默认值是从登录用户的user对象中读取的,很简单吧。
6 l4 w4 v5 A$ ~5 `( W最后,如果你只是一个drupal使用者,不妨可以使用drupal内置的profile模块,手动配置和添加,更方便。
* W3 R5 `& _& y9 j8 y6 n4 f: h1 \, N( e6 r" p8 u2 Q9 u9 b! S
本文选自代码云,谢谢!!!
9 x6 V" o8 K: e# |. j) ?2 w5 H2 `$ x/ c8 |6 e& r- ^
( B6 o: v% {0 Y, ~( i7 C; Q6 Z |