【问题标题】:Using Drupal AJAX with Drupal Messages将 Drupal AJAX 与 Drupal 消息一起使用
【发布时间】:2013-11-14 00:20:54
【问题描述】:

我现在有一个自定义模块,以便使用 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 功能在单击按钮时发生 - 设置消息 - 而消息仅在页面重新加载后显示。我试图通过返回 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");
    });
  });
});

由于 Drupal 的本机 ajax.js 通过添加其 ajax-x 类来阻止它,所以不会发生 #edit-test-x 按钮上的单击事件绑定。

当目标如此简单时,这个问题却如此顽固且难以解决,这让我发疯了。我错过了什么? Google 搜索和 StackOverflow 浏览出人意料地毫无结果。

谢谢!

【问题讨论】:

    标签: php jquery ajax drupal drupal-7


    【解决方案1】:

    首先,这里有一些解释:
    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="the-error-container" 的 div。

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

    $commands = array();
    $commands[] = ajax_command_replace($selector, theme('status_messages'));
    return array(
      '#type' => 'ajax',
      '#commands' => $commands,
    );
    

    其中 $selector - 是您的消息区域的 css/jquery 选择器。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多