Drupal 6:如何隐藏字段集以使其不显示在表单中?
我已启用“位置用户”模块,并且在用户注册期间我正在收集 2 个位置字段“国家/地区”和“邮政编码”。当我为用户注册表单设置主题时,我想删除围绕国家/地区和邮政编码元素设置的位置字段。
你能指导一下吗?
I have enabled Location User module and during user registration I am collecting 2 location fields Country and Postal Code. As I am theming user registration form, I want to remove the Location field-set around Country and Postal Code elements.
Could you please guide me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要更改内容类型表单上的位置字段集,您必须添加后构建,如下所示
,回调函数将如下所示:
To alter the location fieldset on content type forms you have to add an after build, like so
and the callback function would look something like this:
到目前为止,还没有直接的答案,因为位置表单结构与其他表单数组不同。基本上使用 drupal_render 函数,我在默认位置字段集之外填充了邮政编码和国家/地区表单元素。因此模板显示空位置字段集。
我使用肮脏的技术 - CSS hack 暂时隐藏字段集来纠正它,如下所示:
So far no straight answer for this as Location form structure is different from other form array. Basically using
drupal_render
function, I have populated postal code and country form element outside of the default Location field-set. So template was displaying empty Location field-set.I rectified it using dirty technique - CSS hack for time being hiding field-set as below:
对于 D6,最好的方法(不要使用
['#type'] = 'hidden'
; 或unset()
,这些方法会产生错误):在 form_alter 中:
By Css:
By Js:
不需要 id 元素。
For D6, the best way (don't use
['#type'] = 'hidden'
; orunset()
, these methods can produce bugs):In form_alter:
By Css:
By Js:
It isn't necessary a id element.
如果您想要做的只是隐藏视图中的字段,那么将它们设置为
display:none;
的简单 CSS 类就可以解决问题。如果您想实际更改表单中显示的字段(而不是简单地隐藏它们),您可以使用
form_alter
挂钩覆盖表单中的字段。form_alter
挂钩允许您覆盖任何表单的内容。例如,如果您想向“user_login”表单添加一个字段,您可以编写如下函数:然后,当显示用户登录表单时,其中将包含您的新字段。您可以在此
$form
数组中设置任意数量的字段。同样,您可以通过取消设置来从表单中删除字段,如下所示:
但是,您将面临的问题是,在 Drupal 6 中,
form_alter
挂钩仅适用于模块,不适用于主题。因此,您可能需要创建一个小模块来完成您需要的操作。我相信 Drupal 7 中已删除此限制。请参阅 Drupal Hook_Form_Alter 文档 了解更多详细信息。
您可能还想查看 此链接,这是 Drupal 错误单,他们在其中讨论了添加功能以允许 < code>form_alter 主题。您会注意到,它被标记为已修复,Drupal 版本号为 7。
您对该表单的另一个选择是在您的主题中为其编写自己的模板。使用此功能,您可以使用
drupal_render
单独输出表单中的每个字段/字段集。如果您需要额外的 HTML 代码等,这还允许您进行更复杂的主题。但是,这种方法充满了危险,因为如果稍后将任何其他字段添加到表单中,您的主题将无法应对它。通常不建议以这种方式手动呈现表单。希望有帮助。
If all you want to do is hide the fields from view, then a simple CSS class setting them to
display:none;
will do the trick.If you want to actually change the fields that are presented in the form (as opposed to simply hiding them), you can override the fields in a form using the
form_alter
hook.The
form_alter
hook allows you to override the contents of any form. For instance, if you want to add a field to the 'user_login' form, you could write a function like this:Then when the user login form is displayed, it will now have your new field in it. You can set as many fields as you like in this
$form
array.Likewise, you can remove a field from the form by unsetting it as follows:
The problem you will face, however, is that in Drupal 6, the
form_alter
hook is only available for modules, not themes. So you may need to create a small module to do what you need. I believe this limitation has been removed in Drupal 7.See the Drupal Hook_Form_Alter documentation for more detail.
You may also want to see this link which is the Drupal bug ticket where they discussed adding the feature to allow
form_alter
on themes. You'll note that it is marked as fixed with the Drupal version number of 7.Another option you have for the form is to write your own template for it in your theme. Using this, you can use
drupal_render
to output each field/fieldset in the form individually. This also allows you to do more complex theming if you want additional HTML code, etc. However, this method is fraught with danger, as if any other fields are added to the form later on, your theme will not be able to cope with it. Its generally not advised to render your form manually in this way.Hope that helps.