Theme a CCK input form for CCK2

Last modified: November 24, 2009 - 16:54

Spending several days for me to find out how to theming Input Form in CCK2 on Drupal 6, hope this can help you and save your time.

To theming CCK2 Input you just need:

  1. Edit template.php
  2. Create node-content_type-edit.tpl.php
  3. Clear Cache Data before view your result: Administer-Site Building-Performance: Clear Cache Data

EXAMPLE

Suppose your content-type is: "account_registration" and you theme is "bluemarine"

  1. Edit template.php, add this:
    <?php

    function bluemarine_theme($existing, $type, $theme, $path) {
      return array(
       
    'account_registration_node_form' => array(
           
    'arguments' => array('form' => NULL),
           
    'template' => 'node-account_registration-edit'
       
    )
      );
    }

    ?>

  2. Create node-account_registration-edit.tpl.php

    //To REMOVE Title field
    <?php unset($form['title']); ?>

    <fieldset class=" collapsible">
        <legend>Company Data</legend>
        <?php
         
    //NOTE: if you don't have Field Group then simply type:
          // print drupal_render($form['field_accreg_company_name']['0']['value']);
         
    print drupal_render($form['group_company']['field_company']['0']['value']);
          print
    drupal_render($form['group_company']['field_street']['0']['value']);
       
    ?>

    </fieldset>  
    <?php print drupal_render($form);  ?>
    <?php
    // print_r($form);   //Enable this to show all Array Variables of Form
    ?>

HOW TO ...........

  1. RENDER ONLY 1 FIELD
    <?php print drupal_render($form['group_company']['field_street']['0']['value']); ?>

  2. RENDER A GROUP OF FIELDS
    You don't need to render fields in group one by one, just enter this code to render all fields in a group:
    <?php print drupal_render($form['group_company']); ?>

  3. RENDER A SELECT LIST
    Almost same like render a TEXTFIELD, but avoid ['0']['value'] at the end
    <?php print drupal_render($form['group_company']['field_region']); ?>

  4. REMOVE AN INPUT FIELD
    You may want to disable an input form, usually you need to remove TITLE as shown below:
    <?php unset($form['title']); ?>

  5. HIDE AN INPUT FIELD (HIDE vs REMOVE!)
    You may still want to enable an input form but need to prevent it:
    <?php $form['title']['#access'] = FALSE; ?>

  6. SHOW ALL VARIABLES OF FORM
    You may want to know what variables available for you:
    <?php print_r($form); ?>

  7. REORDER
    Use ['#weight'] to reorder
    $form['buttons']['#weight'] = -50; // buttons at the top

  8. PRINT BUTTONS
    <?php print drupal_render($form['buttons']); ?>

  9. RENAMING BUTTON
    What is "Submit"? You may need to write it as "Save now!", don't you?
    $form['buttons']['submit']['#value'] = 'Save to Database';

  10. HIDE GROUP FIELD-SET
    $form['group_general']['#access'] = FALSE;

  11. HIDE BUTTON
    $form['buttons']['submit']['#access']= FALSE;
 
 

Drupal is a registered trademark of Dries Buytaert.