How Drupal works? [closed] - php

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Could someone provide a architectural overview of the Drupal 7 control flow? Perhaps in the sense of a flowchart about how a page gets generated. What additional resources would you suggest consulting with regards to how Drupal works?

Drupal can be confusing on this front, partially because it has a relatively deep function stack. Although it's procedural PHP it's purely event/listener driven in its architecture, and there's no simple "flow" in the main PHP script for you to look though. I recently did a presentation on this very subject, and the slides are posted on slideshare, but a quick high-level summary may be useful.
Drupal's index.php file functions as a frontside controller. All page are piped through it, and the "actual" url/path the user requested is passed to index.php as a parameter.
Drupal's path router system (MenuAPI) is used to match the requested path to a given plugin module. That plugin module is responsible for building the "primary content" of the page.
Once the primary page content is built, index.php calls theme('page', $content), which hands off the content to Drupal's theming/skinning system. There, it's wrapped in sidebars/headers/widgets/etc..
The rendered page is then handed back to apache and it gets sent back to the user's browser.
During that entire process, Drupal and third-party plugin modules are firing off events, and listening for them to respond. Drupal calls this the 'hook' system, and it's implemented using function naming conventions. The 'blog' module, for example, can intercept 'user' related by implementing a function named blog_user(). In Drupal parlance, that's called hook_user().
It's a bit clunky, but due to a PHP quirk (it keeps an internal hashtable of all loaded functions), it allows Drupal to quickly check for listeners just by iterating over a list of installed plugins. For each plugin it can call function_exists() on the appropriately named pattern, and call the function if it exists. ("I'm firing the 'login' event. Does 'mymodule_login' function exist? I'll call it. Does 'yourmodule_login' exist? No? How about 'nextmodule_login'?" etc.) Again, a touch clunky but it works pretty well.
Everything that happens in Drupal happens because of one of those events being fired. The MenuAPI only knows about what urls/paths are handled by different plugin modules because it fires the 'menu' event (hook_menu) and gathers up all the metadata plugin modules respond with. ("I'll take care of the url 'news/recent', and here's the function to call when that page needs to be built...") Content only gets saved because Drupal's FormAPI is responsible for building a page, and fires the 'a form was submitted' event for a module to respond to. Hourly maintenance happens because hook_cron() is triggered, and any module with mymodulename_cron() as a function name will have its function called.
Everything else is ultimately just details -- important details, but variations on that theme. index.php is the controller, the menu system determins what the "current page" is, and lots of events get fired in the process of building that page. Plugin modules can hook into those events and change the workflow/supply additional information/etc. That's also part of the reason so many Drupal resources focus on making modules. Without modules, Drupal doesn't actually DO anything other than say, 'Someone asked for a page! Does it exist? No? OK, I'll serve up a 404.'

To understand how Drupal works, you need to understand Drupal's page serving mechanism.
In short, all the calls/urls/requests are served by index.php which loads Drupal by including various include files/modules and then calling the appropriate function, defined in module, to serve the request/url.
Here is the extract from the book, Pro Drupal Development, which explains the Drupal's bootstrap process,
The Bootstrap Process
Drupal bootstraps itself on every request by going through a series of bootstrap phases. These phases are defined in bootstrap.inc and proceed as described in the following sections.
Initialize Configuration
This phase populates Drupal’s internal configuration array and establishes the base URL
($base_url) of the site. The settings.php file is parsed via include_once(), and any variable or string overrides established there are applied. See the “Variable Overrides” and “String Overrides” sections of the file sites/all/default/default.settings.php for details.
Early Page Cache
In situations requiring a high level of scalability, a caching system may need to be
invoked before a database connection is even attempted. The early page cache phase lets
you include (with include()) a PHP file containing a function called page_cache_
fastpath(), which takes over and returns content to the browser. The early page cache
is enabled by setting the page_cache_fastpath variable to TRUE, and the file to be included
is defined by setting the cache_inc variable to the file’s path. See the chapter on caching
for an example.
Initialize Database
During the database phase, the type of database is determined, and an initial connection is
made that will be used for database queries.
Hostname/IP-Based Access Control
Drupal allows the banning of hosts on a per-hostname/IP address basis. In the access control
phase, a quick check is made to see if the request is coming from a banned host; if so,
access is denied.
Initialize Session Handling
Drupal takes advantage of PHP’s built-in session handling but overrides some of the handlers
with its own to implement database-backed session handling. Sessions are initialized
or reestablished in the session phase. The global $user object representing the current user
is also initialized here, though for efficiency not all properties are available (they are added by an explicit call to the user_load() function when needed).
Late Page Cache
In the late page cache phase, Drupal loads enough supporting code to determine whether or
not to serve a page from the page cache. This includes merging settings from the database into the array that was created during the initialize configuration phase and loading or parsing module code. If the session indicates that the request was issued by an anonymous user and page caching is enabled, the page is returned from the cache and execution stops.
Language Determination
At the language determination phase, Drupal’s multilingual support is initialized and a decision is made as to which language will be used to serve the current page based on site and user settings. Drupal supports several alternatives for determining language support, such as path prefix and domain-level language negotiation.
Path
At the path phase, code that handles paths and path aliasing is loaded. This phase enables
human-readable URLs to be resolved and handles internal Drupal path caching and
lookups.
Full
This phase completes the bootstrap process by loading a library of common functions, theme
support, and support for callback mapping, file handling, Unicode, PHP image toolkits, form
creation and processing, mail handling, automatically sortable tables, and result set paging. Drupal’s custom error handler is set, and all enabled modules are loaded. Finally, Drupal fires the init hook, so that modules have an opportunity to be notified before official processing of the request begins.
Once Drupal has completed bootstrapping, all components of the framework are available.
It is time to take the browser’s request and hand it off to the PHP function that will
handle it. The mapping between URLs and functions that handle them is accomplished using
a callback registry that takes care of both URL mapping and access control. Modules register
their callbacks using the menu hook (for more details, see Chapter 4).
When Drupal has determined that there exists a callback to which the URL of the browser
request successfully maps and that the user has permission to access that callback, control is handed to the callback function.
Processing a Request
The callback function does whatever work is required to process and accumulate data needed to fulfill the request. For example, if a request for content such as http://example.com/
q=node/3 is received, the URL is mapped to the function node_page_view() in node.module.
Further processing will retrieve the data for that node from the database and put it into a data structure. Then, it’s time for theming.
Theming the Data
Theming involves transforming the data that has been retrieved, manipulated, or created
into HTML (or XML or other output format). Drupal will use the theme the administrator
has selected to give the web page the correct look and feel. The resulting output is then sent to the web browser (or other HTTP client).

Eaton's answer provides a good overview. (I'm new here so I can't mod him up, thus the comment.)
The brutal "aha" moment for me was realizing everything happens through index.php, and then through the waterfall of modules (core first, then by site). To extend core functionality don't rewrite it. Instead copy the module into /sites/all/modules/ or /sites/[yoursite]/modules and extend THAT, or create a new module in those places. Same for themes. Module directories can contain display code as well, in the form of tpl, css etc.
If you're used to stricter MVC type frameworks like Rails, Django etc. all this gets a little confusing. Modules can mix in a lot of display code, and if you're looking at someone else's modules or templates you'll eventually wind up walking backwards through the stack. That's the beauty/pain of working in PHP.
Ironically, "just build an app" might be the worst way to learn this. Drupal does so much out of the box that's simply obscure until you figure out the control flow. There's nothing in a tpl file that tells you where a function with a fun name like l() comes from, for example.

It depends on how deep an understanding you're looking for; if you have a good knowledge of php, I would suggest reading through the code itself, starting with index.php, and then going on to the includes/bootstrap.inc, and then some of the other scripts in that directory.
The key include files:
menu.inc is very important to understanding how the overall system works, as it handles a lot of the implicit mapping of URLs to content.
common.inc has most of the otherwise-mysterious functions that form the basis of the API.
module.inc handles the hook invocations that Eaton mentioned
form.inc deals with form display, submission and processing
theme.inc handles presentation.
There's also some key functionality in the modules/ directory; in particular, modules/node/node.module forms the basis of the node system, which is in general what's used to encapsulate site content.
The code is, in general, very well-commented and clear. The use of Doxygen markup within the commenting means that the code effectively is the canonical documentation.
It also helps to do this using an editor that can quickly jump to the definition of a function. Using vim in combination with ctags works for me; you do have to tell ctags to index .inc, .module, etc. files as php files.

This (for Drupal 6) & this (for Drupal 7) is a pretty good architectural overview of drupal. If you want more detail then I would start writing something most of the documentation is good. Trying to learn it at a high level of detail without something concrete to achieve will be much more difficult that trying something out.

I learned loads by importing the drupal .php code into a NetBeans project.
You can then run the netbeans debugger and watch the different phases of the page come together.

The best books on the subject are "Pro Drupal Development" and "Using Drupal."
"Pro Drupal Development" includes several nice flowcharts and thorough summaries of each of Drupal's APIs (forms, theming, etc.). It's intended to be especially instructive to people making their own modules and themes, but has lots of value to the average PHP-savvy developer who wants to understand Drupal. Besides which, I've created a custom module for every site I've built, just to gain the extra control over things like selectively hiding fields on various forms (which you generally want to do for the sake of simplifying node forms for end-users), so it's good to have this knowledge under your hat.
"Using Drupal" is aimed at the site developer who wants to know how to build the good stuff like galleries, blogs, and social networking sites. It goes through several use-cases and shows how to configure existing modules to do each job. In the process it familiarizes you with the essential add-on modules "Content Construction Kit" (CCK) and "Views," how to make custom blocks and templates, and the ins-and-outs of maintaining a Drupal site. I recommend this book especially for those who want to get up to speed and actually USE Drupal right away. In the process you gain an understanding of the internal organization of Drupal.

New contributor here, 2 years late on the conversation ;-)
Replying to https://stackoverflow.com/a/1070325/1154755
To extend core functionality don't rewrite it. Instead copy the module
into /sites/all/modules/ or /sites/[yoursite]/modules and extend
THAT, or create a new module in those places. Same for themes.
Actually, I never had to copy a core module to update it. Drupal Hooks should be all you need.
For themes, yeah, sometimes it's the only way to go, but often, you can build a subtheme to get the result you need.

Related

How to avoid bloat in Zend_Navigation?

I've been researching using Zend_Navigation in combination with Zend_Acl to manage navigation and access permissions in a new app I'm working on.
One thing that really bothers me, is the examples I have seen end up making an enormous XML file that contains every possible nav item in the application. Loading this file on every request seems like major performance bottleneck and there has to be a better way. I realize I could alleviate much of that with the use of memcached or another caching mechanism, but I feel like the application itself should be written in the most optimal way and, only then, do you add caching. It doesn't make sense to make something slow and bloated and rely on caching to clean up my dirty work.
I'm using a modular setup in this ZF app, so each module has a unique bootstrap. I've considered creating module specific nav XML files and loading the specific one, but I'm not sure if that's the best way either.
What is the suggested method of using Zend_Navigation in large application with potentially hundreds of navigation paths?
I wouldn't call this the suggested method it's just how I work with it when I basically had the same question two years ago. In a nutshell: I have all the paths in the XML I need on each and every single page. All other paths are added at run time. Only then I add the ACL.
First, be aware that ACL in Zend_Navigation only manages the presentation of the navigation. It doesn't provide or better guarantee access control to your application. A certain link will be missing in the menu but if the user knows the correct path s/he may access the resource nevertheless. Of course, you can use the ACL information in the navigation object to bolt down your application but I believe there are smarter ways mainly to incorporate ACL directly into controllers and models.
Second and to your main question, my navigation's XML file only contains the most basic structure up to the second level which is the menu I always need. It's also more or less what I have controllers and actions for. Any paths resulting from params are added at run time. Because of that I don't even include the ACL into the XML but inject it at run time. Simply because only then do I have the full extended current branch with all its paths.
I've not used XML to generate navigation. It is possible to add your pages at run-time in php using array notation.
Using modules, you could set up the navigation object in the registry, in each module have a model that adds its pages, and a plugin that calls each modules' navigation model at preDispatch.

Codeigniter hooks

Anybody knows reasonable hooks usage? I wrote 2 projects and have no idea what the used for.
Thanks
Hooks in CodeIgniter are used to extend or override the core functionality -- for example:
EXTEND:
If you want to add some basic analytics to your page you might add a pre_system and a post_system hook to log the length of time the request took (or at least, how long CodeIgniter took to process the request) and to record the requested URL, the user, and the time. (The first part of this hook series could be handled better by the Benchmark class, since it is already loaded).
OVERRIDE:
From the documentation:
cache_override
Enables you to call your own function instead of the _display_cache() function in the output class. This permits you to use your own cache display mechanism.
From CodeIgniter User Guide Version 2.1.4
CodeIgniter's Hooks feature provides a means to tap into and modify the inner workings of the framework without hacking the core files. When CodeIgniter runs it follows a specific execution process, diagramed in the Application Flow page. There may be instances, however, where you'd like to cause some action to take place at a particular stage in the execution process. For example, you might want to run a script right before your controllers get loaded, or right after, or you might want to trigger one of your own scripts in some other location.
Not sure what you consider "reasonable" though, but the above summarizes it pretty well. They allow you to add additional behavior to core library classes at various points in the execution cycle.

Difference between Module and Component in Joomla

Can somebody please tell me what is the basic difference between the module and component in Joomla?
If possible, please tell with some examples, so that it is easily understood.
Modules are usually small pieces of functionality designed to _present information in your site). They can appear a number of times, on a number of pages in various positions.
On the other hand, a component is typically more complex, with extensive functionality and capabilities. A component can only be displayed in the main area of a page, and can usually only be displayed in a single page.
Read this article for more info:
http://www.dart-creations.com/joomla/joomla-tutorials/the-difference-between-modules-and-components.html
Taken from http://forum.joomla.org/viewtopic.php?t=344599#p1485432 by David Hurley.
When I'm working with clients here is how I typically explain the differences.
A plugin will manipulate output already generated by the system. It typically does not run as a stand-alone piece, but takes data from other sources (i.e. the content) and manipulates it before outputting to the user window. A plugin typically does not display directly to a user, but does its work behind the scenes.
A module is typically considered to be an add-on to the site that extends the functionality of another part of the system. It usually has fewer options that are configurable by the end user and most of the time does not handle any storing of information into the database. A module usually occupies a secondary portion of the web page and is not considered the primary focus of a page.
A component is the most extensive add-on. This typically adds completely new, or different, functionality to your site and extends the overall site possibilities. A component handles data manipulation as well as input and storage into the database. A component on most sites occupies the main area of the website layout and is the primary focus of the page.
These are just generalizations and there are exceptions to every rule, but this should give you a good starting point.
As far as I can see, any reason for distinguishing between components and modules is hidden in the technique of the Joomla framework. The fact that the main editorial content of a page is provided by a component, whereas subsidiary information (side frames, headers and footers, etc.) are provided by modules is not a real difference as far as the user is concerned. Page content is page content - full stop!
I think it is misleading to confuse users by stressing this difference. The way modules associate with menu items is probably technically different to the way the components do. Components are associated with a page on a one to one basis and they are run by triggering a menu item. Also modules do not always need to access the database whereas this is an integral part of a component. I expect these technical differences ultimately explain why Joomla has built the distinction in the the user interface. Perhaps avoiding this distinction would be the basis for Joomla version 4 - but I suspect this would mean starting from scratch with a totally new CMS :(
A component always displays its results in the "mainbody" area of your template.
A module displays its results outside of the "mainbody" ... usually along the side, top, or bottom of the mainbody.

Drupal & Regular PHP Integration

I'm building a new website which has one core application and many content pages. Content pages are mostly dynamic and I require a way to manage this dynamic content on a regular basis. The core application's main functionality is a 3 step process or reading user data (input page), reading data from MySQL (product page) and submitting an application to an email address (application page).
Ideally I would like to build the core application in regular PHP and leverage Drupal for its content management capabilities. Can Drupal and regular PHP be integrated as I suggest easily? My feeling is that coding the core application as a Drupal module(s) will add layers of complexity that could be difficult to code from the outset and maintain later on as the system matures - so I would really like to just use regular PHP.
Let me explain where dynamic content (managed by the CMS) intersects with the core application:
Dynamic content such as FAQ data is used both on the 'normal' help pages and also within a mini-feed displayed within core application pages down a right hand side column. In this column, 3 random questions are pulled from the database and displayed as a feed. When users click on FAQ question they are not taken away from the core application product page but are instead shown data in a pop-up window displaying the question and answer. In addition, users can browse other questions and answers through a simple navigation menu within this popup. There are 3 such like feeds as I describe above that I require on the core application product page.
So, what is the ideal solution here in terms of 'keeping things simple' for both the management of dynamic content and the ease of coding the core application? Can 'regular PHP' and Drupal co-exist 'peacefully'? If so, how is this technically possible? Because there is some content managed by Drupal contained within core application pages, can the core application still be coded in regular PHP?
Any advice / suggestions?
Thank you!
Jim.
This is part of a post I submitted few years ago. Unfortunately the linked article is long gone, but it might still be relevant for your needs:
Importing old-style standalone PHP scripts into Drupal system
Drupal system is a very strong CMS, but taking an existing PHP site and trying to force it on Drupal might be a hard thing to do.
The "right" way to convert a site into Drupal is to "Drupalize" it, which means to do everything "the Drupal way", which is really the right thing to do - but is some cases not everybody agrees about that.
I had an example with a company that has many HTML/PHP based sites. They heard about the flexibility and strength of Drupal, and decided it is their choice for the revolution. But, on the other hand, they want not to change anything, and that I will do it as fast as possible.
The "right way" to do it was to break each PHP script they have to its pieces, find the commons, create the right methodology with existing Drupal modules, and finish the gaps with my own additional module.
But this is not what they asked me to do - instead, I looked for a way to just import the existing PHP script into Drupal environment.
Searching for am existing solution brought me to a proof-of-concept described by Dan Morrison. In the bottom line it uses PHP output buffering commands it order to collect everything the old script does, and then to display it as part of a Drupal module. In my case I had to make some small modifications (mainly to pass variables from the request to the PHP in the relevant scope), but in the bottom line it did exactly what I needed.
Well, almost... In other cases, in which I wanted to enjoy from Drupal's Form API, I handled it differently, as I will describe later on.
It sounds like you are asking about two things:
Including Drupal content in a custom-built PHP application.
Displaying Drupal content in a pop-up window from the PHP application.
There are a number of different levels of integration- if your need is as simple as pulling content into a page then you may be able to simply create a view (http://drupal.org/project/views) with that content from Drupal that you can make available to and then include it in your web app.
If you are looking for a tighter integration between the 2 including sharing of users, sessions, etc... then it is likely also "possible" but would take additional information to understand what you need.

Hooks...exactly what they are

I have seen hooks in Kohana PHP framework, and they work as some sort of a callback function triggered by a certain event (Kohana's events that is, some sort of method overloading).
I have seen hooks in Wordpress, and I don't know what they are or how to use them (just saw them yesterday).
I'm looking for events in "non-frameworked" php, I cannot find ones.
Do hooks work only in an "event-based" environment? What are they anyway (in general, not just in PHP)? What are they good for if not in an "event-based" environment.
Hooks are, indeed, hooks into an event stack of sorts; a list of values some controller iterates over, and if you have anything registered to that event, the controller can run your custom code. But PHP itself doesn't have anything (useful) like that, so you make it yourself or use the ones you find in your favorite application / system. It's a fairly common way to create a plugin architecture, but can also be used for application control and other things. I've written earlier about my quest for a more universal event and operating set of stack events, including this post here on StackOverflow.
As others have mentioned, PHP is stateless, so where I use them I use them as a simple execution list, and hook every part of my application into it. This way I'm very extensible, and have a basis for a plugin stack as well. (And I'll release it one magical day when I'm bored or retired or just got too much time on my hands, etc.)
You'll find similar stacks and hooks in, for example, WordPress, so a plugin that deals with, say, CSS, will hook itself to the CSS_DEFINITION_EVENT (basically, that part of the WordPress application that writes CSS stuff into the HTML section). This stuff is everywhere. In PHP it only applies (well, mostly) to the limits of the request you get per PHP page (unless you're doing PHP outside the webserver), but all major operating systems, applications, frameworks and systems have some form of event stack. PHP just doesn't have one (seriously) built in.
PHP is stateless, therefore it cannot really have events. They are emulated with manually adding and storing event listeners ( functions to be called ) and then calling said listeners explicitly when something happens in the code. Like a new picture was uploaded or a 404 error occurred.
Are you looking for these?
register_shutdown_function
set_error_handler
set_exception_handler

Categories