Drupal Content Type Module Skeleton

If you're a big fan of CCK, you've probably noticed that there are limitations eventually. It does fantastic work with data, but not so much with the nodes themselves. If you need things like access rights, redirection after XYZ, or custom permissions for business logic, you will like end up having to create your own content type through a module. Following is a very simple copy paste you can use to get started. It's got the most common hooks in there. Just copy and replace 'node_type' with whatever you wish yours to be. I'll include a .info, .install, and .module to make things extra cheesy.

node_type.info

  1. name = Content Type
  2. description = Description
  3. package = Custom
  4. version = VERSION
  5. core = 6.x

node_type.module

  1. /**
  2.  * Implementation of hook_node_info
  3.  */
  4. function node_type_node_info() {
  5.   return array(
  6.     'node_type' => array(
  7.       'name' => t('node_type'),
  8.       'module' => 'node_type',
  9.       'description' => t('Description.'),
  10.     )
  11.   );
  12. }
  13.  
  14. /**
  15.  * Implementation of hook_perm
  16.  */
  17. function node_type_perm() {
  18.   return array(
  19.     'create node_type'
  20.     ,'edit own node_type'
  21.     ,'edit any node_type'
  22.     ,'delete own node_type'
  23.     ,'delete any node_type'
  24.     ,'view own node_type'
  25.     ,'view any node_type'
  26.   );
  27. }
  28.  
  29. /**
  30.  * Implementation of hook_access().
  31.  */
  32. function node_type_access($op, $node, $account) {
  33.   switch ($op) {
  34.     case 'create':
  35.       if (user_access('create node_type', $account)) {
  36.         return TRUE;
  37.       }
  38.       break;
  39.  
  40.     case 'update':
  41.       if (user_access('edit any node_type', $account) || ($account->uid == $node->uid && user_access('edit own node_type', $account))) {
  42.         return TRUE;
  43.       }
  44.       break;
  45.  
  46.     case 'delete':
  47.       if (user_access('delete any node_type', $account) || ($account->uid == $node->uid && user_access('delete own node_type', $account))) {
  48.         return TRUE;
  49.       }
  50.       break;
  51.     case 'view':
  52.       if (user_access('view any node_type', $account) || ($account->uid == $node->uid && user_access('view own node_type', $account))) {
  53.         return TRUE;
  54.       }
  55.       break;
  56.   }
  57. }
  58.  
  59. /**
  60.  * Implementation of hook_form().
  61.  */
  62. function node_type_form(&$node, $form_state) {
  63.   // The site admin can decide if this node type has a title and body, and how
  64.   // the fields should be labeled. We need to load these settings so we can
  65.   // build the node form correctly.
  66.   $type = node_get_types('type', $node);
  67.  
  68.   if ($type->has_title) {
  69.     $form['title'] = array(
  70.       '#type' => 'textfield',
  71.       '#title' => check_plain($type->title_label),
  72.       '#required' => TRUE,
  73.       '#default_value' => $node->title,
  74.       '#weight' => -5
  75.     );
  76.   }
  77.  
  78.   if ($type->has_body) {
  79.     // In Drupal 6, we use node_body_field() to get the body and filter
  80.     // elements. This replaces the old textarea + filter_form() method of
  81.     // setting this up. It will also ensure the teaser splitter gets set up
  82.     // properly.
  83.     $form['body_field'] = node_body_field($node, $type->body_label, $type->min_word_count);
  84.   }
  85.  
  86.   return $form;
  87. }
  88.  
  89. /**
  90.  * Implementation of hook_validate()
  91.  */
  92. function node_type_validate($form, &$form_state) {
  93.    
  94.  
  95. }
  96.  
  97. /**
  98.  * Implementation of hook_view
  99.  */
  100. function node_type_view($node, $teaser = 0, $page = 0) {
  101.   $node = node_prepare($node, $teaser);
  102.  
  103.   return $node;
  104. }
  105.  
  106. /**
  107.  * Implementation of hook_insert().
  108.  */
  109. function node_type_insert($node) {
  110.  
  111. }
  112.  
  113. /**
  114.  * Implementation of hook_update().
  115.  *
  116.  */
  117. function node_type_update($node) {
  118.  
  119.  
  120. }
  121.  
  122. /**
  123.  * Implementation of hook_delete().
  124.  */
  125. function node_type_delete($node) {
  126.  
  127.  
  128. }  

node_type.install

  1. /**
  2.  * Implementation of hook_schema().
  3.  */
  4. /*
  5. function node_type_schema() {
  6.   $schema['node_type'] = array(
  7.  
  8.   );
  9.   return $schema;
  10. }
  11. */
  12.  
  13. /**
  14.  * Implementation of hook_install().
  15.  */
  16. function node_type_install() {
  17.   //drupal_install_schema('node_type');
  18.  
  19. }
  20.  
  21. /**
  22.  * Implementation of hook_uninstall().
  23.  */
  24. function node_type_uninstall() {
  25.   //drupal_uninstall_schema('node_type');
  26.   // Advisable to loop over all nodes owned by this module and call node_delete() to clear out system references
  27. }
Tags: 

Comments

Please understand that while this is a very nice skeleton, it is not meant for exact copy paste. Anytime you add additional content types, they should be merged into a custom module added at once, not seperate for each.

Add new comment

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.