我现在有一个自定义模块,以便在Drupal消息中添加文本AJAX功能。该模块具有以下代码(test_error.module):
<?php
function test_error_user_view_alter(&$build) {
//_assassinations_player_profile($build);
}
function test_error_form_alter(&$form, &$form_state, $form_id) {
$form['test_error'] = array(
'#type' => 'button',
'#name' => 'Error',
'#value' => t('Error'),
'#ajax' => array(
'callback' => 'test_error_error',
'wrapper' => 'the-wrapper-div-field',
),
);
$form['test_warning'] = array(
'#type' => 'button',
'#name' => 'Error',
'#value' => t('Error'),
'#ajax' => array(
'callback' => 'test_error_warning',
'wrapper' => 'the-wrapper-div-field',
),
);
return $form;
}
function test_error_error() {
drupal_set_message("Header error", 'error');
}
function test_error_warning() {
drupal_set_message("Header warning", 'warning');
}然后,在page.tpl.php中,我有以下内容来输出$messages:
<div id="messages-wrap"><?php print $messages; ?></div>AJAX函数在单击按钮时发生-设置消息-而消息仅在页面重新加载后显示。我尝试通过return AJAX调用来实现,如下所示:
jQuery(document).ready( function(){
jQuery('.ajax-processed').each( function(){
jQuery(this).unbind();
});
jQuery('#edit-test-warning, #edit-test-error').click( function() {
var the_form = jQuery(this).closest('form');
var form_action = the_form.attr('action');
var details = the_form.serialize();
jQuery.ajax({
type: "POST",
url: form_action,
data: details
}).done( function(){
jQuery('#messages-wrap').load("/ #messages-wrap");
});
});
});
#edit-test-x按钮上的单击事件绑定从未发生过,因为Drupal的原生ajax.js通过添加它的ajax-x类来阻止它。
当目标是如此简单的事情时,这个问题如此持久,如此难以解决,这让我发疯。我遗漏了什么?谷歌搜索和StackOverflow浏览结果出人意料地毫无结果。
谢谢!
发布于 2013-11-14 19:02:23
首先,这里有一些解释:
简单情况下的代码示例:
function test_error_form(&$form, &$form_state) {
$form['test_error'] = array(
'#type' => 'button',
'#value' => t('Error'),
'#ajax' => array(
'callback' => 'test_error_error',
'wrapper' => 'the-error-container',
),
);
$form['test_warning'] = array(
'#type' => 'button',
'#value' => t('Error'),
'#ajax' => array(
'callback' => 'test_error_warning',
'wrapper' => 'the-error-container',
),
);
return $form;
}
function test_error_error($form) {
drupal_set_message("Header error", 'error');
return array(
'#type' => 'markup',
'#markup' => theme('status_messages'),
);
}
function test_error_warning($form) {
drupal_set_message("Header warning", 'warning');
return array(
'#type' => 'markup',
'#markup' => theme('status_messages'),
);
}请注意,在页面上必须有带有错误容器“id=-error-container”的div。
如果您希望在标准位置呈现新的drupal消息,请尝试使用ajax命令,如下所示:
$commands = array();
$commands[] = ajax_command_replace($selector, theme('status_messages'));
return array(
'#type' => 'ajax',
'#commands' => $commands,
);其中$selector -是消息区的css/jquery选择器。
请询问,如果有一些不清楚的地方。
https://stackoverflow.com/questions/19967180
复制相似问题