任务说明:
我需要通过url location hash打开模态窗口,即
www.example.com#overlay = my-multistep-form(与D7覆盖模块完全相同)。该窗口应包含多步骤ajaxified形式,
所以我用
Drupal.behaviors.rf_overlay = {
attach: function(context, settings) {
// get overlay url
if (location.hash.indexOf('#overlay') >= 0) {
var event = { data: {} };
// Special route for multistep forms
if (location.hash.indexOf('#overlay=become-a-sales-partner') >= 0) {
event.data.url = '/overlay/partner-form';
openMultistepFormModal(event);
}
}
function openMultistepFormModal(event) {
Drupal.ajax({
url: event.data.url,
success: function(response) {
var content = '';
for (var i in response) {
if (response[i].data != undefined) {
content += response[i].data;
}
}
$('.modal-body').empty();
var $overlayContent = $('<div>' + content + '</div>').appendTo('.modal-body');
Drupal.dialog($overlayContent, {});
$("#drupal-modal").addClass('overlay-' + event.data.url).modal();
}
}).execute();
}
}
};
我在/ overlay / partner-form路由中也有表单控制器,所以它根据指定的步骤返回表单数组
public function buildForm(array $form, FormStateInterface $form_state, array $options = array()) {
$step = in_array($form_state->get('step'), array(1, 2)) ? $form_state->get('step') : 1;
$form_state->set('step', $step);
switch ($step) {
case 1:
return $this->stepOnePerson($form, $form_state);
case 2:
return $this->stepTwoCompany($form, $form_state);
}
}
当我提交此表单时,它在ajax调用后不会加载第2步表单。
我的问题是:这个主题是否有一些很好的代码示例或教程?我做错了什么?