Turning a content type into a module

Turning a content type into a module

If you create a content type through the administration menu that content type (i.e. mynodetype) will be derived from the node module. If you want to extend functionality of that content type by providing hooks, you cannot do it by creating a new module with the name of the content type (i.e. mycontenttype.module) and enabling it. If you try that Drupal will still see the content type deriving from node.module. You can modify your custom content type to derive from your custom module instead of node.module with the following hook in mynodetype.module without losing any data. Even cck fields will carry over. This eliminates the need to create a node skeleton for every new content type. /** * Implementation of hook_enable **/ function mynodetype_enable() { $nodeinfo = module_invoke('mynodetype', 'node_info'); //node_info may define multiple content types. Cycle through each one. foreach($nodeinfo as $nodetype => $info) { $current_module = db_result(db_query("SELECT module FROM {node_type} WHERE type='%s'", $nodetype)); $new_module = $info['module']; if ($current_module == 'node') { db_query("UPDATE {node_type} SET module = '%s' WHERE type='%s'", $new_module, $nodetype); node_types_rebuild(); drupal_set_message($info['name'] . ' now derives from module ' . $new_module); } } }

Related Posts

Drupal Tutorial: How to Sort Search Results by Content Type

Kristin Brinner
Read more

Drupal Content Optimizer Module Makes SEO a Breeze

Chris Sloan
Read more

Turning a Chicken Into a Pig

Tom McCracken
Read more

Think Before You Add A Module

Felipa Villegas
Read more

Futureproof Your Content: The Rise of Adaptive Content Architecture

Tom McCracken
Read more

Stop Spam, Not Your User - The un.captcha.lous Module Demo

Michael Kasberg
Read more