6

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?

migrated from stackoverflow.com Mar 1 '12 at 15:26

This question came from our site for professional and enthusiast programmers.

  • 1
    To which Drupal version are you referring? Things changed a little, in Drupal 7. – kiamlaluno Mar 1 '12 at 21:50
7

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 want to retrieve nodes in AJAX, you can also use the Services module which provides JavaScript callable endpoints (REST, JSON over HTTP) for nodes, users, taxonomy terms, etc.

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.
3

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:

  1. Create a drupal views to show get articles body text as output (which can take nodeid as parameter)
  2. Use View Data source module http://drupal.org/project/views_datasource to output the view result in JSON format.

Now you can call this view(using view page URL) with required parameter to get article body in JSON format. That can be used by your javascript to place the content inside div.

Your Answer

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