|
drupal默认用户注册表单中只有用户名称,帐号密码,邮箱等字段,如果想对用户做一些好的交互,必须要用到用户一些稍微详细的信息,而drupal的hook_user可以很方便的让我们添加这些信息,比如我的站点要给用户加入性别、详细地址和个人简介,我们可以实现user钩子如下(我的模块叫snippet):
% R r! u7 ~* I) _注:本人对于字符串都没有加t函数做翻译,是为了提高速度,需要的用户可以适当修改。 1 ~. G! d& l0 l' l4 y
/** *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’ => ‘现居住地址’, ); % l, J' Z r( n
$fields['personal_profile']['introduction'] = array(’#title’ => ‘个人介绍’, ‘#type’ => ‘textarea’, ‘#rows’ => 5, ‘#cols’ => 50);
/ z; u6 X# d$ M8 c7 B0 H" Z" i return $fields;
+ D" ^& i, R" R8 [ 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, ); 4 y5 _6 ]2 ^6 F+ e
$fields['personal_profile']['introduction'] = array( ‘#title’ => ‘个人介绍’, ‘#type’ => ‘textarea’, ‘#rows’ => 5, ‘#cols’ => 50, ‘#default_value’ => $user->introduction ); return $fields; } }
" M$ |9 u0 L V8 d( m: Q. Z
4 a2 g1 y" m% N( i) Q: N$ ?5 ?" B其中的register这个op控制用户注册表单,而form这个op控制用户编辑自己个人资料的表单。form只是比register加入了一些默认值而已,而这些默认值是从登录用户的user对象中读取的,很简单吧。 7 W% c2 g: Y# v+ h, r& s a( y7 c4 |3 n/ y
最后,如果你只是一个drupal使用者,不妨可以使用drupal内置的profile模块,手动配置和添加,更方便。 ) ?) \+ \* F. r8 p
" ^5 {% L# F0 a/ A本文选自代码云,谢谢!!!
! v% X' \$ m1 f( M) o" v: I4 u: M
) S! X/ c7 F3 G' |! i# Z" y; a5 G) |1 K5 _9 J
|