Drupal 7 AJAX 通过hook_menu调用

Drupal 7 AJAX Call via hook_menu

本文关键字:menu 调用 hook 通过 AJAX Drupal      更新时间:2023-09-26

因此,我正在尝试在Ubercart结帐页面上获取新窗格,以便在有人选择新的运输选项时进行更新。到目前为止,每次选择新的运输选项后刷新页面时,我都可以发生这种情况。下一步是通过AJAX使其工作。

我的问题是 ajax 命令不是从 AJAX 回调触发的。div 未更新。现在我只有一些简单的文字。稍后,我将添加我需要的实际表单信息,这将是一个完全不同的问题。但是,我什至无法让这个测试用例工作。

我正在为Drupal的Ubercart FedEx模块开发一项新功能。我已经为此工作了一段时间,但无济于事。我尝试了很多不同的东西,但没有一个有效。

要非常清楚...我知道 ajax 调用正在触发。我甚至可以将成功附加到 ajax $.get 调用并获得控制台输出并设置 drupal 变量。只有div 替换代码不起作用。这个拼图的所有其他部分都有效。

我进入 ajax 调用并返回 javascript。我甚至可以在 JS 中的 ajax 调用上附加一个成功函数,并在成功时获取控制台输出。javascript不是问题。

// Implements hook_menu()
function uc_fedex_menu() {
  $items = array();
  $items['uc_fedex_jquery_callback/%'] = array(
    'title' => 'My Custom Callback', 
    'description' => 'Listing of blogs.', 
    'page callback' => 'uc_fedex_calendar_jquery_callback', 
    'page arguments' => array(1),
    'access arguments' => array('access content'), 
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
  return $items;
}
// AJAX callback: Updates calendar
// THIS IS WHERE THE ERROR IS... the variable is set
// But the div content is never replaced.
function uc_fedex_calendar_jquery_callback($argument) {
  variable_set('uc_fedex_shipment_type',$argument);
  $commands[] = ajax_command_replace('#fromdate', "works");
  return array('#type' => 'ajax', '#commands' => $commands);
}
// Actual UI of Calendar Pane
function uc_fedex_uc_checkout_pane_shipdate($op, $order, $form = NULL, &$form_state = NULL) {
    switch ($op) {
    case 'view':
      // Check for shipping quote option without altering Ubercart Core.
      // The $.get line makes the hook_menu call which in turn
      // makes the call back to the above function that has the issue
      drupal_add_js("
      jQuery(function ($) {
      $(document).ready(function() {
      last = 'o';
          setInterval(function() {
           $('#quote input:radio:checked').each(function() {
              if($(this).val() != last){
                last = $(this).val()
                $.get('https://www.fdanconia.com/uc_fedex_jquery_callback/'+ $(this).val());
              }
           });
          }, 500);
        });
      });", 'inline');
      $contents['calendar'] = array(
        '#type' => 'textfield',
        '#title' => t('Choose Your Estimated Arrival Date'),
        '#default_value' => date('m/j/Y',$response[1]),
        '#prefix' => '<div id="fromdate">',
        '#suffix' => '</div>',
      );
      return array('description' => $description, 'contents' => $contents);
    }
}
// Implements hook_uc_checkout_pane().
function uc_fedex_uc_checkout_pane() {
  $panes['calendar'] = array(
    'callback' => 'uc_fedex_uc_checkout_pane_shipdate',
    'title' => t('Shipping Calendar'),
    'desc' => t('A calendar to allow customers to choose shipping date.'),
    'process' => TRUE,
  );
  return $panes;
}

关于 js 的说明,jQuery( function same_func(){} ) 相当于 $(document).ready( function same_func() {} )

因此,外部 jQuery() 调用将函数绑定到文档就绪。该函数在文档就绪时触发,并将另一个函数绑定到已触发的文档就绪。

大多数时候,在Drupal中,变量$是未附加的,所以你必须显式创建一个方法(函数),在jQuery中作为$传递。

(function($){ 
$ available in here
})(jQuery);
Think of the the above as: 
(function)(variables) where the function takes a variable $ .. 
Or you can think of it like this:
function test ($) {
  $(jquery magic).etc()'
}
test(jQuery);

.. 除了函数测试和函数调用包含在"相同"行中。

检查 firebug/console 以查看是否正在调用 $.get。

另一个调试是在uc_fedex_calendar_jquery_callback()中使用php的error_log('message')并观察apache错误.log。

我最终在响应回调中添加了我需要的参数,然后在原始调用的 AJAX 响应上处理它们。我只是使用 jQuery 手动使用更新的变量数据操作 DOM。我认为没有办法在不改变 Ubercart 核心的情况下做到这一点,这是不可接受的。

如果有人想要我用来解决这个问题的代码,只需询问并收到。(它有点长,但如果有人想要,我会在这里发布。