Upgrade Your Drupal Skills

We trained 1,000+ Drupal Developers over the last decade.

See Advanced Courses NAH, I know Enough

What Every Drupal Developer Should Know

Parent Feed: 

When you first get started with Drupal it can be extremely intimidating as there are many options, modules and things goin on. This guide will help show you some of the most important parts of Drupal that really make it stand above other content management systems.

How to control different parts of the page

Different parts of a page are controlled via different template files. The primary ones you will deal with are:

  • page.tpl.php - Controls the whole page view of the site. Everything that lays out the foundation of your site is done here.
  • node.tpl.php - This controls the look of the nodes, or content of your page. This will control how the text that you create as like a 'page' or 'story' looks.
  • block.tpl.php - This controls the blocks that go on your sidebars. You can style theme here.
  • comment.tpl.php - This controls how the comments look.

All you do is pretty much create a file called something like page.tpl.php and then just fill it out w/ whatever HTML you want and then you can choose from a list of variables to use, such as the page title, the navigation, etc.

Node.tpl.php

It is worth mentioning node.tpl a little more as this is an important one that is used a lot in most cases. Each type of node you create can have its own theme. So you could theme your 'page' nodes to look different from your 'story' nodes. This would be done by simply naming your file node-page.tpl.php or node-story.tpl.php - you just add on -nodetype to the file and this will take precedence over the default node.tpl.php :).

You can override any function that begins with theme_ in your template.php file

I didn't really know this for a while and it is very useful information, especially for people into theming. A lot of repetitive Drupal functions use the format theme_username, or theme_table. If you go through modules you will notice they too have the theme_ format. What this does is allow any part that actually emits HTML or display-oriented parts to run through a Drupal function that first searches your template.php and overrides it with something new. This is instead of modules calling theme_username they would call theme('username'). This is basically the same thing except if called as the latter it will allow your custom template.php file to overwrite the default one.

For example, you can customize the user page by overriding the function theme_user_profile

Say you want to change how the user page looks. You could cruise around your modules and find the function that handles this. (You should be able to open up say user.module and search for 'theme_').

You will come across the function: theme_user_profile which looks like this is the one that handles that.

We would now copy this code and paste it into our template.php page:


function theme_user_profile($account, $fields) {
$output = '

';
$output .= theme('user_picture', $account);
foreach ($fields as $category => $items) {
if (strlen($category) > 0) {
$output .= '

'. $category .'

';
}
$output .= '
';
foreach ($items as $item) {
if (isset($item['title'])) {
$output .= '
'. $item['title'] .'
';
}
$output .= '
'. $item['value'] .'
';
}
$output .= '
';
}
$output .= '
';
return $output;
}

We could now rename it to phptemplate_user_profile and we can tweak it however we like :)


function phptemplate_user_profile($account, $fields) {
$output = 'The profile page for '.$account->name.' is super simple but still badass';
return $output;
}

That's it! You will now have a custom profile page for every user. The possibilities are endless!

Use a node w/ CCK for pretty much anything you want to store data with

CCK is one of the best modules because it allows users to add any custom fields into any node type. Say you want to add a text field for a city. Boom, done. Now your node would have Title, City and Body during creation. Each one of these fields is also themable via the node.tpl file for the node. They can be accessed like $node->field_city[0]['view']. You can add as many, or as few fields as you would like for each node.

You can share multiple sites on the same Drupal install

This is somewhat advanced but not really. If you have multiple sites it makes sense to share the same modules with all of them. That way if you update core or the modules it will take effect on all the sites using that install. You can also share parts of the databases such as users and stuff, but that does a bit more advanced and isn't always necessary. I do think it is necessary to get all sites using the same Drupal install though. Otherwise you are always having to figure out which modules versions its using, make sure all the files have the current stuff uploaded and it just becomes a hassle.

Don't go through and hack up the core code or modules

When I first started using Drupal I did this. I wanted my site EXACTLY how I wanted it so I would do whatever it took to make that change. In a way this is okay, but just don't expect to have an easy upgrade when a new version of Drupal comes out with new features that you have to have. It will work you bad - it did to me. As you grow with Drupal more you will figure out ways to override certain features and use modules to pretty much do anything you want anyway.

The current system path is always sent via arg(n)

To figure out where a certain page is located it can be useful to call arg(n). For instance if you are viewing node 5 and you wanted to figure out what node you were viewing from a module or theme you would call arg(1) and it would return 5. This is because it goes through the default URL structure and splits each section between '/' up.

For instance, when viewing node 5 arg(0) would return 'node' while arg(1) would return 5. arg(2) would return nothing because there is nothing there. It pretty much takes the default Drupal path, regardless of whether there is a path or not because it is all handles internally.

So say you are editing user 3. The path in the url may look like: www.myurl.com/user/3/edit. The args would be:

  • arg(0) = 'user'
  • arg(1) = 3
  • arg(2) = 'edit'

So say you have all your nodes being re-written as just the tile of the node. So externally to users it would look ike: www.myurl.com/node-name-title-here. Internally, however, it would be:

  • arg(0) = 'node'
  • arg(1) = n (where n = whatever the nid is)

Hope that makes sense.

Hooks are awesome

Hooks are a bit more advanced and used in modules but there are still worth mentioning because they are like the heart of Drupal and its modularity. Pretty much any common or major task that is done in Drupal invokes what is called a "hook" This task could be pretty much anything. Drupal comes with a bunch of default hooks that are used, but you can also create your own.

When a user creates a new account the function hook_user is called. When a user edits a node hook_nodeapi is called. What this means is that anytime something happens in Drupal, it tells your module that it is happening and allows you to do something when this happens also. It's very cool. Say you want to also include new users in another custom table when they sign up. You would name your function something like mymodule_user and you would now be able to include these users in a custom table when they sign up :).

Recommended Modules

This tutorial wouldn't be complete without a list of some very useful modules

  1. CCK - CCK allows you to create custom fields for any node type. These fields can range from simple checkboxes, to full blown AJAX date fields.
  2. Views - Views allows you to create custom pages and blocks that list your content in tons of different ways. Say you want to have the 10 most recent story nodes listed descending by most page views. Done!
  3. Pathauto - Pathauto automatically creates a path for nodes, users and taxonomy upon creation based on settings you have control over.
  4. Webform - Webform allows you to set up any type of form that collects info. It saves the data to the database and sends an email to wherever. Very cool!

Conclusion

I hope this little walk-through will clear up and add some insight into the inner workings of Drupal. Drupal really is a super powerful system and it is so cool to use once you have a better understanding of it. It is the only way to build a web site in my opinion.

 Filed under: Internet / Tech, Drupal
Original Post: 

About Drupal Sun

Drupal Sun is an Evolving Web project. It allows you to:

  • Do full-text search on all the articles in Drupal Planet (thanks to Apache Solr)
  • Facet based on tags, author, or feed
  • Flip through articles quickly (with j/k or arrow keys) to find what you're interested in
  • View the entire article text inline, or in the context of the site where it was created

See the blog post at Evolving Web

Evolving Web