How to create content types / CCK programatically in Drupal 6

How to create content types and or CCK fields in your module install/update code? I actually found this howto somewhere but I keep looking for it all the time. Here it is:

Step 1

Click the content type in UI and export it using Content Copy module into yourmodule.content.crud.inc file. Add <?php to the start of the file.

Step 2

Don't forget to add proper dependencies into it's .info file:

dependencies[] = content
dependencies[] = content_copy
dependencies[] = any_other_cck_modules

Now put this into content of yourmodule_install():

$content = array();
include "yourmodule.content.robinhood_profile.inc";

$form_state = array();
$form = content_copy_import_form($form_state, $type_name);
$form_state['values']['type_name'] = $type_name ? $type_name : '';
$form_state['values']['macro'] = '$content = '. var_export($content, 1) .';';
$form_state['values']['op'] = t('Import');
content_copy_import_form_submit($form, $form_state);

Voila! When you enable your module, your new content type will be created, including the CCK fields.

Jakub Suchý

Jakub is owner and founder of Dynamite Heads. Jakub is a member of Drupal Security Team and supports Czech Drupal community at Drupal.cz

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

CAPTCHA
Are you a robot?
Image CAPTCHA
Enter the characters shown in the image.