Drupal 6 Node Skeleton
Here is the node skeleton for Drupal 6 for anyone who cares and for me to reference :)
This contains hook_views_api which requires the views module. The code inside the implementation of hook_views_api assumes views exports are in mymodule/includes/*
The code inside hook_enable assumes that there is a cck export located in mymodule/includes/mymodule.cck
& Tom McCracken
*/
/**
* Implementation of hook_node_info().
*/
function mymodule_node_info() {
// We return an array since a module can define multiple node types.
// We're only defining one node type, type 'mymodule'.
return array(
'mymodule' => array(
'name' => t('My Node Type'), // Human readable name. Required.
'module' => 'mymodule', // Machine readable name. (alphanueric, '_' only) Required.
'description' => t('My Custom Node Type'), // Required.
'has_title' => TRUE,
'title_label' => t('Title'),
'has_body' => TRUE,
'body_label' => t('Content'),
'min_word_count' => 0,
'locked' => TRUE
)
);
}
/**
* Implementation of hook_menu().
*/
function mymodule_menu() {
return $items;
}
/**
* Implementation of hook_perm().
*/
function mymodule_perm() {
return array('create mymodule', 'edit own mymodule', 'edit any mymodule', 'delete own mymodule', 'delete any mymodule');
}
/**
* Implementation of hook_access().
*/
function mymodule_access($op, $node, $account) {
if ($op == 'create') {
return (user_access('create mymodule',$account));
}
else if ($op == 'update') {
return (user_access('edit any mymodule',$account) || (user_access('edit own mymodule',$account) && ($account->uid == $node->uid)));
}
else if ($op == 'delete') {
return (user_access('delete any mymodule',$account) || (user_access('delete own mymodule',$account) && ($account->uid == $node->uid)));
}
}
/**
* Implementation of hook_form().
*/
function mymodule_form($node) {
//echo "mymodule_form(node)\n";
// Get metadata for this node type
// (we use it for labeling title and body fields).
// We defined this in mymodule_node_info().
$node_info = mymodule_node_info();
$form['title'] = array(
'#type' => 'textfield',
'#title' => $node_info['mymodule']['title_label'],
'#default_value' => $node->title,
'#size' => 60,
'#maxlength' => 60,
'#weight' => -5,
);
$form['body_filter']['body'] = array(
'#type' => 'textarea',
'#title' => $node_info['mymodule']['body_label'],
'#default_value' => $node->body,
'#weight' => 0,
'#rows' => 20,
);
$form['body_filter']['filter'] = filter_form($node->format);
$form['body_filter']['#weight'] = -3;
return $form;
}
/**
* Implementation of hook_validate().
*/
function mymodule_validate($node) {
}
/**
* Implementation of hook_insert().
*/
function mymodule_insert($node) {
}
/**
* Implementation of hook_update().
*/
function mymodule_update($node) {
}
/**
* Implementation of hook_delete().
*/
function mymodule_delete(&$node) {
}
/**
* Implementation of hook_load().
*/
function mymodule_load($node) {
}
/**
* Implementation of hook_view().
*/
function mymodule_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;
}
/**
* Implementation of hook_views_api().
*/
function mymodule_views_api() {
return array(
'api' => 2,
'path' => drupal_get_path('module', 'mymodule') . '/includes',
);
}
/*
Careful with this code snippet. It does two things, only use if you know what you are doing.
function mymodule_enable() {
//for creating a new content type
//grab cck definition
$filename = drupal_get_path('module','mymodule') . "/includes/mymodule.cck";
$content = implode ('', file ($filename));
// Build form state
$form_state = array(
'values' => array(
'type_name' => '',
'macro' => $content,
'op' => 'Import',
'submitted' => 1
),
);
// Put it in there
drupal_execute('content_copy_import_form', $form_state);
//turn content type into a module instead of base node (not compatible with code above)
$nodeinfo = module_invoke('mymodule', '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);
}
}
*/
}