Question

I want to add a new area which will be site wide. Describe the process of getting this new region into my theme, if possible generalize it for adding any new, custom region.

Was it helpful?

Solution

You define regions in your theme .info file. See: Structure of the .info file

You then need to put the relevant template tags in your page.tpl.php file.

So for example if you defined the region sidebar in your themes .info file, you would then add: <?php print render($page['sidebar']); ?> to the relevant place in your page.tpl.php.

OTHER TIPS

One other point: Don't miss the required regions!

regions[content] = Content
regions[help] = Help
regions[page_top] = Page top
regions[page_bottom] = Page bottom

More on this can be found at http://drupal.org/update/themes/6/7#closure.

To clarify on this point: If you define your own regions, you need to define all that you are using, including those that are required. In other words, you cannot just define additional regions: If you define any, you must define them all.

Creating a region for a Drupal 7 theme is not different from creating a region for a Drupal 6 region; the differences between Drupal 7 and the previous versions are essentially:

  • The regions are not defined using a mytheme_regions() function as it was done in Drupal 5, but they are defined in mytheme.info using regions[] directives, which is what is done also with Drupal 6 themes.
  • The regions are rendered using the Drupal function render(), instead of printing the content of a variable.

The default Drupal 7 regions are the following ones:

regions[header] = Header
regions[highlighted] = Highlighted
regions[help] = Help
regions[content] = Content
regions[sidebar_first] = Left sidebar
regions[sidebar_second] = Right sidebar
regions[footer] = Footer

Drupal 7 core themes now include a region named 'highlighted' which uses the same display as the mission statement area in Drupal 6; they also include a region named 'help', that by default has the same content of the $help variable used by Drupal 6.

The theme regions are defined in your theme .info file — see docs. Just add a line with regions[corner] = Corner to get a Corner region.

Then you need to edit the page.tpl.php file and add <php print render($corner); ?> where you want the region to show up. If your theme doesn't have a page.tpl.php, copy from the base theme or from the system module.

Common pitfalls:

  • If the theme.info file had no regions declared, it was using Drupal default regions. If you want to add, you will have to re-declare the default regions. You can, of course, remove any region but please support the required regions. See the docs again.

  • Changes to the .info file or adding new files to the theme (like page.tpl.php) will only be noticed after you clear caches.

  • Omega, a very popular base theme, won't need any change in page.tpl.php, just the .info file. But you will have to visit the theme settings in the UI and assign your region to a zone.

Follow the below steps to create custom region in your theme:

  1. Go to your theme.info file and add this line.

    regions[machine_name_of_your_region] = Name of your region
    
  2. Although you can also call region blocks in the node template as well, you will generally add the region to the theme's page template. This is usually page.tpl.php, or if you have custom template files for different pages, edit those ones too. The new region will appear only on the pages built around the template you add the new region to. Paste it wherever you want your new region to appear.

    <?php if ($page['machine_name_of_your_region']): ?>
      <div id="sidebar-first" class="column sidebar"><div class="section">
      <?php print render($page['machine_name_of_your_region']); ?>
      </div></div> <!-- /.section, /#sidebar-first -->
     <?php endif; ?>
    
  3. Configuration - Performance - Clear all caches

Create a New region in Theme

You Just need to define it in two files.

  • Theme.info
  • Page.tpl.php

Theme.info

;;;;;;;;;;;;;;;;;;;;;
;; Regions
;;;;;;;;;;;;;;;;;;;;;

regions[navigation]      = 'Navigation'
regions[header]          = 'Top Bar'
regions[highlighted]     = 'Highlighted'
regions[new_region_name] = 'Region Name' ;; Add New Region

Page.tpl.php

Now render this region

  <?php if ($page['new_region']): ?>
      <div class="new_region_class">
        <?php print render($page['new_region']); ?>
     </div>
  <?php endif; ?>

Now Clear all cache, Here you go, you have a new region in your theme. You can see it at your block configuration page.

For more visit http://drupal.org/node/171205

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top