I'm using drupal 7. I want to create a php script that uses Drupals DB abstraction layer to query the database and spit out JSON data. Where would I place such a script? At the moment, I've done it in a round-about way by create a new node, then in the function theme_process_page() of my theme/template.php, I wrote an IF statement to detect the node id, then run my code, then call a die() statement.

Is there a more appropriate place for me to put this script?

** additional notes ** This script is to be called every time a person presses "Provide a menu link" when adding/editing a node in the CMS. The script will then automatically select the appropriate Parent Item and weight based on any other existing translation.

  • Can you clarify a little more what is the purpose of your Ajax script ? When should it be called ? Is it after the insertion of a new node, before loading a node ?
    – tostinni
    Aug 28, 2011 at 19:03
  • @tostinni - ok more info added. Aug 28, 2011 at 19:48

1 Answer 1


The easiest is to use Views datasources if it can solve your problem, in essence this means that if you can use views to display the data you need, you can use this module to return it as JSON.

Another option is to write a custom module and use hook_menu to define a 'menu' item which you can use from the calling code. To return your data as JSON you can use the standard php functions.

If you need multiple scripts and advanced configuration you can always have a look at Services or


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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