Creating a Node Module
1.Creating the .info File
Let’s also create the joke.info file and add it to the joke folder.
; $Id$
name = Joke
description = Provides a joke node type with a punchline.
version = "$Name$"
2.Creating the .install File
<?php
function joke_install() {
       switch ($GLOBALS['db_type']) {
              case 'mysql':
              case 'mysqli':
                     db_query("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)
                     ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
                     break;
              case 'pgsql':
                     db_query("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)
                     )");
              break;
       }
}
function joke_uninstall() {
       db_query('DROP TABLE {joke}');
}
3.Creating the .module File
<?php
function joke_perm() {
       return array('create joke', 'edit joke', 'delete joke');
}
/**
* @file
* Provides a "joke" node type.
*/
/**
* Implementation of hook_node_info().
*/
function joke_node_info() {
       // We return an array since a module can define multiple node types.
       // We're only defining one node type, type 'joke'.
       return array(
              'joke' => array(
                     'name' => t('Joke'), // Required.
                     'module' => 'joke', // Required.
                     'description' => t('Tell us your favorite joke!'), // Required.
                     'has_title' => TRUE,
                     'title_label' => t('Title'),
                     'has_body' => TRUE,
                     'body_label' => t('Joke'),
                     'min_word_count' => 2,
                     'locked' => TRUE
              )
       );
}
function joke_menu($may_cache) {
       $items = array();
       if ($may_cache) {
              $items[] = array(
                     'path' => 'node/add/joke',
                     'title' => t('Joke'),
                     'access' => user_access('create joke'),
              );
       }
       return $items;
}
function joke_access($op, $node) {
       global $user;
       if ($op == 'create') {
              return (user_access('create joke'));
       }
       if ($op == 'update') {
              return (user_access('edit joke') && ($user->uid == $node->uid));
       }
       if ($op == 'delete') {
              return (user_access('delete joke') && ($user->uid == $node->uid));
       }
}
function joke_form($node) {
       // Get metadata for this node type
       // (we use it for labeling title and body fields).
       // We defined this in joke_node_info().
       $type = node_get_types('type', $node);
       $form['title'] = array(
              '#type' => 'textfield',
              '#title' => check_plain($type->title_label),
              '#required' => TRUE,
              '#default_value' => $node->title,
              '#weight' => -5
       );
       $form['body_filter']['body'] = array(
              '#type' => 'textarea',
              '#title' => check_plain($type->body_label),
              '#default_value' => $node->body,
              '#rows' => 7,
              '#required' => TRUE
       );
       $form['body_filter']['filter'] = filter_form($node->format);
       $form['punchline'] = array(
              '#type' => 'textfield',
              '#title' => t('Punchline'),
              '#required' => TRUE,
              '#default_value' => $node->punchline,
              '#weight' => 5
       );
       return $form;
}
function joke_validate($node) {
       //Enforce a minimum word length of 3.
       if (isset($node->punchline) && str_word_count($node->punchline) <= 3) {
              $type = node_get_types('type', $node);
              form_set_error('punchline', t('The punchline of your @type is too short. You need at least three words.', array('@type'=> $type->name)));
       }
}
function joke_insert($node) {
       db_query("INSERT INTO {joke} (nid, vid, punchline) VALUES (%d, %d, '%s')",
       $node->nid, $node->vid, $node->punchline);
}
function joke_update($node) {
       if ($node->revision) {
              joke_insert($node);
       } else {
              db_query("UPDATE {joke} SET punchline = '%s' WHERE vid = %d", $node->punchline, $node->vid);
       }
}
/**
* Implementation of hook_delete().
*/
function joke_delete(&$node) {
       // Delete the related information we were saving for this node.
       db_query('DELETE FROM {joke} WHERE nid = %d', $node->nid);
}
/**
* Implementation of hook_load().
*/
function joke_load($node) {
       drupal_add_js('misc/collapse.js');
       return db_fetch_object(db_query('SELECT punchline FROM {joke} WHERE vid = %d', $node->vid));
}
function joke_view($node, $teaser = FALSE, $page = FALSE) {
       if (!$teaser) {
              // Use Drupal's default node view.
              $node = node_prepare($node, $teaser);
              $node->guffaw = str_repeat(t('Ha!'), mt_rand(0, 10));
              // Now add the punchline.
              /**
              $node->content['punchline'] = array(
                     '#value' => theme('joke_punchline', $node),
                     '#weight' => 2
              );
              */
       }
       if ($teaser) {
              // Use Drupal's default node view.
              $node = node_prepare($node, $teaser);
       }
       return $node;
}
function theme_joke_punchline($node) {
       $output = '<div class="joke-punchline">'.check_plain($node->punchline). '</div><br />';
       $output .= '<div class="joke-guffaw">'.check_plain($node->guffaw). '</div>';
       return $output;
}
	posted on 2007-12-04 09:28 
周锐 阅读(304) 
评论(0)  编辑  收藏  所属分类: 
PHP