允许节点表单提交多次

时间:2012-05-15 14:45:44

标签: jquery ajax drupal

使用Drupal 7,我遇到了Drupal不允许两次提交节点表单的问题。场景:

  • 我显示一个节点表单,并使用Drupal的ajax框架通过use-ajax-submit功能处理提交。
  • 我第一次提交表单时,没问题。
  • 我第二次提交表单时收到以下消息:

    “此页面上的内容已被其他用户修改,或者您已使用此表单提交了修改。因此,您的更改无法保存。”

我理解这是预期的行为,我正试图找到解决方法。我似乎记得在Drupal 6中有一个表单属性,它可以在构建表单时允许多个提交,但是在Drupal 7中找不到任何关于它的信息。

会喜欢任何人可能要分享的建议。

3 个答案:

答案 0 :(得分:1)

我解决了这个问题!查看节点模块,结果node_validate根据form_state变量中的值检查上次提交的时间。我为表单编写了一个自定义验证处理程序,绕过node_validate函数并允许节点表单多次提交。

 /**
 * Sets up a node form so that it can be submitted more than once through an ajax interface
 * @param unknown_type $form
 * @param unknown_type $form_state
 */
function MYMODULE_allow_multisubmit (&$form, &$form_state){

  // set this as a custom submit handler within a form_alter function
  // set the changed value of the submission to be above the last updated time
  // to bypass checks in the node_validate
  $check = node_last_changed($form_state['values']['nid']);
  $form_state['values']['changed'] = $check + 120;

}

答案 1 :(得分:0)

hook_allow_multisubmit不会在drupal 7中出现

答案 2 :(得分:0)

这是我最近遇到的一个问题,所以我会添加一个更全面的' Mark Weitz的答案确实有效。

首先,您需要更改您需要输入的内容的节点表单

//Implements hook_form_alter()
function MYMODULE_form_alter(&$form, &$form_state, $form_id){

    //Check form is the node add/edit form for the required content type
    if($form_id == "MYCONTENTTYPE_node_form"){

        //Append our custom validation function to the forms validation array
        //Note; We must use array_unshift so our function is called first.
        array_unshift($form['#validate'], 'my_custom_validation_function');

    }

}

现在定义的是自定义验证功能,它将修复错误:

"此页面上的内容已被其他用户修改,或者您已使用此表单提交了修改。因此,您的更改无法保存。"

//Our custom validation function
function my_custom_validation_function($form, &$form_state){

    //Drupal somewhere in this validation chain will check our $form_state
    //variable for when it thinks the node in question was last changed,
    //it then determins when the node was actually changed and if the $form_state
    //value is less than the drupal value it throws the 'Cant edit' error.
    //To bypass this we must update our $form_state changed value to match the actual
    //last changed value. Simple stuff really...

    //Lets do the above ^^
    $form_state['values']['changed'] = node_last_changed($form_state['values']['nid']);

    //Any other extra validations you want to do go here.

}

显然,这并非没有风险,因为现在对于我们选择的内容类型,人们可以覆盖彼此的工作。假设他们正在同时编辑节点。

相关问题