The Definitive Guide to Drupal 7


Creating New Regions

The creation of a new region is a two-step process. Using the example in Figure 15–12, here’s the process of creating the new Banner Ad and Navigation regions.

  1. Define regions in the file. Begin by adding the code for your new regions to the defaults you started with in Listing 15–3, plus the definition of the banner_ad and navigation regions to your file.

    2. regions[page_top] = Page Top
    3. regions[header] = Header
    4. regions[highlight] = Highlight
    5. regions[help] = Help
    6. regions[content] = Content
    7. regions[sidebar_first] = Sidebar First
    8. regions[sidebar_second] = Sidebar Second
    9. regions[footer] = Footer
    10. regions[page_bottom] = Page Bottom
    13. regions[banner_ad] = Banner Ad
    14. regions[navigation] = Navigation
  2. Print the regions in the page.tpl.php template file. Once you clear your site caches, you’ll be able to see and populate the new regions on the Blocks administration page at admin/structure/block. In order to get them to display on the page, you’ll need to override the page.tpl.php file in your theme and print the new regions.

    Navigate to the modules/system directory, copy the page.tpl.php file and paste it into the sites/all/themes/dgd7 directory you created earlier.

    Open the page.tpl.php file in the theme and scroll down to the <div id="page-wrapper"> and paste the code to print the region below it, and above the <div id="header">.

    1. <div id="page-wrapper"><div id="page">
    2. <?php print render($page['banner_ad']); ?>
    3. <div id="header"><div class="section clearfix">

    Remove the default markup for the $main_menu and replace it with the region code for your new navigation region.

    Remove this code:

    1. <?php if ($main_menu || $secondary_menu): ?> <div id="navigation"><div class="section">
    2. <?php print theme('links__system_main_menu', array('links' => $main_menu, 'attributes' => array('id' => 'main-menu', 'class' => array('links', 'inline', 'clearfix')), 'heading' => t('Main menu'))); ?>
    3. <?php print theme('links__system_secondary_menu', array('links' => $secondary_menu, 'attributes' => array('id' => 'secondary-menu', 'class' => array('links', 'inline', 'clearfix')), 'heading' => t('Secondary menu'))); ?>
    4. </div></div> <!-- /.section, /#navigation --> <?php endif; ?>

    Replace with this code:

    1. <?php print render($page['navigation']); ?>
  3. Technically you’re finished, but let’s add some content to illustrate what you’ve done.

    Add a new custom block for the Banner Ad code. Title the block “Banner Ad” and add the following code to fake the appearance of an ad banner in the Block body (be sure to select the Full HTML text format). Then, select the Banner Ad region for the region settings and save it.

    1. <img style="width: 728px; height: 90px; border: solid 1px #000;" alt="728 x 90 Banner Ad" src="image.png" />

    Go back to the admin/structure/block page. Find the Main Menu block and place it inside the Navigation region and click Save blocks.

You’ve just added and populated two new custom regions!

You are reading content from two chapters on Theme Development from The Definitive Guide to Drupal 7, written by Jacine Luisi and published by Apress on July 19, 2011. All rights reserved.