不确定Drupal7中模板文件中使用的以下格式的问题所在。非常感谢帮助。问题如下:
1.变量$ title和$ surname不会传递给表单的默认值
=>错误消息:注意:未定义的变量:form_user_information中的标题()
=>错误消息:注意:未定义的变量:form_user_information()中的姓氏
2.有一个警告:strpos()期望参数1是字符串,在drupal_strip_dangerous_protocols()中给出的数组
提前谢谢。
<?php
//Load User data:
global $user;
$uid = $user->uid;
$account = user_load($uid);
//Get User data:
$title = 'Mrs.';
print $title . '<br><br>'; //Result: Value is printed and not empty!
$surname = check_plain($account->field_vorname['und']['0']['value']);
//$surname = 'Tom';
print $surname . '<br><br>'; //Result: Both values are printed and are not empty!
function form_user_information($form, &$form_state) {
//Form
$form['#action'][] = request_uri();
$form['#id'][] = 'form_user_information';
$form['#validate'][] = 'form_user_information_validators';
$form['#submit'][] = 'form_user_information_submit';
$form['#prefix'] = '<div id="form_user_information">';
$form['#suffix'] = '</div>';
//Select-Field: https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7#select
$form['Title'] = array(
'#type' => 'select',
'#title' => t('Title'),
'#options' => array(
'Frau' => t('Mr.'),
'Herr' => t('Mrs.'),
),
'#default_value' => $title,
);
$form['surname'] = array(
'#type' => 'textfield',
'#maxlength' => 50,
'#size' => 40,
'#required' => TRUE,
'#title' => t('Surname'),
//'#attributes' => array('placeholder' => $surname),
'#default_value' => $surname,
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array('#type' => 'submit', '#value' => 'Confirm data');
return $form;
}
//print form
$form = drupal_get_form('form_user_information');
print drupal_render($form);
//Form Validation:
function form_user_information_validators($form, &$form_state) {
if ($form_state['values']['surname'] == '') {
form_set_error('surname', t('Please enter your surname.'));
}
}
//Form Submit:
function form_user_information_submit($form, &$form_state) {
//...
}
//get form information
echo "<pre>".print_r($form,true)."</pre>";
?>
答案 0 :(得分:0)
1)使用全局范围设置全局变量$ title和$ surname:
//Get User data:
$global $title = ...
$global $surname = ...
否则将所有这些变量(包括$ user)设置在function form_user_information
内,这是最佳做法。
我还建议不要使用$ title作为变量名,因为它可能会导致$ page变量的已定义$ title的问题。而是使用$ user_title。
之类的东西2)这是来自哪条线?
答案 1 :(得分:0)