我使用 hook_form_alter 向节点添加了一个字段,我可以看到它很好,但现在我当然希望在该字段中输入的数据也被保存。我该怎么做才能发生这种情况?
最佳答案
修改表单时,还需要在模块中添加表单提交处理程序;这样,您的模块将在提交表单时被调用,并且您可以保存您添加的字段的值。
如果有多个按钮,则最好将提交处理程序添加到您需要操作的特定按钮。例如,假设表单有两个提交按钮:“保存”和“删除”;如果您使用 $form[#submit][] = "my module_form_submit";
添加提交处理程序然后即使单击“删除”按钮也会调用提交处理程序。
如 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.
此外,The 'op' element in the form values is deprecated and should not be relied upon报告:
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.
关于Drupal 保存数据 hook_form_alter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7228483/