0

I am a bit confused on the appropriate way to generate a drupal page for handling ajax posts. For example I'm creating a custom module that has favorites buttons. When a user clicks the button the ajax request sends the data to example.com/ajaxscripts/process.php. I need to create a page at process.php to handle the request but it shouldn't generate the full drupal theme. Rather it should just have access to the core drupal information such as $user and drupal database functionality. How can I create such a page? It seems that tying to use hook_menu() would force a full page rendering.

0

In your hook_menu, create a menu item with 'type' => MENU_CALLBACK, like so:

$items['YOUR_MENU_PATH'] = array(
  'page callback' => 'YOUR_PAGE_CALLBACK',
  ...
  'type' => MENU_CALLBACK,
);

Then, in your page callback function, you can use the drupal_json_output($some_array) function to convert $some_array to json, and return it, all in one go.

I wouldn't use the .php extension in the menu path.

0

In my opinion, services module is the best fit for your use case. Just create appropriate resources and define end-points for various types of calls. A nice tutorial on creating a resource can be found here. Although it may seem daunting at first, its really easy to manage if you have multiple end points. And plus, you don't have to worry about security if you are using a standard drupal module for providing web services end-points.

From the Services module intro page:

Services module provides a standardized solution of integrating external applications with Drupal. Service callbacks may be used with multiple interfaces like REST, XMLRPC, JSON, JSON-RPC, SOAP, AMF, etc. This allows a Drupal site to provide web services via multiple interfaces while using the same callback code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.