LevelTen Interactive

Drupal node module skeleton

Recipe for creating a node module skeleton:
1. Create three files using the code snippets at the bottom of this post

  • foo.info
  • foo.module
  • foo.theme

Note: replace foo with your module name. If this is for a specific project, use project codes in the format [pc][nodetype]

2. do the following search and replace on all three files
"[pc]foo" -> [pc][nodetype]
"foo" -> [nodetype]

foo.info code:

  1. ; $Id $
  2. name = "Foo"
  3. description = "Foo posts."
  4. package =
  5. dependencies =

foo.module code:

<?php
// $Id$
 
/**
 * @file
 * Provides a "foo" node type.
 */
include_once(drupal_get_path('module', 'foo') .'/foo.theme');
 
/**
 * Implementation of hook_node_info().
 */
function foo_node_info() {
  // We return an array since a module can define multiple node types.
  // We're only defining one node type, type 'foo'.
  return array(
    'foo' => array(
      'name' => t('Foo'), // Human readable name. Required.
      'module' => 'foo',  // Machine readable name. (alphanueric, '_' only) Required.
      'description' => t('This is foo'), // Required.
      'has_title' => TRUE,
      'title_label' => t('Title'),
      'has_body' => TRUE,
      'body_label' => t('Body'),
      'min_word_count' => 0,
      'locked' => TRUE
    )
  );
}
 
/**
 * Implementation of hook_menu().
 */
function foo_menu($may_cache) {
  $items = array(); 
 
  // Do not cache this menu item during the development of this module.
  if (!$may_cache) {
 
  } else {
 
  }
 
  return $items;
 
}
 
/**
 * Implementation of hook_perm().
 */
function foo_perm() {
    return array('create foo', 'edit own foo', 'edit foo');
}
 
/**
 * Implementation of hook_access().
 */
function foo_access($op, $node) {
	global $user;
 
	if ($op == 'create') {
		return (user_access('create foo'));
	}
 
	if ($op == 'update' || $op == 'delete') {
		return (user_access('edit foo') || (user_access('edit own foo') && ($user->uid == $node->uid)));
	}
}
 
/**
 * Implementation of hook_form().
 */
function foo_form($node) {
 
  $type = node_get_types('type', $node);
  if ($type->has_title) {
	  $form['title'] = array(
	    '#type' => 'textfield',
	    '#title' => check_plain($type->title_label),
	    '#default_value' => $node->title,
	    '#size' => 128,
	    '#maxlength' => 128,
	    '#weight' => -5,
	  );
  }
  if ($type->has_body) {
	  $form['body_filter']['body'] = array(
	    '#type' => 'textarea',
	    '#title' => check_plain($type->body_label),
	    '#default_value' => $node->body,
	    '#weight' => 0,
	    '#rows' => 10,
	  );
  }
  $form['body_filter']['filter'] = filter_form($node->format);
  $form['body_filter']['#weight'] = -4;
 
  return $form;
}
 
/**
 * Implementation of hook_validate().
 */
function foo_validate($node) {
 
 
}
 
/**
 * Implementation of hook_insert().
 */
function foo_insert($node) {
 
}
 
/**
 * Implementation of hook_update().
 */
function foo_update($node) {
 
}
 
/**
 * Implementation of hook_delete().
 */
function foo_delete(&$node) {
 
}
 
/**
 * Implementation of hook_load().
 */
function foo_load($node) {
 
}
 
/**
 * Implementation of hook_view().
 */
function foo_view($node, $teaser = FALSE, $page = FALSE) {
 
  if (!$teaser) {
    // Use Drupal's default node view.
    $node = node_prepare($node, $teaser);
  }
 
  if ($teaser) {
    // Use Drupal's default node view.   
  	$node = node_prepare($node, $teaser);
  }
 
  return $node;
}

foo.theme code:

<?php
 
function theme_l10foo_teaser($node) {
 
	return $output;
 
}

Syndicate content

©1999 - 2010 LevelTen Interactive - Dallas, TX