Creating a Node Module

Traditionally, when you wanted to create a new content type in Drupal, you would write a node module that takes responsibility for providing the new and interesting things your content type needs. We say traditionally, because recent advents within the Drupal framework allow you to create content types within the administrative interface and extend their functionality with contributed modules rather than writing a node module from scratch. We'll cover both solutions within this chapter.

Let's write a node module that lets users add jokes to a site. Each joke will have a title, the joke itself, and then a punchline. You should easily be able to use the built-in node title attribute for your joke titles and the node body for the joke contents, but you'll need to make a new database table to store the punchline. Here's the database schema:

CREATE TABLE joke ( nid int unsigned NOT NULL default '0', vid int unsigned NOT NULL default '0', punchline text NOT NULL, PRIMARY KEY (nid,vid), UNIQUE KEY vid (vid), KEY nid (nid)

You store the node's ID so you know which node to reference in the node_revisions table, which stores the title and body. The vid column is so you can use Drupal's built-in revision control for nodes. You'll see how to use this when writing the updates to the database. Start by creating a folder a named joke in your sites/all/modules/custom directory.

Make Money Writing

Make Money Writing

This Report Will Show You How To Make Money By Providing Writing Services To Other Internet Marketers. Learn how to make money by writing the right way. Grab your copy of this report now and learn. Why writing is a great way to earn money. How to compete with cheap writers, even if you charge a lot more money.

Get My Free Ebook

Post a comment