Search code examples
phpajaxmoduledrupal-7

Drupal multiple ajax calls to a custom module


I have the following code in my custommodule_menu right now there is one ajax call to it. I want to add another and consequently create a new function (a new page callback) to handle it, how would i do this.

function a_b_menu() {
   $items['/a_b/email/%'] = array(
      'page callback' => 'a_b_ajax_email',
      'type' => MENU_CALLBACK,
      'page arguments' => array(1),
      'access arguments' => array('access content'),
      'access callback' => TRUE,

   );
   return $items;
}

Solution

  • You just need to do the following:

    $items['/a_b/email/%'] = array(
        'page callback' => 'a_b_ajax_email',
        'type' => MENU_CALLBACK,
        'page arguments' => array(1),
        'access arguments' => array('access content'),
        'access callback' => TRUE,
    
    );
    
    $items['/a_b/new_page/%'] = array(
        'page callback' => 'a_b_ajax_new_page',
        'type' => MENU_CALLBACK,
        'page arguments' => array(1),
        'access arguments' => array('access content'),
        'access callback' => TRUE,
    
    );
    

    Then since no file key is defined in the menus $items array for where the methods are stored, find a_b_ajax_email method in the same file and copy/paste this and rename to a_b_ajax_new_page. Then go ahead and change the functionality to suit your needs.