How to bootstrap Drupal from other applications?

Drupal bootstrap will load necessary commonly used functionality and it allows to bootstrap from other applications easily. Drupal allows to bootstrap its functionality by phase wise like basic initialization, caching, database layer, initialize variable system, etc.,. That means you can partially load Drupal and call necessary functionality required to your application.

In order to use any of the Drupal functionality from other applications, first you need to bootstrap Drupal; then start using Drupal functionality.

This article explains the necessary steps required to bootstrap Drupal.

This article applies to:

  • Drupal 7.x

Step (1). Prepare to call Drupal bootstrap.

  • Change current directory to the directory where Drupal installed.
  • Define DRUPAL_ROOT and point it to the directory where Drupal installed.

Step (2). Include necessary files.

  • Include .inc files. These files are located in includes folder. Make sure that include these files only once.

Step (3). Bootstrap Drupal.

  • Call drupal_bootstrap function with necessary arguments.
  • drupal_bootstrap function takes 2 arguments.
    • $phase – This is a constant value which takes what phase to bootstrap to. The following are the valid values:
    • DRUPAL_BOOTSTRAP_CONFIGURATION – Initializes configuration phase.
      DRUPAL_BOOTSTRAP_PAGE_CACHE – Caching phase.
      DRUPAL_BOOTSTRAP_DATABASE – Initializes the database layer phase.
      DRUPAL_BOOTSTRAP_VARIABLES – Initializes the variable system phase.
      DRUPAL_BOOTSTRAP_SESSION – Initializes session handling phase.
      DRUPAL_BOOTSTRAP_PAGE_HEADER – Sets up the page header phase.
      DRUPAL_BOOTSTRAP_LANGUAGE – Finds out the language of the page phase.
      DRUPAL_BOOTSTRAP_FULL – Fully loads Drupal. Validates and fixes input data.
    • $new_phase – It is a boolean value TRUE or FALSE to indicate calling drupal_bootstrap recursively.
  • This function returns the most recently completed phase.

Step (4). Change directory to your application’s directory.

Now you can call Drupal’s functions from your application. Altogether, below is the code.

<?php
	$var_cwd = getcwd();

	define('DRUPAL_ROOT', $_SERVER['DOCUMENT_ROOT']);

	chdir(DRUPAL_ROOT);

	include_once(DRUPAL_ROOT."/includes/bootstrap.inc");
	include_once(DRUPAL_ROOT."/includes/menu.inc");
	include_once(DRUPAL_ROOT."/includes/common.inc");
	include_once(DRUPAL_ROOT."/includes/theme.inc");    	

	drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);

	chdir($var_cwd);
?>

by Code Steps

Leave a Reply