Drupal保存数据hook_form_alter

发布于 2024-12-01 15:47:28 字数 85 浏览 0 评论 0原文

我使用 hook_form_alter 添加了一个字段到节点,我可以看到它很好,但现在我当然希望在该字段中输入的数据也被保存。我必须做什么才能发生这种情况?

I added a field to a node using hook_form_alter and i can see it fine but now i would like of course that the data enterd in that field is also saved. What do i have to do for this to happen?

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

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

发布评论

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

评论(1

欲拥i 2024-12-08 15:47:28

当您更改表单时,您还需要向模块添加表单提交处理程序;这样,您的模块将在提交表单时被调用,并且您可以保存您添加的字段的值。

如果有多个按钮,那么最好将提交处理程序添加到您需要执行操作的特定按钮。例如,假设表单有两个提交按钮:“保存”和“删除”;如果您使用 $form[#submit][] = "my module_form_submit"; 添加提交处理程序,那么即使单击“删除”按钮也会调用提交处理程序。

正如表单按钮可以定义自定义#submit 和#validate 处理程序中所述:

所有表单都可以具有“#validate”和“#submit”属性,其中包含用户提交数据时要执行的验证和提交处理程序列表。以前,如果表单具有多个提交按钮来启动不同的操作(例如,更新记录与删除记录),则需要检查传入的 $form_values['op'] 中单击按钮的名称,然后执行不同的代码基于其价值。

现在,如果需要,可以在每个单独的表单按钮上定义 #validate 和 #submit 属性。

当使用特定按钮提交表单时,将使用其验证和提交处理程序,而不是默认的表单级处理程序。如果在按钮级别未指定任何内容,则将使用表单级别处理程序。

此外,表单值中的“op”元素已弃用,不应依赖报告:

如上所述,每个按钮都可以具有与其关联的“#validate”和“#submit”功能。因此,应该有一个按钮用于提交表单并调用普通的 $form_id_validate$form_id_submit 处理程序。任何需要调用不同验证或提交功能的其他按钮都应具有按钮特定功能。另请注意,表单值中的“op”元素(对应于 Drupal 5.x 中表单中有多个表单时单击的按钮)不应再依赖,并且可能不存在。

When you alter the form, you need to also add a form submission handler to the module; in that way, your module would be invoked when the form is being submitted, and you can save the value of the field you added.

If there are more than one button, then it's preferable to add the submission handler to the specific button you need to act upon. Suppose, for example, that the form has two submission buttons: "Save" and "Delete"; if you add the submission handler with $form[#submit][] = "my module_form_submit"; then the submission handler is called even when the "Delete" button is clicked.

As reported in Form buttons can define custom #submit and #validate handlers:

All forms can have "#validate" and "#submit" properties containing lists of validation and submission handlers to be executed when a user submits data. Previously, if a form featured multiple submission buttons to initiate different actions (updating a record versus deleting, for example), it was necessary to check the incoming $form_values['op'] for the name of the clicked button, then execute different code based on its value.

Now, it is possible to define #validate and #submit properties on each individual form button if desired.

When a specific button is used to submit a form, its validation and submission handlers will be used rather than the default form-level ones. If none are specified at the button level, the form-level handlers will be used instead.

Additionally, The 'op' element in the form values is deprecated and should not be relied upon reports:

As discussed above, each button can have "#validate" and "#submit" functions associated with it. Thus, there should be one button that submits the form and which invokes the normal $form_id_validate and $form_id_submit handlers. Any additional buttons which need to invoke different validate or submit functionality should have button-specific functions. Note also that the 'op' element in the form values, corresponding to the button clicked when there are several in a form in Drupal 5.x, should no longer be relied upon and may not be present.

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