.on web development, Drupal, PHP, photography, and the like.

Arguing with Drupal

Posted by root
08.10th.2008

Filed under:

When taxonomy based theming in Drupal becomes cumbersome or its just not enough, theming based on arguments just might do the trick! Recently I had to setup a theme without setting up a site and parts of the site had different themes. Since I had no knowledge of any taxonomy, customizing page layouts via path arguments became a reality (and quite powerful!)

The use of extracting arguments in Drupal is a very powerful thing. Once you break down the Drupal path, you can customize practically everything. The path is the part after ?q= or /drupal_installation/. For example, adding a new comment ('add new comment') path for a page type is: comment/reply/10. arg(0) is comment, arg(1) is reply and arg(2) is the node nid. Once you wrap this path break down around your head, you can customize blog reply or story comment pages by cross referencing the node nid and its arguments to find out its ->type.

Below is a layout when someone visits your Drupal web site. First, Drupal loads its default template php file: page.tpl.php. In it are PHP if statements, determining what type of content is going to use which page layout template.

<?php
  if ( is_numeric(arg(1)) ){  /* for node/NID */
    $nid = arg(1);
  }elseif( is_numeric(arg(2)) ){ /* for comment/reply/NID */
    $nid = arg(2);
  }
$node = node_load(array('nid' => $nid));
$drupaltype = $node->type;
 
  if ($is_front) {                /* check if it's the front page */
    include 'page-front.tpl.php'; /*load a custom front-page.tpl.php */
    return;
  }
 
  if ($node->type == 'blog' || $drupaltype == 'blog' || arg(0) == 'month') { /* check if it's a blog node */
    include 'page-blog.tpl.php'; /*load  page-blog.tpl.php */
    return;
  }
 
  include 'page-default.tpl.php'; /*if none of the above applies, load the page-default.tpl.php */
  return;
?>
Reference: http://drupal.org/node/46027