Drupal - text_list 字段的动态选项

发布于 2024-10-12 08:18:39 字数 551 浏览 2 评论 0原文

我有一个自定义节点类型,我想要一个字段使用基于 list_text 的特殊组合框。当选择list_text类型时,通常可以输入可选文本的静态列表,但是,我希望该列表是动态的,即基于db_query的结果>。使用 Drupal 7 执行此操作的最佳方法是什么?

一个简单的说明示例:此自定义类型 X 的节点包含一个指向另一个节点的字段,因此每当创建类型 X 的节点时,我都需要一个包含所有其他节点的组合框。

(最好的解决方案是仅在节点创建期间显示组合框,而在编辑期间不再显示。但如果在编辑期间也显示组合框,我也可以接受它。)

我尝试自定义 options_select 通过定义我自己的数据类型并相应地实现 hook_options_list 。组合框在创建过程中以正确的值显示,但是,我无法保存它。我不知道那里出了什么问题,但在第一次提交时它会更改为不同的主题,当我再次尝试时,我得到了一个内部服务器错误。我为该字段定义全新的数据类型是否走在正确的轨道上?肯定有更简单的方法吗?

I have a custom node type for which I want to have a field that uses a special combobox based on list_text. When one chooses the type list_text it is normally possible to enter a static list of selectable texts, however, I want this list to be dynamic, i.e. based on the results of a db_query. What is the best way to do this using Drupal 7?

A simple example for clarification: A node of this custom type X contains a field that points to another node, so whenever a node of type X is created I want a combobox that contains all other nodes.

(Best solution would be to only display the combobox during node creation, and no longer during edit. But I could also live with it if the combobox was shown during the edit as well.)

I have tried to customize options_select by defining my own data type and implementing hook_options_list accordingly. The combobox was displayed during creation with the correct values, however, I could not save it.. I have no idea what went wrong there, but on the first submit it would change to a different theme, and when I tried again I got an internal server error. Am I on the right track at all with defining a completely new data type for the field? there surely must be a simpler way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天涯离梦残月幽梦 2024-10-19 08:18:39

你是对的,你不需要新的数据类型。这是关于如何执行此操作的一个很好的教程 。它不是专门针对 D7 的,但我没有看到太多仍然适用的内容。可能有更好的方法专门在 D7 中执行此操作,但如果是的话我也很想知道:)

You're right in that you don't need a new datatype. Here's a good tutorial on how to do this. It's not specifically for D7 but I didn't see much that wasn't still applicable. There may be a better way to do it in D7 specifically but I would love to know it too if so :)

羁拥 2024-10-19 08:18:39

allegroconmolto 链接的教程为我提供了正确的方法。谢谢你。

这是更简单的方法: 教程

基本上,正如我所假设的,它是一个常见的问题,因此现在 webform 模块中包含了一个简单的解决方案。它提供了一个hook_webform_select_options_info,可用于注册回调方法。每次显示 Web 表单的相应选项选择时都会调用回调方法,以便您可以轻松地使用 dbquery 或其他任何内容的结果填充它。效果就像一个魅力,几乎不需要时间来实施。

The tutorial linked by allegroconmolto sent me on the right way. Thanks for that.

Here's the simpler way of doing it: tutorial

Basically, it is, as I assumed, a common problem and hence a simple solution for it was included in the webform module by now. It provides a hook_webform_select_options_info which can be used to register a callback method. The callback method is then called each time a corresponding option select of a webform is shown, so that you can easily fill it with the results of a dbquery or anything else. Works like a charm and takes next to no time to implement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文