Web design, programming, graphics, and pretty much anything else I care about.

Configure CCK fields on module installation

You can have a module installation automatically add and configure CCK field. First, setup the CCK fields on a development site exactly as you want them. Then, export the fields from /admin/content/types/export.

You will end up with the code for a php array that looks something like ...

<?php
$content
['type']  = array (
 
'name' => 'My Type',
 
'type' => 'my_type',
 
'description' => 'A type for adding CCK fields.',
 
'title_label' => 'Title',
 
'body_label' => 'Description',
 
'min_word_count' => '0',
 
'help' => '',
 
'node_options' =>
  array (
   
'status' => true,
   
'promote' => false,
   
'sticky' => false,
   
'revision' => false,
  ),
 
'old_type' => 'my_type',
 
'orig_type' => '',
 
'module' => 'node',
 
'custom' => '1',
 
'modified' => '1',
 
'locked' => '0',
);
$content['fields']  = array (
 
0 =>
  array (
   
'label' => 'Date & Time',
   
'field_name' => 'field_date',
   
'type' => 'datetime',
   
'widget_type' => 'date_select_repeat',
   
'change' => 'Change basic information',
   
'weight' => '-4',
   
'default_value' => 'blank',
   
'default_value2' => 'blank',
   
'default_value_code' => '',
   
'default_value_code2' => '',
   
'input_format' => 'Y-m-d H:i:s',
   
'input_format_custom' => '',
...
?>

Put this code into a file (with opening <?php) and give it any name. I prefer to name it module name.content.def.inc

In the module .install file add the following lines.

<?php
// Include the pre-defined node type and CCK fields.
include "my_module.content.def.inc";

// Invoke form, exactly as seen at /admin/content/types/import.
$form_state = array();
$form = content_copy_import_form($form_state, $content['type']['type']);
$form_state['values']['type_name'] = '<create>';
$form_state['values']['macro'] = '$content = '. var_export($content, 1) .';';
$form_state['values']['op'] = 'Import';

// Submit the form.
content_copy_import_form_submit($form, $form_state);
?>

This essentially invokes the CCK import form at /admin/content/types/import, inserts the data from the .inc file, and submits the form.

NOTE: The required CCK modules must be installed before hand. They cannot be installed at the same time as the module. The reason is module precedence cannot be controlled, so your module may try to install before the required CCK modules are installed.