Adding Body Classes by Node Type in Drupal

<?php 
	if($node->type == 'story') {
		$nodetype_class = 'story';
	}
	elseif($node->type == 'blog') {
		$nodetype_class = 'blog';
	}
	elseif($node->type == 'page') {
		$nodetype_class = 'page';
	}
?>
  <body class="<?php print $nodetype_class; ?>">

This looks at the nodetype of the page and if it's a story node adding the class "story" or else if it's the node type blog it will print out the class "blog", etc.

I've used this on the top of page.tpl.php to create a class depending on the nodetype of the page. Helpful to create css changes to the layout.

Adding Custom Page Templates for Node Type

For Drupal 5

Adding the following to template.php allows files named page-nodetype.tpl.php to override the base page.tpl.php. This way, nodetypes can have their own design or blocks without relying on the general template.


function _phptemplate_variables($hook, $vars = array()) {

// Add additional template suggestions
  switch ($hook) {
    case 'page':  
      // Add page template suggestions based on node type, if we aren't editing the node.
      if ($vars['node'] && arg(2) != 'edit') {
        $vars['template_files'][] = 'page-'. $vars['node']->type;
      }
      break;
  }
 
  return $vars;
}

I've used this to limit the blocks or the menus appearing on a per nodetype basis.

Read More at Drupal.org

Formatting Date and Time in Drupal

Snippet to change the node created on date for use in story nodes:

In node-story.tpl.php or other node tpl:

<?php print format_date($node->created, 'custom', 'F jS, Y'); ?>

Prints:

March 25th, 2009

Check out php.net for complete list!

Changing the Drupal Admin Password in SQL

This is a handy bit to know when you get a db dump and don't know the admin password that's been setup. (And are too impatient to wait for an email response? :P )

update users set pass=md5('kittens') where uid=1

Replace kittens with desired password. This effects User ID 1 (admin).

Updating to Views 2: How to convert views

Recently I needed to update a Drupal 5 site to Drupal 6 that used views. The old views from 5 weren't showing up after the update, I couldn't find much information on this strangely. If you're having problems finding out where your views went they need to be converted, and it's not in a very noticeable area.

It's hiding under the tools tab and convert.

/admin/build/views/tools/convert

Would be nice they had a pleasant little notice that told me that I had views needed to be converted and a link.

Overriding Breadcrumb Separator in Drupal

Looking to override the breadcrumb separators? Add the following to template.php-
Replace 'kitten' with something like > or » or even an image.

function phptemplate_breadcrumb($breadcrumb) {
  if (!empty($breadcrumb)) {
    return '<div class="breadcrumb">'. implode(' kitten ', $breadcrumb) .'</div>';
  }
}

To replace the breadcrumb separator with an image use the following, replacing the image path/name with the appropriate location of the image. This will be looking for an image in the theme directory.

function phptemplate_breadcrumb($breadcrumb) {
  if (!empty($breadcrumb)) {
	 return '<div class="breadcrumb">'. implode('<img src="' . base_path() . path_to_theme() . '/path/to/image.png" />', $breadcrumb) .'</div>';
  }
}

Making a separator in your page title to not show up on the front page in Drupal

By divider I mean the '|' that I currently have on my site's titlebar. Now, this is all fine, to have $site_name and $title appear with a | separator, but It kind of looks weird on the front page when there is no page title.

It's pretty easy to just have it called differently when you are using page-front.tpl.php, but my site doesn't use a separate template file for the front page, so why should I have to make one for just this?

I've been using an if else statement to check $if_front and then display the separator, title and site name or else to show just the site name. Alternatively you could just check if there is a title and do it that way.

<title>
<?php 
if ($is_front) {
	print $site_name; 
} else {
	print $title . t(' | ') . $site_name; 
}
?>
</title>

This seems to work well for me, thoughts?

From PSD to Drupal Theme Tutorial - Part III

Part III of III

The last two parts of the tutorial, we went over how to take a finished design using the Photoshop slice tool to html and css.

This part we will take that completed HTML page and make it into a functioning Drupal theme.

What you will need:

  1. Download the latest version of Drupal found here
  2. Read through the requirements for Drupal here
  3. For notes regarding installation of Drupal, please read this article
  4. You will also need a text-editor and web browsers for testing. (I would also suggest downloading the firefox add ons Web Developer Kit and Firebug


From PSD to Drupal Theme Tutorial Part II

SECTION II of III: HTML view part I here..

We left off with finishing the Photoshop slices. These images should be in an images folder inside a folder which will hold your website/theme files.

For this part of the tutorial you wil need a text editor. I will be using the free program TextWrangler If you are using a MAC I would recommend trying out the demo of CODA. But any text editor will suffice.

From PSD to Drupal Theme Tutorial Part I

While at a recent meeting held at Bryght's Office, there was great interest by the audience in seeing how to convert a Photoshop file into a Drupal theme.

This tutorial will show you how to take an existing design in either Photoshop or Illustrator to XHTML to a working Drupal theme.

For this example, I will be using Photoshop CS3 on a mac, however older versions of Photoshop and PC users should be able to follow along as the primary tools and menus that we will be using should remain very similar.

Those who prefer Illustrator, the method is almost exactly the same as in Photoshop. But since there was an interest in Photoshop specifically, it is the program I'll be using for this example.