Drupal 7 Line by Line

With the choice made I got working on a graphic design and thetask of decorating/styling/skinning (or what Drupal called theming)my website. The hacker in me didn't bother Googling for answers orreading documentation onhow I might theme the site. Ijust dove into the code.

Here is the code I dove into: index.php - as it was in the daysof Drupal 4.4:

<?php
include_once"includes/bootstrap.inc";
drupal_page_header();
include_once
"includes/common.inc";fix_gpc_magic();menu_build("system");

if (

menu_active_handler_exists ()) {
 
menu_execute_active_handler ();
}
else {
 
drupal_not_found ();
}
drupal_page_footer (); ?>

And that's how I became a Drupal developer. I went down therabbit hole. I began stepping through the code (without adeubugger!) trying to sort out what it was that Drupal was doing sothat I could find what I needed to change to make my site look theway I wanted to. By the time I found my answer I had learned moreabout Drupal than I needed or wanted to know at the time. I wasalso hooked.

And the rest, as they say, was history.

With the release of Drupal 7 I thought I would repeat theexercise. This time however I will document the experience to sharewith you. We can learn something new together.

Getting Started: Drupal 7 index.php

Here is index.php - as it is today:

<?php

define('DRUPAL_ROOT',getcwd());

require_once

DRUPAL_ROOT . '/includes/bootstrap.inc' ;
drupal_bootstrap ( DRUPAL_BOOTSTRAP_FULL );
menu_execute_active_handler ();
?>

The first thing you'll notice is that Drupal has a lot moredocumentation inside the code than it did 6 versions ago. This willmake stepping through and explaining the code line by line a littleeasier (since much of the work is already done). Its worth notingthat the code comments are in phpDoc format and are used togenerate the code documentation found athttp://api.drupal.org/

As you can see in the code comments index.php serves(nearly1) all page requests on yourwebsite. In other words, index.php is where code execution beginswhether you requesthttp://example.com/home orhttp://example.com/news/january/headlinesor http://example.com/admin/rule/the/world.

Yes. Four (4) lines of code return every single page on yourDrupal site! That's pretty amazing. So what's going on here.

A call is made to getcwd() whichreturns the current workingdirectory whichgets assigned to the DRUPAL_ROOTconstant.

Basically Drupal is telling itself which directory in theserver's file system it is installed and assigning that informationto a constant that will be used elsewhere in the code.

Using that constant Drupal then includes the file bootstrap.incfrom the includes directory usingrequire_once.

<?php
// $Id: bootstrap.inc,v 1.459 2010/12/30 04:35:00webchick Exp $

//3000 or so more lines of code
?>

Bootstrap.inc contains roughly 80 functions that "need to beloaded on every Drupal request". As soon as bootstrap.inc getsincluded 37 new constants get defined.

From those 80 or so functions and 37 or so new constants onlythe drupal_bootstrap function iscalled with the value of theDRUPAL_BOOTSTRAP_FULL constant as anargument.

The short version of what is happening in this one function callis Drupal is told to start up (bootstrap) all of its mechanismsrequired to handle the page request. It loads configurationinformation, page caching system(s), database(s), session handling,translation system, modules, and everything else you can imagine(and things you might not have imagined) that are required tohandle a page request.

The long version of what happens in this one function callis what many follow up posts in this series will be about. You'llhave to come back for those.

Finally, the last line in index.php calls the functionmenu_execute_active_handler(). Theshort version of what this function does is to act like a telephoneoperator and direct or route your call to the appropriate functionthat can answer you.

It takes a look at the path of the requested URL (i.e. the partsafter the domain name e.g. news/headlines/december) and figure outwhich function is responsible for handling requests for that path.Oncemenu_execute_active_handler()finds the function to call, it calls it. What happens at the pointdepends entirely on which function was called, but in most cases anhtml page is sent to the browser.

Once again the long version of what happens is left foranother time.

Summary

And there you have it: Four lines of code and only two functioncalls. All you need to get a page out of Drupal. Sounds easy.

Next time I'll start looking in depth at the bootstrap processin bootstrap.inc and thedrupal_bootstrap() function.

If you have any questions, suggestions or corrections just leavea comment. I truly appreciate the feedback.

footnotes

1) Isay nearly every page, because install.php, update.php exist andother special or custom parts of your site may be served in adifferent way.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值