Theming CCK Input Form

Last modified: June 30, 2008 - 23:37

Suppose you create new CCK content type called: product. You can alter how CCK displays content by creating node-product.tpl.php file. Now, how to alter the INPUT FORM?

Drupal's FormAPI will always look for a theme function based on the form id. In the case of node/add or node/edit forms, this id is type_node_form. So you can use the theme override like so:

<?php
function phptemplate_product_node_form($form) {
  global
$user;
 
$vars = array('user' => $user, 'form' => $form);
  return
_phptemplate_callback('product_edit', $vars);
}
?>

You can also go further in making really designer-friendly tpl.php files by doing your drupal_renders in template.php like so:

<?php
function phptemplate_product_node_form($form) {
  global
$user;
 
$vars = array('user' => $user, 'form' => $form);
 
$vars['title'] = drupal_render($form['title']);
 
$vars['body'] = drupal_render($form['body_filter']);
 
// etc
 
return _phptemplate_callback('product_edit', $vars);
}
?>

This will let you have a product_edit.tpl.php like:

<div class="intro">Start your product here</div>
<div class="title"><?php print $title; ?></div>
<div class="body"><?php print $body; ?></div>

Finally, for maximal control, you can unset the FormAPI #title attributes and label your form elements directly in the template file:

<?php
function phptemplate_product_node_form($form) {
  global
$user;
 
$vars = array('user' => $user, 'form' => $form);
  unset(
$form['title']['#title']);
 
$vars['title'] = drupal_render($form['title']);
  unset(
$form['body_filter']['body']['#title']);
 
$vars['body'] = drupal_render($form['body_filter']);
 
// etc
 
return _phptemplate_callback('product_edit', $vars);
}
?>

How about D6 and CCK2?

trogie - November 16, 2008 - 14:46

How about D6 and CCK2?

Help with D6 please

discursives - November 29, 2008 - 06:42

Could use a few pointers!

Is it in a book somewhere? Is that what is going on?

Theme Individual CCK 2 Fields

davidwhthomas - June 13, 2009 - 09:48
  • Copy content-field.tpl.php from sites/all/modules/cck/theme to your theme folder. (thanks alanburke: http://drupal.org/node/269319)
  • Make your new template: content-field-field_name_of_my_field.tpl.php -- placed in your theme folder. Note: you can make it from a copy of content-field.tpl.php or you can put just test html in an empty file - just to get the template to show up something besides default output. Later, go back and really edit the theme output for the fields with the correct syntax. You don't need to clear the cache after you get your tpl.php registered (again, unless you move/rename it)
  • Add your new template files to your theme folder - you can nest these in folders, just keep in mind you need to clear your cache to get the templates to show up if you move them around
  • Clear theme registry cache manually, or visit admin/build/modules to trigger a rebuild, or execute drupal_rebuild_theme_registry(); to register your new template files

Source: http://drupal.org/node/269319#comment-1184661

cck input theming!

pan0s - January 11, 2009 - 15:17

i was wondering whether someone has better documentation about that...i would really appreciate a relevant more accurate post.

-----------------------------------------------------------------------------------------
Some people say the glass is half-full while others say its half-empty....i say bottoms up.

Additional info

jmav - January 15, 2009 - 12:27
 
 

Drupal is a registered trademark of Dries Buytaert.