How to bootstrap Drupal

1
May 2012

How to bootstrap Drupal

Tags: bootsrap, Drupal
comment icon3 comment(s) |

Create a page outside of the CMS workflow that has access to Drupal's built-in functions.

There are scenarios, the deeper you get into Drupal, where you will need to access Drupal's database and functions, but outside of the scope of the CMS. For instance, you may need to create an AJAX callback that hooks into the database for a TinyMCE plugin, or you may be looking for ways to port over a custom CMS to Drupal… These are just a couple of reasons for having an easy way to hook into Drupal at the base level. And like with everything Drupal, there is a Drupal function for this: drupal_bootstrap($phase = NULL, $new_phase = TRUE) Before we show how to use this function in code, let's break it down a little.

$phase

Phase is a predefined constant that identifies which phase of Drupal to load. As the docs state "each phase adds to the previous one, so invoking a later phase automatically runs the earlier phases as well." The constants available for the $phase variable (in order) are:

DRUPAL_BOOTSTRAP_CONFIGURATION

Sets up the script environment and loads settings.php  

DRUPAL_BOOTSTRAP_PAGE_CACHE

Attempts to serve a page from the cache  

DRUPAL_BOOTSTRAP_DATABASE

Initializes the database system and registers autoload functions

DRUPAL_BOOTSTRAP_VARIABLES

Loads system variables and all enabled bootstrap modules  

DRUPAL_BOOTSTRAP_SESSION

Initialize session handling  

DRUPAL_BOOTSTRAP_PAGE_HEADER

Invokes hook_boot(), initializes locking system, and sends HTTP headers  

DRUPAL_BOOTSTRAP_LANGUAGE

Finds the language of the page  

DRUPAL_BOOTSTRAP_FULL

Drupal is fully loaded; validate and fix input data.

 

Remember, calling any of the phases automatically will load any preceding phases as well.

 

$new_phase

TRUE declares that drupal_boostrap() will run recursively, loading all previous phases up to the one we have chosen.

 

Now that we have the basics of the function out of the way, let's see how we can implement it. Create a custom page called test.php and insert the code below:

    // Get path of drupal install. 
    $drupal_path = $_SERVER['DOCUMENT_ROOT'];
    
    // Create a constant DRUPAL_ROOT that 
    // defines our path to the drupal install	
    define('DRUPAL_ROOT', $drupal_path);
    
    // We need to load the bootstrap.inc file so we can 
    // have access to the drupal_bootsrap() function
    require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
    
    // Bootstrap Drupal at the phase that you need
    drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    
    // Our custom page now has access to Drupal functions 
    // such as node_load() and db_query()  
    $node = node_load(1);

(As a side note, if all you need is to query the database, then setting your phase to DRUPAL_BOOTSTRAP_DATABASE will be much less costly in terms of load time).

That is basically all you need to tie Drupal into any custom page!

Subscribe to our Networks

Popular Tags by Javod

IW on Facebook

Comments

November 15, 2012

Angeline

is this website using

is this website using bootstrap?
http://eunited.com.my/

December 13, 2012

Anonymous

Thanks

This is exactly what I was looking for. Right to the point! Right on!

July 24, 2013

Preethi

Hi

I used the following query in php file inside my themes folder,to use the db_query to fetch some data from Database.

<?php
drupal_bootstrap
(DRUPAL_BOOTSTRAP_FULL);
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);

$sql = db_query("SELECT field_support_text_value FROM field_data_field_support_text");
$res = db_fetch_array($sql);
print
$res['field_support_text_value'];
?>

But its not retrieving any data.Any idea?

Search