How to bootstrap Drupal

Dev

How to bootstrap Drupal

by Javod Khalaj
//

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!

More About the Author

Javod Khalaj

Experience Architect
Cohort Analysis in Tableau: User Retention Given Only Created and Last Seen Dates Cohorts have been the go-to analysis for user retention for a while now, and Tableau has a great article on how to go about creating ...
Use DreamObjects to Backup Your MySQL Database (PHP Script) DreamObjects is DreamHost’s public cloud storage offering. It’s analogous to Amazon’s S3 offering, but slightly cheaper. The nice thing ...

See more from this author →

InterWorks uses cookies to allow us to better understand how the site is used. By continuing to use this site, you consent to this policy. Review Policy OK

×

Interworks GmbH
Ratinger Straße 9
40213 Düsseldorf
Germany
Geschäftsführer: Mel Stephenson

Kontaktaufnahme: markus@interworks.eu
Telefon: +49 (0)211 5408 5301

Amtsgericht Düsseldorf HRB 79752
UstldNr: DE 313 353 072

×

Love our blog? You should see our emails. Sign up for our newsletter!