Drupal 7 theming basics

Dec 17 2009

Drupal 7 theming basics

Posted by amandar

I am no expert on Drupal theming, but I recently attempted to convert one of my Drupal themes to Drupal 7 and it was pretty stressful at times! I'm gonna try and outline some of my changes here. The theme I am attempting to update is http://www.drupal.org/project/orange/

.info files

.info files have changed. The content variable has been removed for Drupal 7, so you need to declare the region in your .info file or you wont even be allowed to enable the theme. Also because of problems with sub-theming style.css is no longer added by default. You will need to also declare this in your .info file.
Like Drupal 6 if you want custom regions and js files you declare them here as usual.

<?php
regions
[content] = Content
stylesheets
[all][] = css/style.css
?>

page.tpl.php

Page.tpl.php has some changes, for one you no longer have to have html head or body tags. This data is contained in a the page variable. Regions are also in the page variable. At the top of your page.tpl.php you'll see:

<?php print render($page['header']); ?>

At the bottom you'll see:

<?php print render($page['footer'); ?>

To check if a region has content and print out (which was really hard for me to realize at first!) you do the following:

<?php if ($page['sidebar_first']): ?>
<?php print render($page['sidebar_first']); ?>
<?php endif; ?>

template.php

So I had to make a few changes here. First off my breadcrumb, previously was:

<?php
function orange_breadcrumb($breadcrumb) {
  if (!empty(
$breadcrumb)) {
   
$breadcrumb[] = drupal_get_title();
   
$separator = theme('image', drupal_get_path('theme', 'orange') .'/images/black-bullet.gif');
    return
'<div class="breadcrumb">'. implode(' '. $separator .' ', $breadcrumb) .'</div>';
  }
}
?>

the theme function has changed, this took me a WHILE to figure out... so to get the same result I have the following for D7:

<?php
function orange_breadcrumb($variables) {
 
$breadcrumb = $variables['breadcrumb'];
 
$output = '';
  if (!empty(
$breadcrumb)) {
   
$breadcrumb[] = drupal_get_title();
   
$separator = theme('image', array('path' => path_to_theme() . '/images/black-bullet.gif'));
   
$output .= '<div class="breadcrumb">' . implode(' '$separator .' ', $breadcrumb) . '</div>';
    return
$output;
  }
}
?>

Drop down menus

This was probably the hardest task for me but I managed to figure it out in the end. In the Drupal 6 theme I had the following in my preprocess_page function to achieve a drop down:

<?php
if ($vars['primary_links']) {
 
$pid = variable_get('menu_primary_links_source', 'primary-links');
 
$tree = menu_tree($pid);
 
$tree = str_replace(' class="menu"', '', $tree);

 
$vars['primary_links'] = '<del class="wrap">'. $tree .'</del>';
}
?>

Primary links have been renamed to main menu in D7. So we have to alter this, also printing the $tree variable just gives you "Array" so you need to use drupal_render to have it appear. This is what I ended up with for D7:

<?php
if (isset($vars['main_menu'])) {
 
$pid = variable_get('menu_main_links_source', 'main-menu');
 
$tree = menu_tree($pid);
 
$tree = str_replace(' class="menu"', '', $tree);
 
$vars['primary_nav'] = drupal_render($tree);
}else{
 
$vars['primary_nav'] = FALSE;
}
?>

Thats all I've got so far! I'll post more struggles as they come. If I am doing anything wrong here, by all means correct me!

Post new comment
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options