Programmatic CCK Content Types – Updated

Not long ago, I published a HowTo for creating CCK Content Types programmatically, including the ability of updating them via code. After some events, the HowTo needed an update, as it now (necessarily) depends on the Alternate Content Copy module.

Step 0: pick up a name and set up your module

In this post I will be using example_cck_content as name for both the module and the content type. So, let’s create a folder called example_cck_content and underneath that the following files:

  • example_cck_content.module
  • example_cck_content.install
  • example_cck_content.info
  • example_cck_content.def.inc

The first three files are common to almost every Drupal module. The last file will host the exported content type.

Step 1: create your content type using CCK

To create a new CCK content type go to: Administer » Content management » Content types » Add content type. For more information about this, read Getting started with CCK.

Step 2: export your newly created content type

To export the content type we just created, let’s go to Administer » Content management » Content types » Export. Follow the wizard instructions until you get the screen with the code exported (read more details about the steps in this wizard in the Step 2 of this post). The code exported will look like the following

Step 3: paste the code in the module

Let’s go back to the module we have created at the Step 0 and open the file example_cck_content.def.inc. In this file we will create a stub function called

Now, let’s go back to our Drupal site and copy the code from the screen we have got at the Step 2. Then, let’s paste it in place of the comment I put in the stub function above.

Step 4: write the content type create/update function

The function we are about to create will be invoked from both the hook_install and the hook_update_N. It invokes some CCK APIs in order to actually create/update the content type structure into the database.

So, let’s open the example_cck_content.install and let’s write down the following function

Step 5: install, uninstall and update_N hooks

Here it comes the interesting part. Like in the classical way, we can implement the hook_install to actually create the exported content type into the database, then implement the hook_uninstall to remove the content type when the module is uninstalled. Plus, a nice addition that other tutorials did not write in the past: the possibility of implementing the hook_update_N. The latter is very important to amend the structure of a content type when this is already on production.

So, let’s keep open the example_cck_content.install and write down the following.

The very interesting bit is about the hook_update_N. Unlike the classical way, to amend a content type we will be using the CCK interface, by going to Administer » Content management » Content types and then clicking the Edit for our content type. Once done, we will be exporting it again following the Steps 2 and 3. Finally, we will add a hook_update_N exactly the way it is showed in the commented code above. The hook_update_N will all look the same.

Step 6: handling dependencies

One mandatory dependency is the content module, also known as CCK. Plus, depending on which kind of fields you have added to your content type, you might need to make your module dependent on one or more CCK fields module (e.g. filefield, etc.).

In order to do so, we need to specify the dependencies in our example_cck_content.info file:

Those are the actual dependencies of the module I created (and that you can download at the bottom of this post).

However, this is not going to work properly (hopefully, until Drupal 7). In fact, since we need those dependencies enabled and installed before our hook_install will be fired, but there is currently no mechanism that ensures that hook_install is run for dependencies before the dependent modules, the installation of our content type might fail if it depends on one or more currently disabled modules.

In order to enforce a stronger dependencies handling, we can only rely on the hook_requirements for the time being. This hook implementation must reside in the example_cck_content.install.

Step 7: download and test

You can download the example module I created following this procedure and test that everything works as described. Enjoy!

Tags: , ,

15 Responses to “Programmatic CCK Content Types – Updated”

  1. Tony →
    27 August 2010 at 5:41 am #

    Good tutorial!

    I implemented this all for a CCK that I created and I get no errors but the content type doesn’t get added to the database.

  2. Tony →
    28 August 2010 at 6:06 am #

    Not sure if this post is being read any more but when I do the uninstall it doesn’t delete the content type. If I then manually delete it you can’t then reinstall the module and have the content type appear.

  3. Vincenzo
    29 August 2010 at 10:10 am #

    @Tony: check for typos or other mistakes – the hook_uninstall as I wrote it removes the content type perfectly.

  4. Tony →
    29 August 2010 at 11:09 pm #

    I thought that might have been the problem but I downloaded the package from this site and used it exactly. The module gets uninstalled but all the content type stuff remains. Tried this on 2 different servers with Drupal 6.19.

    I found another comment in the drupal.org forums about this problem with node_type_delete.

  5. Tony →
    29 August 2010 at 11:17 pm #

    I should mention I’m using CCK 6.x-3 dev stream. This is the issue someone else logged.
    http://drupal.org/node/740684

  6. Vincenzo
    30 August 2010 at 11:36 am #

    I need to investigate this. On 2.x version it’s always worked without problems. I’ll keep you posted. Please do the same.

  7. Tony →
    30 August 2010 at 3:52 pm #

    Thanks! Just so you know it’s not just your method that the node_type_delete doesn’t work for. I’ve tried the other ones you’ve listed on your site and it’s the same thing. Doesn’t delete the node type and when it’s deleted manually the module can’t recreate it. Imports from the UI work though.

  8. pwaterz
    8 December 2010 at 9:52 pm #

    GREAT POST! Mad props, worked like a charm. You made my life so much easier!

  9. Vincenzo Russo
    9 December 2010 at 2:11 pm #

    You’re welcome Patrick!

  10. andrew →
    21 December 2010 at 12:39 am #

    I have the same problem as Tony. Installation works fine, but upon removal, the content type is still there. I’m using a clean install of drupal 6.2 with cck 2.8.

    Also, do I have to call my module “name”_cck_content? I used just “member” (I know that’s a bad idea) and installing the module didn’t create the content type.

    Thanks for the tut tho, I learned a lot.

  11. molen grew
    8 March 2011 at 1:54 pm #

    Useful content! The module gets uninstalled but all the content type stuff remains. Tried this on 2 different servers with Drupal 6.19. Thanks buddy!

Trackbacks/Pingbacks

  1. Lifestream for December 3rd — Lifestream - 3 December 2009

    [...] #CCK content type Updated http://neminis.org/blog/drupal/programmatic-cck-content-types-updated/ #drupal #php [enzoru] — 5h ago via [...]

  2. Domus Neminis » Programmatic CCK Content Types: why my way? - 7 December 2009

    [...] In another post I explained how to get programmatic CCK content types done, including a programmatic update. My way of doing this last bit only works if the Alternate Content Copy module is used. [...]

  3. [Vincenzo Russo] Programmatic CCK Content Types: why my way? – techPortal - 7 December 2009

    [...] In another post I explained how to get programmatic CCK content types done, including a programmatic update. My way of doing this last bit only works if the Alternate Content Copy module is used. [...]

  4. Programmatically create product classes |Prestashow.com - 17 September 2010

    [...] The code I am using is http://neminis.org/blog/drupal/programmatic-cck-content-types-updated/ [...]