We are working on a module that creates a custom content type. I know we could use CCK to create the content type, but we want this to be in a module so we can drop it in to new installations.
The content type is created, but we want to add CCK fields to the content type programatically so we can use Views with our content type. Is there a way to do that in the install file?

Comments

just_like_good_vibes’s picture

Hello (my first post on drupal.org :) )
i've got exactly the same question, trying to figure out how to do it,
i've been "googling" it and found this thread interesting http://www.poplarware.com/articles/cck_field_module
hope it helps

duckzland’s picture

something like this perhaps ?


function your_module_name_enable() { 
   // Get the files content
   $filename = drupal_get_path('module','your_module_name') . "/your_module_cck_export_file.cck";
  $content = implode ('', file ($filename));
  
   // Build form state
    $form_state = array(
       'values' => array(
            'type_name' => '<create>',
            'macro' => $content,
      ),
     );

   // Put it in there
     drupal_execute("content_copy_import_form", $form_state);

}

put the code above in the .install file, and create your_module_cck_export_file.cck file with the content from exported cck fields

you can export the cck fields then copy paste the content to the your_module_cck_export_file.cck.

Sample of .cck file

$content['type']  = array (
  'name' => 'my content type',
  'type' => 'my content type',
  'description' => 'my content type',
  'title_label' => 'Title',
  'body_label' => 'Body',
  'min_word_count' => '0',
  'help' => '',
  'node_options' => 
  array (
    'status' => true,
    'promote' => false,
    'sticky' => false,
    'revision' => false,
    'moderate' => false,
  ),
  'language_content_type' => '0',
  'upload' => '1',
  'addthis_nodetype' => 1,
  'old_type' => 'orchid_event',
  'orig_type' => '',
  'module' => 'node',
  'custom' => '1',
  'modified' => '1',
  'locked' => '0',
);
$content['fields']  = array (
  0 => 
  array (
    'label' => 'Event Host',
    'field_name' => 'field_host',
    'type' => 'text',
    'widget_type' => 'text_textfield',
    'change' => 'Change basic information',
    'weight' => '3',
    'rows' => 5,
    'size' => '60',
    'description' => '',

............. more

--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com

sebsebseb123’s picture

Hi Everyone,

I've been reading about how I can programmatically create a content type with cck fields. But, most of the solutions only create one particular content type on module_enable() ... just like above... But, what if I want to let the user pick the structure of the content type, based on a pre-defined set of fields. So, we could create several different content_types... as the user sees fit... also, for them to be able to add or remove fields later.. or maybe later define a field as "Required"

...most of the examples I've seen use the .cck export files... I suppose, since I only have 7 different fields they can pick from, (name, address, city, etc) then I could create a seperate file for each specific field. Then, ... ?

I fear I might have to go through the entire cck module and deconstruct it and call specific functions based on what I want to do with the dynamic content types.

...any tips on programmatically adding, removing, or changing the cck fields?

duckzland’s picture

Your requirement sound like the function of the cck module itself. maybe by giving certain user roles the permission to create content type and the 7 cck fields can accomplish the task.

--------------------------------------------------------------------------------------------------------
if you can use drupal why use others?
VicTheme.com

mgqn’s picture

Hello
You managed to do what you posed, I have many days searching for exactly what your pants but can not get anything.

I would greatly appreciate if you show me how you did, very much.