Migrating Drupal 6 CCK Programmatically – Add New Fields

Here is a method for adding new CCK fields to an existing content types and distributing those changes to multiple environments. See previous post for adding an entire content type.

Install the Install Profile API module from http://drupal.org/project/install_profile_api.

  1. Use the Drupal GUI to add the cck fields to the existing content type and place them in the desired order.
  2. Export the fields at /admin/content/types/export. Select the type, Export, select ONLY THE NEW FIELDS, Export. Copy Export data to the clipboard.
  3. Create a new file named {content_type}_update_X.inc in the sites/all/your_module directory, where X is greater than the previous update for that content type.
    Example: article_update_02.inc
  4. Add "<?php" to the first line of the file and paste the export data on the second line. Save file.
  5. Add the following lines to a new update function, your_module_update_X(), where X is one higher than the previous update. Replace "article" with the name of your content type.
    1. $file = drupal_get_path('module', 'your_module') . '/article_update_X.inc';
    2. install_copy_import_from_file($file, 'article');

    NOTE: For adding fields to existing content types, second parameter must be the content type.
  6. Code any permissions that need to be assigned to roles for these new fields.

Save the custom module to your version control system. Distribute code to your other environments. Run update.php in each environment.

Tags: 

Comments

hmm i always get an error with this function... I use it in my module but i cant find out why it fails during the installation process of my profile. Any Ideas howto import the additionally cck fields to my hardcoded node_types?

These days, another good solution is the use the features module. http://drupal.org/project/features

Add new comment