I'm trying to get the content of an article and display it using AJAX by replacing the content of a div with the new article's content. In order to do this I want to make an asynchronous jQuery call to a PHP page that simply outputs the article's body. However, this PHP function needs access to the Drupal PHP functions in order to operate properly. Where or how can I create a PHP file that will have access to these functions in a Drupal install?
To get data from Drupal's database and/or produced in PHP, you need a page callback (in a custom module) that output the data without the full layout rendering. This is easily doable by printing the content of the page directly in your page callback instead of returning it.
If you still want to work outside Drupal, in a standalone script, you should include Drupal's
include/bootstrap.inc and call
drupal_bootstrap() function to initialize Drupal environment (connect to database, load modules, session, etc.). You can select the phase to bootstrap depending on what you need from Drupal.
chdir('path/to/drupal/root'); require_once('includes/bootstrap.inc'); drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); // Drupal is fully loaded here, you can access everything.
If you want to get another drupal article's body text, you need not write custom php code in file.
You could do that using views may be in the following way:
- Create a drupal views to show get articles body text as output (which can take nodeid as parameter)
- Use View Data source module http://drupal.org/project/views_datasource to output the view result in JSON format.