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:
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
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.
Step 7: download and test
You can download the example module I created following this procedure and test that everything works as described. Enjoy!