4

I have to write a module that provides its own content type. In the drupal-documentation there is a guide for that; yet, I would have to deal with the CRUD logic inside of my module. I thought I could use CCK to create my new content-type with its fields inside of hook_install();. But I couldn't find any guides on how to do that.

Can anyone give a short example or provide a link to some decent guide? That would be awesome.

4 Answers 4

4

CCK includes a module named "Content Copy." Enabling that will give you an export tab in the edit section for each content type. That tab will give you code you can copy into a file within your module. If you save that file to /cck/[type-name].cck and set values for $type_name and $module_name, you can use this code in your hook_install() to add the content type:

  $path = $_SERVER['DOCUMENT_ROOT'] . base_path() . drupal_get_path('module', $module_name) . '/cck/' . $type_name . '.cck';
  $content_type = file_get_contents($path);

  $form_state = array();
  $form = content_copy_import_form($form_state, $type_name);

  $form_state['values']['type_name'] = '<create>';
  $form_state['values']['macro'] = $content_type;
  $form_state['values']['op'] = t('Import');

  content_copy_import_form_submit($form, $form_state);
1
  • Wow! Will try that out next week. This sounds great.
    – nocksock
    Commented Apr 22, 2011 at 8:16
5

If you goal it to create your own content type, but don't want to deal with the hassle of CRUD, you could use features. It's designed for doing stuff like this.

CCK doesn't actually handle creating new content types, that is part of the node module (Drupal core). If you want to add a node, as if it was done through the interface and don't want to use features you should use hook_node_info:

/**
 * Implementation of hook_node_info().
 */
function module_node_info() {
  $items = array(
    'activity' => array(
      'name' => t('Name'),
      'module' => 'node',
      'description' => t('Description.'),
      'has_title' => '1',
      'title_label' => t('Title'),
      'has_body' => '1',
      'body_label' => t('Body'),
      'min_word_count' => '0',
      'help' => '',
    ),
  );
  return $items;
}

Setting node to be the owner of the node type lets it handle CRUD etc.

4
  • Yup, I'd like to avoid using features, in order to keep the amount of dependencies low. How can I add fields using cck? I assume "activity" is the machine-name of the content-type? I will have look into this myself but I'm sure me and other people here might benefit from some elaborations. :)
    – nocksock
    Commented Apr 20, 2011 at 14:25
  • When node.module it set as the owner of the content type, it also adds the content permissions: create <content-type> content, delete own <content-type> content, delete any <content-type> content, edit own <content-type> content, edit any <content-type> content.
    – apaderno
    Commented Apr 20, 2011 at 14:32
  • @Nils R: Yeah activity is the machine name. Once the node is defined, it should act as is if were created through the interface, and show up in the list of nodes with the manage fields etc. You will need to clear caches though.
    – googletorp
    Commented Apr 20, 2011 at 14:34
  • in fact i'd like to add the fields programmatically as well. So I'd need some sort of an install and uninstall routine.
    – nocksock
    Commented Apr 20, 2011 at 14:35
4

Creating Custom CCK Fields is useful to understand how to create custom CCK fields, widgets, and formatters; the example code should be enough to let you start.
What is the Content Construction Kit? A View from the Database is an introduction about CCK for Drupal 5, but it is still valid for Drupal 6.
Creating a custom compound field for CCK is another interesting article that explain how to create a CCK field that uses more than one form field for its input.
Programatically creating a CCK field in Drupal 6 explains how to create a field from code, for example in the installation code of a module that needs to add a specific field for a content type.

2

Your best reference will be to take a look at the Examples module.

1
  • The node example in Examples provides NON-CCK fields. It does not demonstrate building a CCK-type field.
    – rfay
    Commented Apr 20, 2011 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.