Adding and theming additional fields to a node form
Adding a form element to an existing node form is a matter of using hook_form_alter and checking for the appropriate form id.
Observe the following example from taxonomy.module:
<?php
function taxonomy_form_alter($form_id, &$form) {
if (isset($form['type']) && $form['type']['#value'] .'_node_form' == $form_id) {
...
// Add your form array here, for example:
$form['taxonomy']['tags'][$vocabulary->vid] = array('#type' => 'textfield', '#default_value' => $typed_string, '#maxlength' => 100, '#autocomplete_path' => 'taxonomy/autocomplete/'. $vocabulary->vid, '#required' => $vocabulary->required, '#title' => $vocabulary->name, '#description' => t('A comma-separated list of terms describing this content (Example: funny, bungie jumping, "Company, Inc.").'));
}
}
?>The only thing in the form_alter hook should be things related to the form declration itself. If you'd like to theme this in a particular way (for example, place the results in a table), set the form's #theme attribute:
<?php
$form['custom_form']['#theme'] = 'custom_form';
?>Then later, declare a function to handle the theming of this form. Here's an example from system.module:
<?php
function theme_system_user($form) {
foreach (element_children($form) as $key) {
$row = array();
if (is_array($form[$key]['description'])) {
$row[] = form_render($form[$key]['screenshot']);
$row[] = form_render($form[$key]['description']);
$row[] = form_render($form['theme'][$key]);
}
$rows[] = $row;
}
$header = array(t('Screenshot'), t('Name'), t('Selected'));
$output = theme('table', $header, $rows);
return $output;
}
?>