我有一个问题,多个AJAX请求修改drupal 8中的表单.
让我解释一下 - 我一直在尝试在drupal中构建一个测验模块,并决定使用一个小部件来创建可变数量的测验问题,在这个问题小部件中是一个可能的答案列表.我在我的问题小部件中创建了一个AJAX按钮,它允许删除答案并在第一次提交时工作,但由于某种原因第二次运行ajax调用时表单被重置(就像没有进行任何更改一样,并没有删除任何答案).在我取消设置答案之后,我已经调试了表单数组,以及第二次运行ajax回调时.
启用ajax的按钮使用默认的ajax replace方法,我尝试了在AJAX回调中返回表单(减去答案)的不同方法,包括简单地取消设置所选表单元素并返回表单,以及使用AjaxResponse和HtmlCommand类.我也尝试过重建表单,通过formbuilder和form_state,没有任何乐趣.此外,每个按钮和答案都有唯一的名称/ ID.
这是我的小部件代码(包括按钮定义):
<?php
/**
* @file
* Contains \Drupal\pp_quiz\Plugin\Field\FieldWidget\QuestionWidget.
*/
namespace Drupal\pp_quiz\Plugin\Field\FieldWidget;
use Drupal\pp_quiz\Controller\QuizController;
use Drupal\pp_quiz\Ajax\AjaxHandler;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Plugin implementation of the 'question_widget' widget
*
* @FieldWidget(
* id = "question_widget",
* label = @Translation("Quiz question widget"),
* field_types = {
* "quizquestion_type",
* },
* )
*/
class QuestionWidget extends WidgetBase
{
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface …
Run Code Online (Sandbox Code Playgroud)