Monday, 9 March 2015

Drupal Ajax Can't Find callback Function

  1. The issue is caused by the separation of form definition from module file
    in module file: 
    /**
     * Implements hook_block_view().
     */
    function urbansherpabooking_block_view($delta='') {
      $block = array();
      
      switch($delta) {
        case 'urbansherpabooking_forms' :
            drupal_add_js('https://maps.googleapis.com/maps/api/js?libraries=places');
            drupal_add_js(drupal_get_path('module', 'urbansherpabooking') . '/js/booking.js');
            $block['subject'] = "Urban Sherpa Booking";
            $block['content'] = drupal_get_form('form_urbansherpabooking_wizard');
            break;
        case 'urbansherpabooking_track' :
            drupal_add_js('https://maps.googleapis.com/maps/api/js?libraries=places');
            drupal_add_js(drupal_get_path('module', 'urbansherpabooking') . '/js/tracking.js');
            $block['subject'] = "Urban Sherpa Delivery Tracking";
            $variables = generate_variables_for_delivery_tracking_by_order_id();
            $block['content'] = theme('urbansherpabooking_track', $variables);
            break;
      }
      
      return $block;
    }
    in form inc file:
    //add pickup location form
        $form['pickup_details']['pickup_from_option'] = array(
          '#weight' => '0',
          '#required' => '1',
          '#key_type_toggled' => '0',
          '#type' => 'radios',
          '#options' => array(
            'enter' => t('Enter a location'),
            'history' => t('Choose from history locations'),
          ),
          '#default_value' => 'enter',
          '#title' => t('Pickup From'),
          '#ajax' => array(
            'callback' => 'ajax_urbansherpabooking_pickup_details_callback',
            'wrapper' => 'pickup-from-input-method-replace',
          ),
        );

  2. Install drupal ddl module(https://www.drupal.org/project/devel_debug_log) for debugging AJAX, cause dsm doesn't output anything for AJAX request.
  3. Go into drupal ajax handling file : includes/ajax.inc and find the function of ajax_form_callback. Figure out that the callback function can't be found by using ddl to debug. 
  4. Add loading inc files in module file and the issue is sorted.
    // load the inc files
    module_load_include('inc', 'urbansherpabooking', 'form_urbansherpabooking');
    module_load_include('inc', 'urbansherpabooking', 'tracking_urbansherpabooking');

No comments:

Post a Comment