欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

drupal form alter field modidy order 修改表单字段顺序 有大用


通过  
$export
= kprint_r($form,true);
print_r($export);
可知是  weight 来 控制字段的显示顺序的

image.png



如何在用户注册表单中更改字段顺序

大家好!

我尝试更改用户注册表单的顺序。为此我创建了一个小模块,称之为reg_override并将一些代码放入...

function reg_override_form_alter(&$ form,&$ form_state,$ form_id){ 
if($ form_id =='user_register'){ 
$ form [additional_profile] ['profile_full_name'] [' #weight '] = 1; 
$ form [additional_profile] [profile_gender] ['#weight'] = 2; 
$ form [additional_profile] [profile_birthdate] ['#weight'] = 3; 
$ form [additional_profile] [profile_country] ['#weight'] = 4; 
$ form [additional_profile] [profile_city] ['#weight'] = 5; 
$ form [account] [mail] ['#weight'] = 6; 
$ form [account] [name] ['#weight'] = 7; 
$ form [account] [pass] ['#weight'] = 8; 
$ form [user_terms] ['#weight'] = 9; 

}

结果我获得了用户注册表单,但是'account'字段集比'additional_profile'字段集更高,尽管我为他设置了较小的权重。为什么?
另一个问题:
是否有可能重新排列字段集之间的字段,我的意思是使用类似的东西:

$ form [additional_profile] ['profile_full_name'] ['#weight'] = 1; 
$ form [account] [name] ['#weight'] = 2; <--- 
$ form [additional_profile] [profile_gender] ['#weight'] = 3; 
$ form [additional_profile] [profile_birthdate] ['#weight'] = 4; 
$ form [additional_profile] [profile_country] ['#weight'] = 5; 
$ form [account] [mail] ['#weight'] = 6; 
$ form [account] [pass] ['#weight'] = 7; 
$ form [additional_profile] [profile_city] ['#weight'] = 8; <--- 
$ form [user_terms] ['#weight'] = 9;

有可能这样做吗?

评论

nevets的头像

对于第一部分,您只更改了元素的权重,在这种情况下,元素相对于未更改权重的字段集。除非您想要更改字段集中字段的顺序,否则您只需要更改字段集的权重。

至于第二部分,也许吧。我说可能是因为字段集可以将'#tree'设置为TRUE,在这种情况下移入或移出元素会导致问题。

analogin的头像

嗯,是的,为fieldsets设置'#weight'参数确实改变了它们的顺序,但是
我尝试#tree = TRUE或FALSE - < - 它没有帮助我重新排列fieldsets之间的字段。
那么,这是否意味着我无法将一个字段从字段集移动到另一个字段集?因为在Drupal中没有它的机制?
我最好拒绝使用我自己的字段顺序的字段集....我不能这样做?

nevets的头像

关于'#TREE'的评论并不是建议你改变hook_form_alter(),因为这通常是一个坏主意。相反,它旨在向您展示如何确定在字段集之间移动字段是否有效,如果'#TREE'设置为TRUE,您将无法将元素移入或移出该字段集,否则它应该起作用。
请注意,如果要在字段集之间移动字段,则需要类似的内容

$form['some_other_fieldset']['some_field'] = $form['some_fieldset']['some_field'];
unset($form['some_fieldset']['some_field']);
analogin的头像

是的,它对我来说很好:)现在我可以在字段集之间重新排列字段。谢谢。


来自  https://www.drupal.org/forum/support/theme-development/2010-11-04/how-to-change-fields-order-in-user-register-form





How to change fields order in user register form

Hi all!

I try to change order of user registration form. For this I created small module, call it reg_override and put some code into...

function reg_override_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'user_register') {
$form[additional_profile]['profile_full_name']['#weight'] = 1;
$form[additional_profile][profile_gender]['#weight'] = 2;
$form[additional_profile][profile_birthdate]['#weight'] = 3;
$form[additional_profile][profile_country]['#weight'] = 4;
$form[additional_profile][profile_city]['#weight'] = 5;
$form[account][mail]['#weight'] = 6;
$form[account][name]['#weight'] = 7;
$form[account][pass]['#weight'] = 8;
$form[user_terms]['#weight'] = 9;
}
}

As a result I got user reg form, but 'account' fieldset is upper then 'additional_profile' fieldset, though I set lesser weight for him. Why is it?
And another question:
Is it possible to rearrange fields between fieldsets, I mean to use something like:

$form[additional_profile]['profile_full_name']['#weight'] = 1;
$form[account][name]['#weight'] = 2; <---
$form[additional_profile][profile_gender]['#weight'] = 3;
$form[additional_profile][profile_birthdate]['#weight'] = 4;
$form[additional_profile][profile_country]['#weight'] = 5;
$form[account][mail]['#weight'] = 6;
$form[account][pass]['#weight'] = 7;
$form[additional_profile][profile_city]['#weight'] = 8; <---
$form[user_terms]['#weight'] = 9;

is it possible to do like that?

Comments

nevets’s picture

For the first part, you only changed the weights for the elements which in this case are relative to the fieldsets which you did not change the weights for. Unless you want to change the order of the fields in the fieldsets you should only need to change the weights of the fieldsets.

As for the second part, maybe. I say maybe because fieldsets can have '#tree' set to TRUE in which case move elements in or out it would cause problems.

analogin’s picture

Well, yes, setting up '#weight' parameter to fieldsets really changed the order of them, BUT
I tried #tree = TRUE or FALSE - <- it does not helped me to rearrange fields between fieldsets.
So, does it mean that I can't move one field from a fieldset to another? Because there is no mechanism for it in Drupal?
I would better refuse from fieldsets at all in pleasure of using my own fields order.... and i can't do this?

nevets’s picture

The comment regarding '#TREE' was not to suggest you change in hook_form_alter() as that would be generally a bad idea. Instead it was meant to show you how to determine if moving a field between field sets would work, if '#TREE' is set to TRUE you will not be able to move elements in or out of that field set otherwise it should work.
Note if you want to move fields between field sets you need something like

$form['some_other_fieldset']['some_field'] = $form['some_fieldset']['some_field'];
unset($form['some_fieldset']['some_field']);
analogin’s picture

Yes, it worked fine for me :) Now I can rearrange fields between field sets. Thank you.

来自  https://www.drupal.org/forum/support/theme-development/2010-11-04/how-to-change-fields-order-in-user...





普通分类: