以编程方式获取和设置字段值
我想用完全相同的值填充两个字段;用户只需填写一项。
我还有一个函数可以检查第二个字段是否为空。 Drupal 6 和 Drupal 7 中字段值的获取和设置方式有什么变化吗?
编辑: 我现在正在尝试编辑模块。
是的,我说的是节点字段。
$node 数组只有我添加到节点的术语 ID。知道术语 ID 后,如何获取术语名称?
I have two fields I want to fill with the exactly same values; users should fill only one.
I also have a function which checks if the second field is empty. Is there any change in how the field values are obtained and set in Drupal 6, and Drupal 7?
EDIT:
I am trying to edit module right now.
Yes, I am talking about node fields.
$node array has only ID of terms I added to node. How do I get the term name, knowing its ID?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于您用 cck 标记了这个问题,我将假设您正在使用节点字段。
要将一个字段 (x) 的值复制到另一个字段 (y),您可以安装 计算字段 模块并对其进行设置,以便根据 x 的值计算 y 的值,或者您可以使用类似于以下挂钩的内容创建自定义模块:
此挂钩将所有数据从字段 x 复制到字段 y:
此挂钩仅复制第一个实例的值字段 x 到字段 y:
您可能希望在
$node->field_x
上执行print_r
并$node->field_y
因为您的数据结构可能会根据您使用的字段类型而有所不同。如果您想检查其中一个字段是否为空,您可以将赋值语句包装在调用自定义函数的条件中。Since you tagged this question with cck, I'm going to assume you are working with node fields.
To copy the value of one field (x) to another (y), you can either install the Computed Field module and set it up so that the value of y is computed from the value of x, or you can create a custom module with something similar to the following hooks:
This hook copies all of the data from field x to field y:
This hook only copies the value of the first instance of field x to field y:
You might want to do a
print_r
on$node->field_x
and$node->field_y
as the structure of your data may be different based on the type of field you are using. If you want to check if either of the fields are empty, you can wrap the assignment statement in a conditional that calls your custom function.查找字段值的一种好方法是使用 field_get_items() 由 Field API 提供。
其中:
$entity_type:类似于“节点”或“用户”,
$entity:是否需要其字段值的实体,
$field_name:字段的机器名称,
$langcode:实体存储的语言,它是可选的,如果没有提供,field_get_items会自动找到它。
One good way for finding out a field's value, is using field_get_items() which is provided by field API.
Where:
$entity_type: Is something like 'node' or 'user',
$entity: Is the entity which it's field value is needed,
$field_name: machine name of the field,
$langcode: The language that entity is stored in, It is optional and if not provided, field_get_items will find it out automatically.