Drupal 7 Theming - Behind the Scenes: PHP control flow starting from entering URL to browser displaying webpage. Covers Theme info file, regions, an Rendear Arrays (Phoenix User Group 1/25/2012)
1. hook
HOOK
Ramu Ramakesavan
Phoenix Drupal User Group
January 2012
2. Client Side Server Side
HTML Apache Webserver
CSS Style Sheets PHP, Perl
JavaScripts MySQL
Drupal Framework
AJAX
Themes, Regions, Blocks
Forms, Views, Panels
Drupal Administration
Installation Module, Hooks
Updates/ Upgrades Caching
Crawlers
Debug
3.
4. • Display of data on the webpage
• Structured presentation
• Site specific modifications to presentation
• Creation of data for the webpage
• Drupal7 mechanism for data creation
• flow of control during data creation (PHP code)
• Site specific modifications to data creation
• Searchable and editable data format
7. • template_preprocess; template_preprocess_<theme_element>
• (web/includes/theme.inc)
• default variables for all theme element/ specific theme elements
• theme_element: page, html, block, region, maintenance_page
• <mymod>_preprocess; <mymod>_preprocess_<theme_element>
• (web/sites/all/modules/<mymod>)
• modules that didn't define the theme hook can alter the variables.
• <ENGINE>_engine_preprocess;<ENGINE>_engine_preprocess_<them
e_element>
• (web/themes/engines/phptemplate/phptemple.engine)
• Drupal uses PHPTemplate as default engine
• <THEME>_preprocess; <THEME>_preprocess_ <theme_element>
• (web/sites/all/theme/template.php)
• Theme Bartik can set variables
8. • template_process; template_process_<theme_element>
• (web/includes/theme.inc)
• default variables for all theme element/ specific theme elements
• theme_element: page, html, block, region, maintenance_page
• <mymod>_process; <mymod>_process_<theme_element>
• (web/sites/all/modules/<mymod>)
• modules that didn't define the theme hook can alter the variables.
• <ENGINE>_engine_process;<ENGINE>_engine_process_<theme_ele
ment>
• (web/themes/engines/phptemplate/phptemple.engine)
• Drupal uses PHPTemplate as default engine
• <THEME>_process; <THEME>_process_ <theme_element>
• (web/sites/all/theme/template.php)
• Theme Bartik can set variables
9. If implemented using a real function instead of page.tpl.php then only
XXXX_preprocess_<theme_element>
XXXX_process_<theme_element>
routines are executed
Pre_process/process functions can define theme_hook_suggestion and
s
theme_hook_suggestion that can override current theme
implementation
links_contextual_node defines fallback implementations for named
objects / contexts
Function a_b_c
Function a_b
Function a
Question: When should these capabilities be used?
10. • Display of data on the webpage
• Structured presentation
• Site specific modifications to presentation
• Creation of data for the webpage
• Drupal7 mechanism for data creation
• flow of control during data creation (PHP code)
• Site specific modifications to data creation
• Searchable and editable data format
11. A web-page is created when a
http://www.ramustores.com/?q=examples/theming_example
is entered at the URL box in a browser
A web-page is updated when a Menu item that is
assigned
http://www.ramustores.com/?q=examples/theming_example
is selected
Modules register path-CBfunction mapping that
are stored in menu_router table
Simplified URL works on top of this?
13. function theming_example_page() {
$content[0] = t('Some examples of pages and forms that are run
through theme functions.');
$content[1] = l(t('Simple page with a list'),
'examples/theming_example/theming_example_list_page');
$content[2] = l(t('Simple form 1'),
'examples/theming_example/theming_example_order_form');
$content[3] = l(t('Simple form 2'),
'examples/theming_example/theming_example_text_form');
$content['#theme_wrappers'] =
array('theming_example_content_array');
return $content;
}
14. 1, User enters URL
_http://localhost/web/?q=examples/theming_example
2. Drupal executes code in web/index.php
invokes function menu_execute_active_handler
3. menu_execute_active_handler (menu.inc) does the following
searches menu_router for (examples/theming_example) and gets
corresponding CallBackFunction.
MainContent=CBfunction(“examples/theming_example”)
drupal_deliver_page(MainContent, HTML (not AJAX or XML))
defined in menu.inc
4. Drupal_deliver_page (common.inc) does the following:
drupal_deliver_html_page (MainContent)
5. Drupal_deliver_html_page (common.inc) does the following:
drupal_render_page(MainContent)
15. 1. drupal_render_page($MainContent) (common.inc) does the following:
1. Copies the $MainContent into $page[„current‟]
2. Invokes <module>_page_build ($page)
each modules adds its data
3. Invokes drupal_alter( $page)
Your custom module can alter the contents of $page
4.Invokes drupal_render($page)
================================================
block_page_build(„html page‟) (defined in block.module) does the following
1. $all_regions = system_region_list($theme);
2. for each ($region in $all_regions) {
if ($blocks = block_get_block_by_regions($region)) {
$page[$region] = $blocks;
endif
endfor
drupal_alter (module.inc) does the following:
1. Invokes each module that has defined <module>_page_alter.
16. 1. Drupal_render($page) (common.inc) hands over to the presentation
layer (ie. Theming)
1. Recursively invokes „theme()‟ to generate HTML code
First it calls page.tpl.php for Bartik
Next it calls html.tpl.php from system module
17. Searchable & editable array with key- $page = array(
value pairs. Keys are searchable '#show_messages' => TRUE,
„#theme_wrappers‟=> „html‟
the data used to build a page is kept '#theme' => 'page',
as render_array until they are ready to '#type' => 'page',
be displayed using user selected
'content' => array(
theme 'system_main' => array(...),
'another_block' => array(...),
Allows flexibility in changing the '#sorted' => TRUE,
layout or content of a page ),
'sidebar_first' => array(
...
Alter and preprocess functions work ),
with Render Array 'sidebar_second' => array(
...
Rendering = transforming render array ),
into HTML 'footer' => array(
...
render([page[„content‟]) = )
drupal_render(page[„content‟]);