Drupal Notes 6: module + new content type (non CCK)

Description: Module that uses a new content type (non CCK), that uses the Schema API, Node API, Database API, Forms API and the following hooks:

hook_form(), hook_install(), hook_uninstall(), hook_schema(), hook_node_info(), hook_perm(), hook_access(), hook_insert(), hook_update(), hook_delete(), hook_nodeapi(), hook_load(), hook_view(), hook_theme()

Step 1: sites/all/modules/personality folder

Step 2: personality.info

; $Id$
name = “Personality Content (Node) Type”
description = “This provides a custom content type to store simple online personalities”
core = 6.x
php = 5.1

Step 3: personality.install

// $Id$
/**
* Install the personality module, including its content (node) type.
* @file
*/

/**
* Implementation of hook_install()
*/

function personality_install(){
drupal_install_schema(‘personality’);
}
/**
* Implementation of hook_uninstall()
*/

function personality_uninstall(){
drupal_uninstall_schema(‘personality’);
}
/**
* Implementation of hook_schema()
*/

function personality_schema(){
$schema[‘personality’] = array(
‘fields’ => array(
‘vid’ => array(
‘type’ => ‘int’,
‘unsigned’ => TRUE,
‘not null’ => TRUE,
‘default’ => 0,
),
‘nid’ => array(
‘type’ => ‘int’,
‘unsigned’ => TRUE,
‘not null’ => TRUE,
‘default’ =>0,
),
‘dates’ => array(
‘type’ => ‘varchar’,
‘length’ => 127,
‘not null’ => TRUE,
‘default’ => ”,
),
// Note: On MySQL, text fields cannot have default values
‘life’ => array(
‘type’ => ‘text’,
‘not null’ => FALSE
),
‘works’ => array(
‘type’ => ‘text’,
‘not null’ => FALSE
),
),
‘indexes’ => array(
‘nid’ => array(‘nid’),
),
// Version is primary key.  Could do nid, vid.
‘primary key’ => array(‘vid’),
);
return $schema;
}

Step 4: personality.module

// $Id$
/**
* Provides the Personality content type
* @file
*/
/**
* Implementation of hook_help()
*/
function personality_help($path, $arg){
if($path == ‘admin/help#personality’){
$txt = ‘A personality is a description of a person\’s person.  Should include a brief summary, biography, date of birth, optionally date of death, and accomplishments.’;
$replace = array();
return ‘<p>’ . t($text, $replace) . ‘</p>’;
}
}
/**
* Imnplementation of hook_node_info()
*/

function personality_node_info(){
return array(
‘personality’ => array(
‘name’ => t(‘Personality’),
‘module’ => ‘personality’,
‘description’ => t(‘A personality of a person.’),
‘has_title’ => TRUE,
‘title_label’ => t(‘Personality of’),
‘has_body’ => TRUE,
‘body_label’ => t(‘Overview’),
)
);
}
/**
* Implementation of hook_form()
*/

function personality_form(&$node){
$type = node_get_types(‘type’, $node);
//Existing files: title (Personality of) and body (Overview)
if($type->has_title){
$form[‘title’] = array(
‘#type’ => ‘textfield’,
‘#title’ => check_plain($type->title_label),
‘#required’ => TRUE,
‘#default_value’ => $node->title,
‘#weight’ => -5,
);
}
if($type->has_body){
$form[‘body_field’] = node_body_field(
$node,
$type->body_label,
$type->min_word_count
);
}
// Our custom fields: Dates, Life, Works
$form[‘dates’] = array(
‘#type’ => ‘textfield’,
‘#size’ => 50,
‘#maxlength’ => 127,
‘#title’ => t(‘Dates’),
‘#description’ => t(‘Birth and death dates.’),
‘#default_value’ => isset($node->dates) ? $node->dates : ”,
);
$form[‘life’] = array(
‘#type’ => ‘textarea’,
‘#title’ => t(‘Life’),
‘#cols’ => 50,
‘#rows’ => 15,
‘#description’ => t(‘A description of this perons\’s life.’),
‘#default_value’ => isset($node->life) ? $node-life : ”,
);
$form[‘works’] = array(
‘#type’ => ‘textarea’,
‘#title’ => t(‘Works’),
‘#cols’ => 50,
‘#rows’ => 5,
‘#description’ => t(‘This person\’s Personality’),
‘#default_value’ => isset($node->works) ? $node->works : ”,
);
return $form;
}
/**
* Implements hook_perm()
* Responsible for registering new permissions
*/

function personality_perm(){
return array(
‘create personality node’,
‘edit personality nodes’,
‘delete personality nodes’,
);
}
/**
* Implementation of hook_access()
* which implements our new permissions
*/

function personality_access($op, $node, $account){
switch($op){
case ‘create’:
return user_access(‘create personality node’, $account);
case ‘update’:
return user_access(‘edit personality nodes’, $account);
case ‘delete’:
return user_access(‘delete personality nodes’, $account);
}
}
/**
* Implements hook_insert()
*/

function personality_insert($node){
if(!isset($node->life)){
$node->life = ”;
}
if(!isset($node->works)){
$node->works = ”;
}
db_query(
INSERT INTO {personality} (vid, nid, dates, life, works) VALUES (%d, %d, ‘%s’, ‘%s’, ‘%s’)“,
$node->vid,
$node->nid,
$node->dates,
$node->life,
$node->works
);
}
/**
* Implements hook_update()
*/

function personality_update($node){
if($node->revision){
personality_insert($node);
}
else{
db_query(
UPDATE {personality} SET dates = ‘&s’, life = ‘%s’, works = ‘%s’ WHERE vid = %d“,
$node->dates,
$node->life,
$node->works,
$node->vid
);
}
}
/**
* Implements hook_delete()
*/

function personality_delete($node){
db_query(
DELETE FROM {personality} WHERE nid = %d“,
$node->nid
);
}
/**
* This implementation deletes node revisions
* Implements hook_nodeapi()
*/

function personality_nodeapi(&$node, $op, $teaser, $page){
if($op == ‘delete revision’){
db_query(
DELETE FROM {personality} WHERE vid = %d‘,
$node->vid
);
}
}
/**
* Implementation of hook_load()
*/

function personality_load($node){
$result = db_query(
SELECT dates, life, works FROM {personality} WHERE vid = %d‘,
$node->vid
);
return db_fetch_object($result);
}
/**
* Implementation of hook_view()
*/

function personality_view($node, $teaser = FALSE, $page = FALSE){
$node = node_prepare($node, $teaser);
$dates = check_plain($node->dates);
$life = check_markup($node->life);
$works = check_markup($node->works);
// Add theme
$node->content[‘personality_info’] = array(
‘#value’ => theme(‘personality_info’, $dates, $life, $works),
‘#weight’ => 1,
);
return $node;
}
/**
* Implements hook_theme()
*/

function personality_theme(){
return array(
‘personality_info’ => array(
‘template’ => ‘personality_info’,
‘arguments’ => array(
‘dates’ => NULL,
‘life’ => NULL,
‘works’ => NULL,
),
),
);
}

Step 5: personality_info.tpl.php template theme file

// $Id$
/**
* Template to display personality nodes.
*
* Fields available:
* $dates: cleaned plain text string
* $life: cleaned HTML string
* $works: cleaned HTML string
*/

<div>
<h2>< ? php print t(‘Dates’); ? >:</h2>
< ? php print $dates; ? >
<h2>< ? php print t(‘Life’); ? >:</h2>
< ? php print $life; ? >
<h2>< ? php print t(‘Works’); ? >:</h2>
< ? php print $works; ? >
</div>

Comments are closed.