Recently I wanted to add a CCK combo field into a custom form. Why would you need to do this? Well, my reason was to re-use a combo field with the 'add another item' functionality. Using the node form was not an option.
In this example we have a CCK combo field named "guests". Each guest has a first name, last name, and email address. The content type the guests field is in is my_event.
When adding this code to your form function, the guests field will display just as it does on the node form.
<?php
// include the content file
module_load_include('inc', 'content', 'includes/content.node_form');
$field = content_fields('field_guests', 'my_event');
$form['#field_info']['field_guests'] = $field;
$form += (array) content_field_form($form, $form_state, $field);
?>
Next thing I needed to do was pre-populate the guest information based on the node. If you know of a better way to achieve this, please comment! ... however, this was all I could come up with.
<?php
if (is_array($node->field_guests)) {
$delta = 0;
// reults always appear in wrong order, so reverse sort them.. not sure why
rsort($node->field_guests);
// loop through the guests in the node and set the default values
foreach ($node->field_guests as $item) {
$form['field_guests'][$delta] = $default_item;
$form['field_guests'][$delta]['firstname']['#default_value'] = $item['firstname'];
$form['field_guests'][$delta]['lastname']['#default_value'] = $item['lastname'];
$form['field_guests'][$delta]['email']['#default_value'] = $item['email'];
$delta++;
}
}
?>