Php 在Drupal消息中使用DrupalAjax

Php 在Drupal消息中使用DrupalAjax,php,jquery,ajax,drupal,drupal-7,Php,Jquery,Ajax,Drupal,Drupal 7,我现在有一个自定义模块,用于将AJAX功能与Drupal消息文本化。模块具有以下代码(test_error.module): 在#edit-test-x按钮上绑定click事件从未发生,因为Drupal的原生ajax.js通过添加其ajax-x类来阻止它 当目标如此简单的时候,这个问题如此顽固,如此难以解决,这让我抓狂。我错过了什么?谷歌搜索和StackOverflow浏览出人意料地毫无结果 谢谢 首先,这里有一些解释: 1.因此,如果您试图将drupal消息作为表单的一部分呈现,则必须手动呈

我现在有一个自定义模块,用于将AJAX功能与Drupal消息文本化。模块具有以下代码(test_error.module):

在#edit-test-x按钮上绑定click事件从未发生,因为Drupal的原生ajax.js通过添加其ajax-x类来阻止它

当目标如此简单的时候,这个问题如此顽固,如此难以解决,这让我抓狂。我错过了什么?谷歌搜索和StackOverflow浏览出人意料地毫无结果


谢谢

首先,这里有一些解释:
1.因此,如果您试图将drupal消息作为表单的一部分呈现,则必须手动呈现drupal消息。
2.使用“#ajax”键,不需要额外的(自定义)JS。
3.单击按钮,表单将被重建,因此,您只需将消息呈现代码放入表单逻辑中即可。

简单案例的代码示例:

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为=“错误容器”的div

如果要在标准位置上呈现新的drupal消息,请尝试使用ajax命令,如下所示:

$commands = array();
$commands[] = ajax_command_replace($selector, theme('status_messages'));
return array(
  '#type' => 'ajax',
  '#commands' => $commands,
);
其中$selector-是消息区域的css/jquery选择器


如果有不清楚的地方,请询问。

首先,这里有一些解释:
1.因此,如果您试图将drupal消息作为表单的一部分呈现,则必须手动呈现drupal消息。
2.使用“#ajax”键,不需要额外的(自定义)JS。
3.单击按钮,表单将被重建,因此,您只需将消息呈现代码放入表单逻辑中即可。

简单案例的代码示例:

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为=“错误容器”的div

如果要在标准位置上呈现新的drupal消息,请尝试使用ajax命令,如下所示:

$commands = array();
$commands[] = ajax_command_replace($selector, theme('status_messages'));
return array(
  '#type' => 'ajax',
  '#commands' => $commands,
);
其中$selector-是消息区域的css/jquery选择器


如果有不清楚的地方,请询问。

首先,这里有一些解释:
1.因此,如果您试图将drupal消息作为表单的一部分呈现,则必须手动呈现drupal消息。
2.使用“#ajax”键,不需要额外的(自定义)JS。
3.单击按钮,表单将被重建,因此,您只需将消息呈现代码放入表单逻辑中即可。

简单案例的代码示例:

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为=“错误容器”的div

如果要在标准位置上呈现新的drupal消息,请尝试使用ajax命令,如下所示:

$commands = array();
$commands[] = ajax_command_replace($selector, theme('status_messages'));
return array(
  '#type' => 'ajax',
  '#commands' => $commands,
);
其中$selector-是消息区域的css/jquery选择器


如果有不清楚的地方,请询问。

首先,这里有一些解释:
1.因此,如果您试图将drupal消息作为表单的一部分呈现,则必须手动呈现drupal消息。
2.使用“#ajax”键,不需要额外的(自定义)JS。
3.单击按钮,表单将被重建,因此,您只需将消息呈现代码放入表单逻辑中即可。

简单案例的代码示例:

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为=“错误容器”的div

如果要在标准位置上呈现新的drupal消息,请尝试使用ajax命令,如下所示:

$commands = array();
$commands[] = ajax_command_replace($selector, theme('status_messages'));
return array(
  '#type' => 'ajax',
  '#commands' => $commands,
);
其中$selector-是消息区域的css/jquery选择器

如果有不清楚的地方,请询问