簡體   English   中英

將Drupal AJAX與Drupal消息一起使用

[英]Using Drupal AJAX with Drupal Messages

我現在有一個自定義模塊,以便通過Drupal Messages向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按鈕上綁定click事件。

當目標如此簡單時,這個問題是如此的持久且難以解決,這令我感到瘋狂。 我想念什么? Google搜索和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'),
  );
}

請注意,頁面上的div必須為id =“ the-error-container”。

如果要在標准位置渲染新的drupal消息,請嘗試使用ajax命令,如下所示:

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

其中$ selector-是消息區域的CSS / jQuery選擇器。

請詢問是否有不清楚的地方。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM