Drupal - Create new content type on hook_install

One of the best ways to keep this information in code is to use features. Feaures can put into code:

  • Content types
  • CCK fields
  • Permissions
  • Roles

The list goes on.

One nice feature about features is the drush integration

drush features will give you a list of all features on the site, and their status
drush features revert all will revert all features back to what is in code (really handy for running after deployments)

Help this helps


To answer your questions exactly:

Creating a content type in hook install: You use node_type_save() to create the content type, here's an example from webform.install:

  // Create the default webform type.
  $webform_type = array(
    'type' => 'webform',
    'name' => st('Webform'),
    'base' => 'node_content',
    'description' => st('Create a new form or questionnaire accessible to users. Submission results and statistics are recorded and accessible to privileged users.'),
    'custom' => TRUE,
    'modified' => TRUE,
    'locked' => FALSE,
  );

  $webform_type = node_type_set_defaults($webform_type);
  node_type_save($webform_type);
  node_add_body_field($webform_type);

What do do in hook_uninstall: Basically just cleanup after yourself, so delete variables your module created (using variable_del()), delete any files uploaded by the module (using file_unmanaged_delete_recursive()), delete defined content types (using node_type_delete()), etc.

Hope this helps!


Check out the D7 Examples module. node_example has install & uninstall.

You may be able to flesh out the type with the interface tools and then make a feature (with features module) & extract relevant parts into the install function of your custom module.

Tags:

7

Hooks