drupal默認用戶註冊表單中只有用戶名稱,賬號密碼,郵箱等字段,若是想對用戶作一些好的交互,必需要用到用戶一些稍微詳細的信息,而drupal的hook_user能夠很方便的讓咱們添加這些信息,好比個人站點要給用戶加入性別、詳細地址和我的簡介,咱們能夠實現user鉤子以下(個人模塊叫snippet):ios
注:本人對於字符串都沒有加t函數作翻譯,是爲了提升速度,須要的用戶能夠適當修改。函數
/**
*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’ => ‘現居住地址’,
);
$fields['personal_profile']['introduction'] = array(‘#title’ => ‘我的介紹’, ‘#type’ => ‘textarea’, ‘#rows’ => 5, ‘#cols’ => 50);
return $fields;
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,
);
$fields['personal_profile']['introduction'] = array(
‘#title’ => ‘我的介紹’,
‘#type’ => ‘textarea’,
‘#rows’ => 5,
‘#cols’ => 50,
‘#default_value’ => $user->introduction
);
return $fields;
}
}
翻譯
其中的register這個op控制用戶註冊表單,而form這個op控制用戶編輯本身我的資料的表單。form只是比register加入了一些默認值而已,而這些默認值是從登陸用戶的user對象中讀取的,很簡單吧。orm
最後,若是你只是一個drupal使用者,不妨能夠使用drupal內置的profile模塊,手動配置和添加,更方便。對象