01. Getting started
Having read the previous tutorial, we can start with the basics of a module. None of this should be new. Functions that are new-node-type specific functions aren't included in this skeleton module, so that they can be described later.
In particular, we'll define the help, perm, access and menu hooks. We won't completely fill in the functionality for these functions, we just want enough to get started developing our new-node-type module.
Note that here hook_access is implemented as "todo_access" not because of the module's name (todo) but after the node type name (that incidentally is the same: "todo"). If your module (module name 'a') defines 2 or more node types in the hook_node_info (http://api.drupal.org/api/4.7/function/hook_node_info) respectively named 'b' and 'c' you can have two separate hook_access implementations: b_access and c_access.
<?php
/**
* @file
* submit, view, and manage to-do items and lists
*/
/**
* Implementation of hook_help()
* Display help text for the todo module
*/
function todo_help($section) {
switch ($section) {
case 'admin/help#todo':
$o .= '<p>' . t('Submit, view and manage todo lists.') . '</p>';
return $o;
case 'admin/modules#description':
return t('Allows users to submit, view and manage to-do items and lists.');
case 'node/add#todo':
return t('Add a task as a to-do item here. You can assign a due date and priority, as desired');
}
}
/**
* Implementation of hook_perm().
* Define the permissions this module uses
*/
function todo_perm() {
return array('create todo items', 'manage own todo items');
}
/**
* Implementation of hook_access().
*/
function todo_access($op, $node) {
global $user;
if ($op == 'create') {
return user_access('create todo items');
}
if ($op == 'update' || $op == 'delete') {
if (user_access('manage own todo items') && ($user->uid == $node->uid)) {
return TRUE;
}
}
}
/**
* Implementation of hook_menu().
*/
function todo_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array('path' => 'node/add/todo', 'title' => t('todo'),
'access' => user_access('create todo items'));
}
return $items;
}
?>