Page Rendering and Altering in Drupal

Step 1: The Router Item
After Drupal has been bootstrapped, the last thing index.php does is call
menu_execute_active_handler(). This retrieves the router item from the menu_router based on the
current path. There is an important chance here to intervene, hook_menu_item_alter()

Step 2: The Page Callback Is Fired
For example, the default home page returns this:
$build[‘default_message’] = array(
  ‘#markup’ => “<p>No front page content has been created yet.</p>”,
  ‘#prefix’ => ‘ ‘,
  ‘#suffix’ => ‘ ‘,
);

Step 3: The Delivery Callback

Now you are back in menu_execute_active_handler(). The next callback to fire is the delivery callback,
which defaults to drupal_deliver_html_page(). As the name suggests, by default you are delivering the
page callback results in HTML.

function overlay_deliver_empty_page() {
  $empty_page = ‘<html><head><title></title>’ . drupal_get_css() . drupal_get_js() .
  ‘</head><body class=”overlay”></body></html>’;
  print $empty_page;
  drupal_exit();
}

Step 4: drupal_render_page()

First, hook_page_build() is called, which allows other modules to add to the page array—because
here you build that. Your modules can add to the page array, also. Second, hook_page_alter() is fired.

Step 5. hook_page_alter()

Perform alterations before a page is rendered.

Use this hook when you want to remove or alter elements at the page level, or add elements at the page level that depend on an other module’s elements (this hook runs after hook_page_build().
For Example Page rendar hook :

function dgd7_page_alter(&$page) {
 if ($node = menu_get_object()) {
  $comments = &$page[‘content’][‘system_main’][‘nodes’][$node->nid][‘comments’][‘comments’];
  $comments[‘ad’] = dgd7_get_ad();
  // The first comment weight is 0, the second is 1, go between them.
  unset($comments[‘#sorted’]);
  $comments[‘ad’][‘#weight’] = 0.5;
 }
}
function dgd7_get_ad() {
  return array(‘#markup’ => t(‘Hello I am an ad!’));
}

Step 6. drupal_render()
This is a recursive function called on every child of the page array. So you start with running
drupal_render($page), then continue with drupal_render($page[‘content’]), then with
drupal_render($page[‘content’][‘system_main’]), and finally with
drupal_render($page[‘content’][‘system_main’][‘default_message’]).

So for example:
$element[‘#cache’] = array(
  ‘cid’ => ‘foo:bar’,
  ‘bin’ => ‘cache_something’,
  ‘expire” => 900,
);

Advertisements