我正在使用 Drupal 7 的表单 API 构建一个表单……到目前为止,一切都是犹太洁食;但看起来我撞到了墙。寻找正确方向的指导/点:
用户使用 CAS(中央认证服务)登录。单击表单以创建新记录。
在表单的顶部,我有一个复选框(默认选中),后跟三个字段:ID、姓名、电话和电子邮件。
我使用以下代码预填充这些字段:
<?php
//... other code prior to this function
function user_form_form_start($form, &$form_state) {
global $user;
$result = db_query('select u.name, dn.field_full_name_value, pn.field_phone_value, u.mail from field_data_field_full_name dn join field_data_field_phone pn on (dn.entity_id = pn.entity_id) join users u on (pn.entity_id = u.uid) where u.uid = :uid', array(':uid' => $user->uid))->fetchAll();
$myID = isset($result[0]->name) ? $result[0]->name : "";
$myFullName = isset($result[0]->field_full_name_value) ? $result[0]->field_full_name_value : "";
$myPhoneNumber = isset($result[0]->field_phone_value) ? substr($result[0]->field_phone_value, 2) : "";
$myEmailAddress = isset($result[0]->mail) ? $result[0]->mail : "";
$form['tab_user'] = array(
'#type' => 'fieldset',
'#title' => t('User Details'),
'#collapsible' => TRUE, // Added
'#collapsed' => FALSE, // Added
);
$form['tab_user']['user_sameAsAuthor'] = array(
'#type' => 'checkbox',
'#title' => t('Filling it out for myself'),
'#default_value' => isset($form_state['object']->user_sameAsAuthor) ? $form_state['object']->user_sameAsAuthor : TRUE,
// '#required' => TRUE,
);
$form['tab_user'] ['user_id'] = array(
'#type' => 'textfield',
'#title' => t('My CAS ID'),
'#default_value' => isset($form_state['object']->user_id) ? $form_state['object']->user_id : $myID,
// '#required' => TRUE,
);
$form['tab_user']['user_name'] = array(
'#type' => 'textfield',
'#title' => t('Name'),
'#default_value' => isset($form_state['object']->user_name) ? $form_state['object']->user_name : $myFullName,
// '#required' => TRUE,
);
$form['tab_user']['user_phone'] = array(
'#type' => 'textfield',
'#title' => t('Phone Number'),
'#default_value' => isset($form_state['object']->user_phone) ? $form_state['object']->user_phone : $myPhoneNumber,
'#attributes' => array('class' => array('phoneNumberField')),
'#size' => 20,
// '#required' => TRUE,
);
$form['tab_user']['user_email'] = array(
'#type' => 'textfield',
'#title' => t('E-mail'),
'#default_value' => isset($form_state['object']->user_email) ? $form_state['object']->user_email : $myEmailAddress,
// '#required' => TRUE,
);
return $form;
}
//... other code after this function
?>
以上所有的工作都很漂亮!这是我卡住的地方:
如果用户取消选中该复选框,我希望用户输入一个新 ID。基于任何 ID 用户输入,我如何“ajaxically”获取名称、电话和电子邮件进行 LDAP 查找?
非常感谢任何和所有帮助!
编辑:
道歉!忘了提到在我的研究中,我挖掘了表单 API(当然)并注意到textfield
(https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7.x#textfield)接受ajax
属性(https://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7.x#ajax)。但是,示例代码并没有完全回答我的问题。