带有自定义 ID 的 Drupal 表单
如果我错了,请纠正我,在阅读了 drupal fapi 相关文章后,我的印象是 fapi 自己生成“id”属性。它允许开发人员仅分配“名称”属性。如果是这种情况,有没有办法为元素设置所需的“id”值?因为,我希望我的元素具有有意义的“id”,以便 html/jquery 代码更易于阅读,并节省我的时间,无需通过已经编写的 jquery 代码来更改我在内部使用的所有“id”。
PS:drupal版本 - 6.x
Correct me if I'm wrong, after reading drupal fapi related articles, I got the impression that fapi generates 'id' attributes by itself. It allows developers to assign 'name' attribute only. If that's the case, is there a way I can set desire 'id' value for elements? Because, I want my elements to have meaningful 'id' so that html/jquery code would be easier to read as well as save my time from going through already written jquery code to change those all 'id's that I've used inside.
P.S:drupal version - 6.x
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好的找到了解决方案。我可以使用
$form
元素的#attributes
键来设置任何其他属性(例如 class、id 等)。感谢您迄今为止的帮助。Ok found the solution. I can use the
#attributes
key of the$form
element to set any additional attributes (such as class, id, etc.). Thanks for your help so far.我有一个类似的问题需要处理。我需要在同一页面上有多个表单,因此我必须更改表单及其元素的 id,以防止重复的 id。我做了类似以下的事情:
这基本上根据传入的 $cid 设置了唯一的 id。该代码还向表单中的每个元素添加了类,以便您可以轻松地对其进行样式设置。我确信可以有更强大的解决方案,但这是基本思想。在 Drupal 7 中测试。
I had a similar issue to deal with. I needed to have multiple forms on the same page so I had to change the ids of the form and its elements to prevent duplicate ids. I did something like the following:
This basically sets unique ids based on the $cid that is passed in. The code also adds classes to each element in the form so you can style it easily. I'm sure a more robust solution is possible but this is the basic idea. Tested in Drupal 7.
确实,您可以设置
$element['#attributes']['id']
并将其应用于表单字段。然而,它会破坏 Drupal 7 中的标签和#states,因为渲染管道的其余部分从其他地方读取 ID。因此,为了让您的标签和#states
继续工作,请使用将 ID 设置为$element['#id']
(这是一个未记录的属性,但表单 API 的方式是这样的)在内部监视 ID)。请务必通过
drupal_html_id
传递您的 ID,以确保不会发生冲突。It's true that you can set
$element['#attributes']['id']
and that will apply to the form field. However, it will break labels and#states
in Drupal 7 because the rest of the rendering pipeline reads the ID from somewhere else. So for your labels and#states
to keep working, use set the ID to$element['#id']
instead (an undocumented property that nonetheless is how the form API watches ID internally).Make sure to pass your ID through
drupal_html_id
as well to ensure no conflicts.这个问题实际上与 Drupal-FAPI 本身没有太大关系,而更多地与 Drupal 主题的形成方式(创建标记)有关。
如果您想要更改网站上的所有表单,您可以覆盖用于表单和不同类型表单字段的主题功能。
如果
如果您只想覆盖某些表单或表单字段,您可以在表单或元素上设置
#theme
属性,以更改应使用哪个函数来创建标记。This problem doesn't really have much to do with the Drupal-FAPI itself, but more with how Drupal theme forms (create the markup).
If you want to alter all forms on your site, you can overwrite the theming functions that is used for forms and the different type of form fields.
If you just want to overwrite some forms or form fields, you can set the
#theme
attribute on the form or an element, to change which function should be used for creating the markup.