Programmatic CCK content type creation

Do you want your Drupal module to create a new content type using the power and flexibility of CCK, perhaps including the built-in integration with Views? Here's one way using Drupal 6. This requires the contributed CCK module (actually named "content") and any specific CCK field type modules for your application to be installed an enabled. It also requires the "content_copy" module to be enabled (it's included with the main CCK module). In the descriptions below, the example URLs all use example.com as the imaginary Drupal site. Substitute your domain name for example.com.

Step 1: Design your CCK content type using the Drupal content type editor

The menu path to create a new CCK content type is: Administer -> Content management -> Content types -> Add content type (http://example.com/admin/content/types/add). For more information about getting started with building a content type with CCK, visit this Drupal handbook page.

Step 2: Export your new CCK content type

  1. After you save your new content type in step 1 above, navigate to menu path Administer -> Content management -> Content types -> Export (http://example.com/admin/content/types/export).
  2. Select the content type you just created from the radio buttons displayed, then click the Export button.
  3. The next screen displays the fields for the selected content type.
  4. Take note of the Types of fields you used. If there are any which are not included with the basic CCK module (content.module), you need to make sure to include them in the dependencies for your module below, or it will be unable to create the content type.
  5. In general, you will want to use the default of exporting all of the fields. Make sure they are all selected via the checkboxes, then click the Export button.
  6. The next page produces a large textarea with the necessary PHP code to define your CCK content type. When ready to use this code, select all of the text in the textarea and copy it to your copy buffer to paste into your definition function, described below.

Step 3: Create a PHP function to hold your CCK definition

The empty template of the function looks like this. In the commented spaces, you'll paste the exported CCK content type definition code from step 2 above. We use a separate function just for the definition because it is must be updated with a new export from CCK if we decide to change or tweak our CCK content type later. It makes editing easier and less error-prone.

function _modulename_cck_export() {
 // paste code after this line.
 // paste code before this line.
 return $content;
}

Step 4: Carefully paste the exported CCK content type code into the function

The end result should look like the following. Note that your actual code will be different, based upon how you defined your content type. This is just an example.

function _modulename_cck_export() {
 // paste code after this line.
 $content[type] = array (
 'name' => 'computed moodle',
 'type' => 'computemoodle',
 'description' => 'Example CCK code.',
 'title_label' => 'Title',
 'body_label' => 'Body',
 'min_word_count' => '0',
 'help' => '',
 'node_options' => 
 array (
 'status' => true,
 'promote' => true,
 'sticky' => false,
 'revision' => false,
 ),
 'old_type' => '',
 'orig_type' => '',
 'module' => 'node',
 'custom' => '1',
 'modified' => '1',
 'locked' => '0',
 'content_profile' => false,
 'comment' => '2',
 'comment_default_mode' => '4',
 'comment_default_order' => '1',
 'comment_default_per_page' => '50',
 'comment_controls' => '3',
 'comment_anonymous' => 0,
 'comment_subject_field' => '1',
 'comment_preview' => '1',
 'comment_form_location' => '0',
 );
 $content[fields] = array (
 0 => 
 array (
 'label' => 'Example field',
 'field_name' => 'field_example',
 'type' => 'number_integer',
 'widget_type' => 'number',
 'change' => 'Change basic information',
 'weight' => '-1',
 'description' => '',
 'default_value' => 
 array (
 0 => 
 array (
 'value' => '',
 '_error_element' => 'default_value_widget][field_example][0][value',
 ),
 ),
 'default_value_php' => '',
 'default_value_widget' => NULL,
 'group' => false,
 'required' => 0,
 'multiple' => '0',
 'min' => '',
 'max' => '',
 'prefix' => '',
 'suffix' => '',
 'allowed_values' => '',
 'allowed_values_php' => '',
 'op' => 'Save field settings',
 'module' => 'number',
 'widget_module' => 'number',
 'columns' => 
 array (
 'value' => 
 array (
 'type' => 'int',
 'not null' => false,
 'sortable' => true,
 ),
 ),
 'display_settings' => 
 array (
 'label' => 
 array (
 'format' => 'above',
 'exclude' => 0,
 ),
 'teaser' => 
 array (
 'format' => 'default',
 'exclude' => 0,
 ),
 'full' => 
 array (
 'format' => 'default',
 'exclude' => 0,
 ),
 4 => 
 array (
 'format' => 'default',
 'exclude' => 0,
 ),
 ),
 ),
 );
 $content[extra] = array (
 'title' => '-5',
 'body_field' => '-3',
 'menu' => '-2',
 );
 // paste code before this line.
 return $content;
}

Step 5 (optional): Clean up the pasted code

You may want to edit string array indexes to be enclosed in single quote marks to avoid PHP warnings. I'm not sure why CCK exports faulty code like this. For example, as exported, the last array element is given as

$content[extra] = array (

but really should be expressed as

$content['extra'] = array (

in order to avoid PHP warning errors.

Step 6: Write PHP code to actually create the content type

Write the install code to initiate creation of the new content type. This is where we make sure we have the necessary CCK pieces, populate a form using the now hard-coded CCK export, and then use drupal_execute() to submit the form to create the new content type. Here's an example:

function _example_install_cck_node() {
 /* get the CCK node types to be created. This is where you load the 
  * file containing your function from above, if necessary, and then call
  * that function.
  */
 module_load_include('inc', 'modulename', 'modulename.ccknodedef');
 $content = _modulename_cck_export(); // in modulename.ccknodedef.inc
 // CCK content_copy.module may not be enabled, so make sure it is included
 require_once './' . drupal_get_path('module', 'content') .  '/modules/content_copy/content_copy.module';
 $form_state['values']['type_name'] = '<create>';
 $form_state['values']['macro'] = '$content = ' . var_export($content, TRUE) . ';';
 // form provided by content_copy.module 
 drupal_execute('content_copy_import_form', $form_state);
 content_clear_type_cache();
}

Step 7: Call the create function from your module

Figure out where you want to call the above install function from. I created an administrator button for creating it, because hook_install() uses the Batch API which is incompatible with programmitcally submitting the form with drupal_execute(). Thus, it's not possible to have the type easily created at install time when the module is enabled. See the bug at issue http://drupal.org/node/297972. This bug is actually now fixed in Drupal CVS, but is not yet in a release as of Drupal 6.10.

Step 8: Edit your *.info file

If your module requires your new CCK type to function, be sure to add the appropriate dependencies[] clauses in your modulename.info file. At a minimum, you will need to specify the CCK module, which is named content, and probably the content_copy module, to enable the content type creation.

All done.

That's it. Now your module can create a CCK content type it can use and will benefit from all of the other functionality, such as Views, which work so well with CCK.

Let me know if you have questions, corrections or improvements.

Thanks to Angie Byron (webchick) for pointing me at some existing code which helped me finish figuring out how to do this cleanly.

Tags:

Comments

I've heard you can use the

I've heard you can use the export feature for CCK to get the code required to create a content type for a module. This is the first place I've seen that explains HOW to do this.

Then there is the question of views... would I approach embedding views into a module in the same way?

Thanks!

Awesome. This is great. The

Awesome. This is great. The importing issue has also been resolved and is in 6.12.

Thanks for the quick

Thanks for the quick tutorial. It's a nice, quick-paced, and informative article.

@Anonymous - the views help

@Anonymous - the views help has a good explanation of what to do to set up a 'default' type view

Hello, I'm beginner in drupal

Hello,

I'm beginner in drupal module creation.
Can you please show me how to "Call the create function from your module"? Is that in .module we use to call the install function

Thank a lot for your help

1. create a .install file for

1. create a .install file for your module and the above function in it.
2. call this function from hook_install() or the hook_enable()

eg :


<?php
/** where 'm' is my module name **/

function m_enable() {
_m_install_cck_node();
}

function _m_install_cck_node() {
/* add the above function code */
}

I need your help. I am not

I need your help. I am not using the thematic theme and am using some other theme. I want to have a page which will list all categories along with their icons. How can I achieve that?
http://www.campionidipoker.it/

Thanks for taking the time to

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful and beneficial to your readers.

MMA's 10 best pound-for-pound fighters
Top 10 MMA Fighter
Top 10 Pound for Pound MMA Fighters Best Of The Best
Top 10 MMA Fighter
Automotive parts
mma pound for pound
gadget reviewers
Blog gadget review
Buying Vehicle Parts
Indonesia Traveling Tour

Post new comment

The content of this field is kept private and will not be shown publicly.
  • 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