简体   繁体   English

Drupal 7.0,hook_form_alter()

[英]Drupal 7.0, hook_form_alter()

I have added a custom select list to the article/blog form in drupal. 我在drupal的文章/博客表单中添加了一个自定义选择列表。 I used the select list Field API to create a select list in the hook_form_alter(). 我使用选择列表字段API在hook_form_alter()中创建了一个选择列表。 I need a way to persist the value chosen for the select list, so when the user edits an article/blog they see the select list with the chosen/persisted value and not the default value. 我需要一种方法来保留为选择列表选择的值,因此,当用户编辑文章/博客时,他们会看到选择列表包含选择的/持久值而不是默认值。 I would appreciate some pointers/suggestions. 我将不胜感激一些指针/建议。

Thanks. 谢谢。

I'm sure there must be a reason you didn't just add a list field, but I would approach this by storing the data in the node itself. 我确定一定有一个原因,您不只是添加一个列表字段,而是通过将数据存储在节点本身中来解决这个问题。

Add a textfield to the node (or whatever is appropriate for your data type), then hide it either within hook_form_alter() by setting the type to hidden, or use a module like field permissions to only allow admins to access that field. 将文本字段添加到节点(或任何适合您的数据类型的文本字段),然后通过将类型设置为hidden将其隐藏在hook_form_alter()中,或使用诸如字段权限之类的模块仅允许管理员访问该字段。 (probably more overhead than you need if you're only using it for this one objective) (如果仅将其用于此目标,则可能会超出所需的开销)

Then use hook_node_presave() to take the value from your custom select field, and assign it to the hidden text field value. 然后,使用hook_node_presave()从自定义选择字段中获取值,并将其分配给隐藏的文本字段值。

Once it has been saved once, you can set the default_value of your select list to be the value of the hidden text field. 保存一次后,可以将选择列表的default_value设置为隐藏文本字段的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM