客戶需求:
- 希望採用 email 作為使用者帳號,方便未來串接 OpenID 等會員認證方式。
- 因為採用 email 作為使用者帳號,希望取消原本的「會員帳號名稱」欄位。
系統背景狀況:
- 在一個 Drupal 7 環境中,已安裝正體中文語系與翻譯。
- 在一個 Drupal 7 環境中,已安裝模組「LoginToboggan」,並設定:
- 「Allow users to login using their e-mail address」:Enabled
- 有客製化翻譯部分 LoginToboggan 的前台用字語句。
練習一:觀察會員註冊表單 $form 的內容
- 檔案位置:該 Drupal 7 系統中所使用的 theme 目錄底下的 template.php
function [your_theme_name]_form_user_register_form_alter(&$form, &$form_state, $form_id) {
print_r($form);
}
然後到 Drupal 7 網站前端觀察這一頁「http://your_website_domain/user/register」,應該可以看到完整的 $form 內容,以下擷取一小段我的環境中的片段範例:
Array (
[#user] => stdClass Object (
[uid] => 0
[hostname] => xxx.xxx.xxx.xxx
[roles] => Array ( [1] => anonymous user )
[cache] => 0 )
[#user_category] => register
[#attached] => Array (
[library] => Array (
[0] => Array (
[0] => system
[1] => jquery.cookie ) ) )
[#attributes] => Array (
[class] => Array (
[0] => user-info-from-cookie ) )
[#validate] => Array (
[0] => user_account_form_validate
[1] => user_validate_picture
[2] => user_register_validate
[3] => logintoboggan_user_register_validate )
[account] => Array (
[#weight] => -10
[name] => Array (
[#type] => textfield
[#title] => 使用者名稱
[#maxlength] => 60
[#description] => 可以使用空格; 除了半形句點、連結線和底線 (. - _) 之外,不可使用其他符號。
[#required] => 1
[#attributes] => Array (
[class] => Array (
[0] => username ) )
[#default_value] =>
[#access] => 1
[#weight] => -30 )
[mail] => Array (
[#type] => textfield
[#title] => 電子郵件地址
[#maxlength] => 254
[#description] => 一個有效的電子郵件地址,所有由系統發出的電子郵件將往此地址。此電子郵件地址位址不會被公開,並且只在您想要申請新的密碼,或是接收某些新聞或通知時,我們才會用此電子郵件地址寄信給您。
[#required] => 1
[#default_value] =>
[#weight] => -29 )
[pass] => Array (
[#type] => password_confirm
[#size] => 25
[#description] => 為新帳號設定密碼。請輸入兩次密碼。 密碼長度至少需有 6 個字元。
[#required] => 1 )
這當中,我們可以觀察到「account」 有兩個屬性「required」和「access」是可以動手腳來達成我們所需要的目的。嘗試看看在 theme 目錄底下的 template.php 中做些調整:
function [your_theme_name]_form_user_register_form_alter(&$form, &$form_state, $form_id) {
$form['account']['name']['#access'] = FALSE;
$form['account']['name']['#required'] = FALSE;
}
9/26: 看來還需要處理 form submit 之後。目前暫時解法是把 email 中的 username 字段直接送到 account name 欄位去,以避開 form validate。再找看看有沒有比較漂亮的解法。
Brought to you by

0 Comments:
Post a Comment