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.

Was this article helpful?

0 0
Article Marketing Knockdown

Article Marketing Knockdown

The lifeblood of any online business is in being able to generate highly targeted, consistent traffic to your websites, landing pages and squeeze pages.

Get My Free Ebook

Post a comment