Theming Node Form into Regions in Drupal 6

This is one of the cases, I needed to change the layout of node add form. This snippet shows us how to theme one node type add form. Originally I inspired the solution from Development Seed’s theme, rubik.

Let’s suppose our node type is “TYPE”, and theme name is “custom_theme”. The template page is located in themes/custom_theme/templates/node-TYPE-form.tpl.php.
First of all, define hook_theme() in your theme’s template.php.

<?php
/**
 * Implementation of hook_theme().
 */
function custom_theme_theme() {
  $items = array();
  $items['TYPE_node_form'] = array(
    'arguments' => array('form' => array()),
    'path' => drupal_get_path('theme', 'custom_theme') . '/templates',
    'template' => 'node-TYPE-form',
    'preprocess functions' => array(
      'custom_theme_preprocess_TYPE_form'
    )
  );

  return $items;
}
?>

Now implement the preprocess handler. We want to divide the form into two columns, main form in left sidebar, and taxonomy in right sidebar. As footer We will print the buttons.

<?php
function custom_theme_preprocess_TYPE_form(&$vars) {
  // Copy taxonomy and buttons from $form to $vars, to pass them to the template file.
  $vars['sidebar'] = $vars['form']['taxonomy'];
  $vars['buttons'] = $vars['form']['buttons'];
 
  // Remove taxonomy and buttons, from $form ( no duplication ).
  unset($vars['form']['taxonomy']);
  unset($vars['form']['buttons']);
}
?>

After that, create the template page, where you will use drupal_render() function.
In or case the template page will be: themes/custom_theme/templates/node-TYPE-form.tpl.php. And its code :

  
    <?php if($sidebar): ?>
      <?php print drupal_render($sidebar); ?>
    <?php endif; ?>
  
  
    <?php if($form): ?>
      <?php print drupal_render($form); ?>
    <?php endif; ?>
    
    <?php if($buttons): ?>
      
        <?php print drupal_render($buttons); ?>
    
  <?php endif; ?>
  
  

Finally, create your own CSS styling 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *