<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 431,  comments - 344,  trackbacks - 0
    Creating a Node Module

    1Creating 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$"

    2Creating 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}');

    }

    3Creating 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 周銳 閱讀(281) 評論(0)  編輯  收藏 所屬分類: PHP
    主站蜘蛛池模板: 哒哒哒免费视频观看在线www| 亚洲视频在线免费观看| 亚洲一级片在线播放| aa午夜免费剧场| 成人午夜性A级毛片免费| 久久91亚洲精品中文字幕| 精品久久久久久亚洲中文字幕| 1000部免费啪啪十八未年禁止观看| 亚洲精品国精品久久99热| 日韩亚洲产在线观看| 特级精品毛片免费观看| ZZIJZZIJ亚洲日本少妇JIZJIZ| 亚洲一卡2卡3卡4卡5卡6卡| 蜜桃成人无码区免费视频网站| 亚洲精品无码激情AV| 亚洲国产成人精品无码区花野真一| 色欲国产麻豆一精品一AV一免费 | 久久亚洲精品国产精品| a免费毛片在线播放| 免费看香港一级毛片| 亚洲成aⅴ人片在线观| 久久九九全国免费| 久久久久久亚洲精品不卡| 含羞草国产亚洲精品岁国产精品 | 国产综合激情在线亚洲第一页| 妻子5免费完整高清电视| 亚洲av女电影网| a级毛片免费播放| 亚洲精品麻豆av| 国产精品亚洲专区在线播放 | 亚洲AV午夜成人片| japanese色国产在线看免费| 国产精品另类激情久久久免费| 亚洲中文字幕无码久久2020| 在线精品一卡乱码免费| 亚洲韩国在线一卡二卡| 亚洲精品免费观看| 亚洲欧洲一区二区| 日本一道本不卡免费| 亚洲大尺度无码专区尤物| 成人无码a级毛片免费|