Drupal hook_form_alter匿名用户看不到字段
我创建了一个用于更改表单的小模块,名为“form_mods”。我要更改的表单是“user_profile_form”。我添加了一个名为“个人资料”的额外字段类别。 我在 Drupal 管理中创建了一个名为“profile_state”的选择字段,并在我的模块中对其进行了更改,以拥有一个键 =>州的值列表,当我以管理员身份登录时,它对我有用,但尝试注册的匿名用户会看到一个空的州选择字段。这里有权限问题吗?我尝试添加“访问”=> user_access('访问内容') 到该字段,但这不起作用。这是我的代码:
function form_mods_form_alter($form, $form_state, $form_id) {
switch ($form_id) {
## user_profile_form ###################################################################################
case 'user_profile_form': // This is our form ID.
//echo '###'.$form['_category']['#value'].'###';
if($form['_category']['#value'] == 'Profile'){
// Modify the states dropdown list
$states = load_states_list();
$form['Profile']['profile_state'] = array(
'#type' => 'select',
'#title' => t('State'),
'#options' => $states,
'#required' => TRUE,
'#default_value' => isset($form['Profile']['profile_state']['#default_value']) ? $form['Profile']['profile_state']['#default_value'] : '',
'#element_validate' => array('profile_state_validate')
);
}
####################################################################################
break;
}
}
function load_states_list() {
$states = array('' => '-- Select a state'); // add a default empty value
$results = db_query("SELECT * FROM {states_list} ORDER BY name ASC");
while ($state = db_fetch_array($results)) {
$states[$state['code']] = $state['name'];
}
return $states;
}
谢谢
I created a small module for altering forms called "form_mods". The form I'm altering is the "user_profile_form". I added a category for extra fields called "Profile".
I created a select field in the Drupal admin called "profile_state" and I'm altering it in my module to have a key => value list of states and It's working for me when logged in as an admin but an anonymous user that's trying to register sees an empty states select field. Is there a permissions issue here? I tried to add 'access' => user_access('access content') to the field but that didn't work. Here is my code:
function form_mods_form_alter($form, $form_state, $form_id) {
switch ($form_id) {
## user_profile_form ###################################################################################
case 'user_profile_form': // This is our form ID.
//echo '###'.$form['_category']['#value'].'###';
if($form['_category']['#value'] == 'Profile'){
// Modify the states dropdown list
$states = load_states_list();
$form['Profile']['profile_state'] = array(
'#type' => 'select',
'#title' => t('State'),
'#options' => $states,
'#required' => TRUE,
'#default_value' => isset($form['Profile']['profile_state']['#default_value']) ? $form['Profile']['profile_state']['#default_value'] : '',
'#element_validate' => array('profile_state_validate')
);
}
####################################################################################
break;
}
}
function load_states_list() {
$states = array('' => '-- Select a state'); // add a default empty value
$results = db_query("SELECT * FROM {states_list} ORDER BY name ASC");
while ($state = db_fetch_array($results)) {
$states[$state['code']] = $state['name'];
}
return $states;
}
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,你确定你的函数曾经运行过吗?按照您的方式命名该函数,我认为不是。如果模块名称是“form_mods”,则应该调用您的函数
或者...因为您只修改 user_profile_form 表单,所以您可以使用函数名称
现在,它不起作用的原因是因为您没有的 &位于参数列表中$form 的前面。的&基本上将变量作为引用传递,因此对 $form 变量所做的任何更改都将被保存并传递回调用函数。因此,您的函数应该类似于
OR
参考: http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_form_alter/6
First of all, are you sure you're function ever gets run? Having the function named the way you do, I don't think it is. If the module name is "form_mods", your function should be called
Or...since you're only modifying the user_profile_form form, you could use the function name
Now, the reason it isn't working is because you don't have the & in front of the $form in the parameter list. The & basically passes the variable as reference, and so any changes you make to the $form variable will be saved and passed back to the calling function. So, your function should look like
OR
Reference: http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_form_alter/6
谢谢 mikesir87 提供的参考链接。我找到了我的问题。
有两种不同的表单正在使用我创建的这些字段。他们有不同的表单 ID。我必须寻找“user_profile_form”和“user_register”表单ID。
这是我有效的新代码:
谢谢
Thank you mikesir87 for the reference link. I figured out my problem.
There are 2 different forms that are using those fields I created. They have different form id's. I have to look for "user_profile_form" and "user_register" form id's.
Here is my new code that works:
thanks