drupal: สร้างมอดูลเอง (Drupal 6 Module Tutorial)

เอามาจาก Creating modules - a tutorial: Drupal 6.x
พยายามเขียนให้เป็นเรื่องเป็นราว แต่ให้สั้น ๆ

ตามตัวอย่างเป็นการสร้างมอดูลชื่อ onthisdate เพื่อจะทำเป็นบล๊อกแสดง "วันนี้ในอดีตเมื่ออาทิตย์ก่อน"

ลิงก์ที่ต้องไป

Topic: 

01. เริ่มต้น (Getting Started)

ควรเขียนไว้ภายใต้ site/all/module/onthisdate เพื่อไม่ให้ปนกับของ Drupal เอง และปลอดภัยจากการอัปเกรด
$ cd /var/www/drupal
$ mkdir -p site/all/module/onthisdate

โครงสร้างชื่อฟังก์ชั่นในมอดูลจะเป็น

function {modulename}_{hook}

เช่น onthisdate_help เพื่อข่วยเหลือ หรือ onthisdate_menu เพื่อแสดงเมนู เป็นต้น

Topic: 

02. บอกว่ามอดูลเราจะทำอะไร (Telling Drupal about your module)

ต้องสร้างไฟล์ module_name.info สำหรับบอก Drupal ตัวอย่างนี้คือ
$ vi onthisdate.info

; $Id$
name = On this date
description = A block module that lists links to content such as blog entries or forum discussions that were created one week ago.
core = 6.x

ต้องมีหัวข้อดังนี้

name
บอกชื่อมอดูล
description
บอกคนอื่นให้รู้ว่ามอดูลทำอะไร สั้น ๆ 1 บรรทัด ถ้ามีอักขระแปลก ๆ ต้องเขียนด้วย HTML entities เพื่อให้แสดงผลในเว็บได้ เช่น
description = This is my "crazy@email.com" email address instead of description = This is my "crazy@email.com" email address
core
บอกให้รู้รุ่น Drupal เพราะเขาใช้เป็นตัวแยกว่าอันไหนจะเปิดให้ใช้งานบ้าง เวลาอัปเกรด

อาจมีหัวข้อดังนี้

ดีเพนเดนซี (dependencies)
บอกให้รู้ว่ามอดูลเราต้องการมอดูลไหนเป็นฐานบ้าง
    dependencies[] = taxonomy
    dependencies[] = comment
แพกเกจ (package)
บอกให้รู้ว่ามอดูลเรามีเพื่อนร่วมมอดูลอะไรบ้าง
package = "ชื่อมอดูลที่ร่วม"

ลองฟังก์ชั่นแรกคือ help
$ vi onthisdate.module

<?php
/**
* Display help and module information
* @param path which path of the site we're displaying help
* @param arg array that holds the current path as would be returned from arg() function
* @return help text for the path
*/
function onthisdate_help($path, $arg) {
  $output = '';
  switch ($path) {
    case "admin/help#onthisdate":
      $output = '<p>'.  t("Displays links to nodes created on this date") .'</p>';
      break;
  }
  return $output;
} // function onthisdate_help
?>

ตัวแปร $path แทนพาธว่า help ของเราจะไปอยู่ตรงไหน

ดูเพิ่มที่

Topic: 

03. มอดูลมีข้ออนุญาตอะไรบ้าง (Telling Drupal who can use your module)

เขียนชื่อฟังก์ชั่นในรูป hook_perm
รูปแบบฟังก์ชั่นคือ

<?php
function newmodule_perm() {
  return array('access newmodule', 'create newmodule', 'administer newmodule');
} / function newmodule_perm
?>

ตามตัวอย่างนี้คือ

<?php
/**
* Valid permissions for this module
* @return array An array of valid permissions for the onthisdate module
*/

function onthisdate_perm() {
  return array('access onthisdate content', 'administer onthisdate');
} // function onthisdate_perm
?>

ดูเพิ่มที่

Topic: 

04. ทำเรื่องบล๊อก (Declare we have block content)

ถ้ามอดูลเราทำบล๊อกด้วย เราต้องเขียนฟังก์ชั่นในรูป hook_block

ดังนี้

<?php
/**
* Generate HTML for the onthisdate block
* @param op the operation from the URL
* @param delta offset
* @returns block HTML
*/
function onthisdate_block($op='list', $delta=0) {
  // listing of blocks, such as on the admin/block page
  if ($op == "list") {
     $block[0]["info"] = t('On This Date');
     return $block;
  }
} // end onthisdate_block
?>

ตัวแปร $op เรียกว่า operation บอกว่าข้อมูลบล๊อกของเราอยู่ในรูปไหน ในที่นี้เป็น list
ตัวแปร $delta เรียกว่า offset บอกว่าระหว่างการแสดงผลเป็นบล๊อก หรือแสดงผลในรูปอื่นมีข้อแตกต่างกันหรือเปล่า

ดูเพิ่ม

Topic: 

05. สร้างเนื้อให้บล๊อก (Generate the block content)

มอดูลนี้ เราจะสร้างรายการของเนื้อหา (nodes) ของวันนี้ในสัปดาห์ก่อน เวลาเราจะดึงรายการมา เราดูจากเวลาที่เนื้อหาถูกสร้าง โดยเราทำในรูปวินาที (ดู php เรื่องเวลา)

<?php
/**
* Generate HTML for the onthisdate block
* @param op the operation from the URL
* @param delta offset
* @returns block HTML
*/
function onthisdate_block($op='list', $delta=0) {

  // listing of blocks, such as on the admin/block page
  if ($op == "list") {
    $block[0]["info"] = t('On This Date');
    return $block;
  } else if ($op == 'view') {

    // our block content
    // Get today's date
    $today = getdate();

    // calculate midnight one week ago
    $start_time = mktime(0, 0, 0,
                         $today['mon'], ($today['mday'] - 7), $today['year']);

    // we want items that occur only on the day in question, so  
    // calculate 1 day
    $end_time = $start_time + 86400; 
    // 60 * 60 * 24 = 86400 seconds in a day
  }

  //We'll use db_query() to get the records (i.e. the database rows) with our SQL query
  $result =  db_query("SELECT nid, title, created FROM {node} WHERE created >= '%s' AND created <= '%s'", $start_time, $end_time);

  // content variable that will be returned for display   
  $block_content = ''; 
  while ($links = db_fetch_object($result)) {
    $block_content .=  l($links->title, 'node/'. $links->nid) .'<br />';
  }

  // check to see if there was any content before setting up
  //  the block 
  if ($block_content == '') {   
    /* No content from a week ago.  If we return nothing, the block  
     * doesn't show, which is what we want. */
    return;
  }

  // set up the block 
  $block['subject'] = 'On This Date'; 
  $block['content'] = $block_content;
  return $block;
}
?>
  • สร้าง query ใช้ฟังก์ชั่น db_query() โดยให้ชื่อตารางในฐานข้อมูลอยู่ในรูป {node} ดูรายละเอียดจาก Table Prefix (and sharing tables across instances)
  • เวลาดึงจริง ใช้ฟังก์ชั่น db_fetch_object()
  • พอดึงมาปุ๊ป ก็สร้างรายการเป็นลิงก์ ด้วยฟังก์ชั่น l() ให้อยู่ในรูปของ <li><a href="node/nid">title</li>
Topic: 

06. ติดตั้ง เปิดใช้ ตั้งค่า และทดสอบ (Installing, enabling and testing the module)

ติดตั้ง
เอาไดเรคทอรี onthisdate (มีไฟล์ onthisdate.info และ onthisdate.module) ไปไว้ที่ sites/all/modules หรือ sites/hostname/modules
เปิดใช้
ผ่านเมนูคือ Administer » Site building » Modules หรือพิมพ์ตรง ๆ ใน URL ว่า admin/build/modules แล้วกาถูก
ตั้งค่า
มอดูลเราเป็นบล๊อก จึงต้องเปิดใช้งานบล๊อกผ่านเมนู Administer » Site building » Blocks หรือ URL ว่า admin/build/block
ทดสอบ
ดูจากตรงบล๊อกที่เราเพิ่งเปิดใช้ ถ้าวันนี้ในสัปดาห์ก่อนไม่มีเนื้อหาอะไร เราก็จะไม่เห็นอะไรเลย แต่ถ้ามีเนื้อหามากหลายหัวข้อ จะเห็นบล๊อกนี้ยาวเหยียด (ซึ่งเราจะไปปรับแต่งต่อไป)
Topic: 

07. เพิ่มส่วนของการตั้งค่า (Create a module configuration (settings) page)

ทำให้ตั้งค่าได้ด้วยฟังก์ชั่น onthisdate_admin
ทำเป็นฟอร์มโดยบรรจุอาเรย์ในรูปของ array( '#name => 'value', ... )
<?php
function onthisdate_admin() {

  $form['onthisdate_maxdisp'] = array(
    '#type' => 'textfield',
    '#title' => t('Maximum number of links'),
    '#default_value' => variable_get('onthisdate_maxdisp', 3),
    '#size' => 2,
    '#maxlength' => 2,
    '#description' => t("The maximum number of links to display in the block."),
    '#required' => TRUE,
  );

  return system_settings_form($form);
}
?>
  • ใช้ฟังก์ชั่น t() ในการแสดงผลอักขระ
  • ใช้ฟังก์ชั่น variable_get('variable_name',default_value) ในการรับค่าตัวแปรจากระบบ ซึ่งในที่นี้เรากำหนดค่าปริยายให้ onthisdate_maxdisp เป็น 3
  • ต้องคืนค่าให้ระบบด้วยฟังก์ชั่น system_settings_form()

เอาส่วนของการตั้งค่านี้ คือตัวเลข onthisdate_maxdisp ไปใส่ในฟังก์ชั่น onthisdate_block() ดังนี้

//--- onthisdate_block function ---
...
  $limitnum = variable_get("onthisdate_maxdisp", 3);

  $query = "SELECT nid, title, created FROM " .
           "{node} WHERE created >= %d " .
           "AND created <= %d";

  $queryResult = db_query_range($query, $start_time, $end_time, 0, $limitnum);
...


ทำให้เรียกใช้งานตั้งค่า ผ่านเมนูได้ด้วยฮุก onthisdate_menu
<?php
function onthisdate_menu() {

  $items = array();

  $items['admin/settings/onthisdate'] = array(
    'title' => 'On this date module settings',
    'description' => 'Description of your On this date settings control',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('onthisdate_admin'),
    'access arguments' => array('access administration pages'),
    'type' => MENU_NORMAL_ITEM,
   );

  return $items;
}
?>
กรองความถูกต้องของการป้อนข้อมูลด้วยฟังก์ชั่น onthisdate_admin_validate
<?php
function onthisdate_admin_validate($form, &$form_state) {
  $maxdisp = $form_state['values']['onthisdate_maxdisp'];
  if (!is_numeric($maxdisp)) {
    form_set_error('onthisdate_maxdisp', t('You must select a number for the maximum number of links.'));
  }
  else if ($maxdisp <= 0) {
    form_set_error('onthisdate_maxdisp', t('Maximum number of links must be positive.'));
  }
}
?>

ดูเพิ่ม

Topic: 

08. เพิ่มส่วนแสดงเนื้อหาในหน้าหลัก (Generate a page content)

โค๊ดหลักมีแต่การแสดงเนื้อในบล๊อกซึ่งมีเนื้อที่จำกัด คราวนี้เรามาเพิ่มให้แสดงเนื้อในหน้าหลักได้ โดยเราสามารถแสดงได้ไม่จำกัดจำนวนหัวข้อ

ทำผ่านฟังก์ชั่น onthisdate_all()

<?php
function onthisdate_all() {
  // content variable that will be returned for display
  $page_content = '';

  // Get today's date
  $today = getdate();

  // calculate midnight one week ago
  $start_time = mktime(0, 0, 0, $today['mon'], ($today['mday'] - 7), $today['year']);

  // we want items that occur only on the day in question,
  // so calculate 1 day
  $end_time = $start_time + 86400;
  // 60 * 60 * 24 = 86400 seconds in a day

  $query = "SELECT nid, title, created FROM " .
           "{node} WHERE created >= '%d' " .
           " AND created <= '%d'";

  // get the links (no range limit here)
  $queryResult =  db_query($query, $start_time, $end_time);
  while ($links = db_fetch_object($queryResult)) {
    $page_content .= l($links->title, 'node/'.$links->nid).'<br />';
  }

  // check to see if there was any content before
  // setting up the block
  if ($page_content == '') {
    // no content from a week ago, let the user know
    $page_content = "No events occurred on this site on this date in history.";
  }
  return $page_content;
}
?>
  • ฟังก์ชั่น onthisdate_all ไม่ใช่ฮุก ถ้ามอดูลอื่นจะเรียกใช้ ต้องเรียกผ่านฟังก์ชั่นระบบ module_invoke()
  • แต่ถ้าเราจะให้ฟังก์ชั่นของเราดูได้เฉพาะภายใน ต้องนำหน้าชื่อฟังก์ชั่นด้วยขีดเส้นใต้ _
Topic: 

09. บอกให้ Drupal รู้ถึงการทำงานใหม่ (Letting Drupal know about the new function)

ถ้าฟังก์ชั่นไหนของเราไม่ใช่ฮุก เราต้องบอกให้ Drupal รับรู้ถึงฟังก์ชั่นเราเสมอ
ทำได้ผ่านฮุก onthisdate_menu() โดยกลับไปแก้ไขงานจากคราวก่อน

<?php
function onthisdate_menu() {

  $items = array();

  //this was created earlier in tutorial 7.
  $items['admin/settings/onthisdate'] = array(
    'title' => 'On this date module settings',
    'description' => 'Description of your On this date settings control',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('onthisdate_admin'),
    'access arguments' => array('access administration pages'),
    'type' => MENU_NORMAL_ITEM,
   );

  //this is added for this current tutorial.
  $items['onthisdate'] = array(
    'title' => 'On this date',
    'page callback' => 'onthisdate_all',
    'access arguments' => array('access onthisdate content'),
    'type' => MENU_CALLBACK
  );

  return $items;
}
?>
  • เมื่อเรียกผ่าน URL ว่า onthisdate ฟังก์ชั่น onthisdate_all() จะทำงาน
  • ชนิด (type) ของเมนู คือ
    • MENU_NORMAL_ITEM เป็นเมนูปกติที่ผู้ใช้มองเห็น
    • MENU_CALLBACK ไม่แสดงในเมนูจริง ๆ แต่จะถูกเรียกใช้ผ่านทาง URL เท่านั้น

    ดูเพิ่ม

    Topic: 

    10. เพิ่ม 'more' ให้บล๊อก (Adding a 'more' link and showing all entries)

    เพิ่มลิงก์ 'more' หรือ 'มีต่อ' ในการแสดงหัวข้อเพิ่มเติมจากที่ล้นเนื้อบล๊อก
    กลับไปแก้งานในส่วนของ onthisdate_block

    <?php
    // add a more link to our page that displays all the links
      $block_content .=
        "<div class=\"more-link\">".
        l(
          t("more"),
          "onthisdate",
          array(
            "title" => t("More events on this day.")
          )
        )."</div>";
    ?>

    ดูเพิ่ม

    Topic: 

    11. จับมารวมกัน

    ได้ดังนี้
    $ cd /var/www/drupal
    $ mkdir -p sites/all/module/onthisdate

    ไฟล์ info
    $ vi onthisdate.info

    ; $Id$
    name = On this date
    description = A block module that lists links to content such as blog entries or forum discussions that were created one week ago.
    core = 6.x

    ไฟล์ module
    $ vi onthisdate.module

    <?php
    /**
    * Display help and module information
    * @param path which path of the site we're displaying help
    * @param arg array that holds the current path as would be returned from arg() function
    * @return help text for the path
    */
    function onthisdate_help($path, $arg) {
      $output = '';
      switch ($path) {
        case "admin/help#onthisdate":
          $output = '<p>'.  t("Displays links to nodes created on this date") .'</p>';
          break;
      }
      return $output;
    } // function onthisdate_help
    
    /**
    * Valid permissions for this module
    * @return array An array of valid permissions for the onthisdate module
    */
    function onthisdate_perm() {
      return array('access onthisdate content', 'administer onthisdate');
    } // function onthisdate_perm
    
    /**
    * Generate HTML for the onthisdate block
    * @param op the operation from the URL
    * @param delta offset
    * @returns block HTML
    */
    function onthisdate_block($op='list', $delta=0) {
      // listing of blocks, such as on the admin/block page
      if ($op == "list") {
        $block[0]["info"] = t('On This Date');
        return $block;
      } else if ($op == 'view') {
    
        // our block content
        // Get today's date
        $today = getdate();
    
        // calculate midnight one week ago
        $start_time = mktime(0, 0, 0,
                             $today['mon'], ($today['mday'] - 7), $today['year']);
    
        // we want items that occur only on the day in question, so  
        // calculate 1 day
        $end_time = $start_time + 86400; 
        // 60 * 60 * 24 = 86400 seconds in a day
      }
    
      //We'll use db_query() to get the records (i.e. the database rows) with our SQL query
      $limitnum = variable_get("onthisdate_maxdisp", 3);
    
      $query = "SELECT nid, title, created FROM " .
               "{node} WHERE created >= %d " .
               "AND created <= %d";
    
      $queryResult = db_query_range($query, $start_time, $end_time, 0, $limitnum);
    
      // content variable that will be returned for display   
      $block_content = '<ul>';
      while ($links = db_fetch_object($queryResult)) {
        $block_content .=  '<li>'.l($links->title, 'node/'. $links->nid) .'</li>';
      }
    
      // check to see if there was any content before setting up
      //  the block
      if ($block_content == '<ul>') {
        /* No content from a week ago.  If we return nothing, the block
         * doesn't show, which is what we want. */
        return;
      }
    
      $block_content .= '</ul>';
    
      // add a more link to our page that displays all the links
      $block_content .=
        "<div class=\"more-link\">".
        l(
          t("more"),
          "onthisdate",
          array(
            "title" => t("More events on this day.")
          )
        )."</div>";
    
      // set up the block 
      $block['subject'] = 'On This Date'; 
      $block['content'] = $block_content;
      return $block;
    } // end onthisdate_block
    
    function onthisdate_admin() {
      $form['onthisdate_maxdisp'] = array(
        '#type' => 'textfield',
        '#title' => t('Maximum number of links'),
        '#default_value' => variable_get('onthisdate_maxdisp', 3),
        '#size' => 2,
        '#maxlength' => 2,
        '#description' => t("The maximum number of links to display in the block."),
        '#required' => TRUE,
      );
    
      return system_settings_form($form);
    }
    
    function onthisdate_menu() {
      $items = array();
    
      //this was created earlier in tutorial 7.
      $items['admin/settings/onthisdate'] = array(
        'title' => 'On this date module settings',
        'description' => 'Description of your On this date settings control',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('onthisdate_admin'),
        'access arguments' => array('access administration pages'),
        'type' => MENU_NORMAL_ITEM,
       );
    
      //this is added in tutorial 9.
      $items['onthisdate'] = array(
        'title' => 'On this date',
        'page callback' => 'onthisdate_all',
        'access arguments' => array('access onthisdate content'),
        'type' => MENU_CALLBACK,
      );
    
      return $items;
    }
    
    function onthisdate_admin_validate($form, &$form_state) {
      $maxdisp = $form_state['values']['onthisdate_maxdisp'];
      if (!is_numeric($maxdisp)) {
        form_set_error('onthisdate_maxdisp', t('You must select a number for the maximum number of links.'));
      }
      else if ($maxdisp <= 0) {
        form_set_error('onthisdate_maxdisp', t('Maximum number of links must be positive.'));
      }
    }
    
    function onthisdate_all() {
      // content variable that will be returned for display
      $page_content = '';
    
      // Get today's date
      $today = getdate();
    
      // calculate midnight one week ago
      $start_time = mktime(0, 0, 0, $today['mon'], ($today['mday'] - 7), $today['year']);
    
      // we want items that occur only on the day in question,
      // so calculate 1 day
      $end_time = $start_time + 86400;
      // 60 * 60 * 24 = 86400 seconds in a day
    
      $query = "SELECT nid, title, created FROM " .
               "{node} WHERE created >= '%d' " .
               " AND created <= '%d'";
    
      // get the links (no range limit here)
      $queryResult =  db_query($query, $start_time, $end_time);
      while ($links = db_fetch_object($queryResult)) {
        $page_content .= l($links->title, 'node/'.$links->nid).'<br />';
      }
    
      // check to see if there was any content before
      // setting up the block
      if ($page_content == '') {
        // no content from a week ago, let the user know
        $page_content = "No events occurred on this site on this date in history.";
      }
      return $page_content;
    }
    ?>
    

    ติดตั้ง
    ติดอยู่แล้ว

    เปิดใช้

    • URL admin/build/module กาถูกหน้า onthisdate
    • URL admin/user/permissions เลือกกาถูกข้ออนุญาตที่เกี่ยวข้องกับ onthisdate

    ตั้งค่า

    • URL admin/settings/onthisdate ตั้งจำนวนหัวข้อ
    • ทำเป็นบล๊อกผ่าน URL admin/build/block เลือกกาถูก On this date

    ทดสอบ
    ดูที่บล๊อก On this date ตามต้องการ

    Topic: 

    drupal6: ตัวอย่างการสร้าง node, page

    มีตัวอย่างการสร้าง node และ page แอบอยู่ที่ api.drupal.org คือ

    สามารถดูซอร์ส แล้วเอามาทดลองสร้างมอดูลเองได้

    Topic: 

    drupal6: แปลงมอดูล onthisdate เป็น recentweek

    จะแปลงมอดูลจากตัวอย่าง คือ On This Date ซึ่งดู "วันนี้ในอาทิตย์ก่อน" มาเป็น Recent Week คือดูหัวข้อใหม่ในสัปดาห์นี้ (เหมือนกับ tracker แต่ทำเป็นบล๊อกได้)

    เริ่มเลย
    $ cd /var/www/drupal/sites/all/modules
    $ cp -xa onthisdate recentweek
    $ cd recentweek
    $ mv onthisdate.info recentweek.info
    $ mv onthisdate.module recentweek.module
    $ sed -i "s/onthisdate/recentweek/g" *
    $ sed -i "s/on this date/recent week/g" *
    $ sed -i "s/On this date/Recent week/g" *
    $ sed -i "s/On This Date/Recent Week/g" *
    $ vi recentweek.module

    <?php
    /**
    * Display help and module information
    * @param path which path of the site we're displaying help
    * @param arg array that holds the current path as would be returned from arg() function
    * @return help text for the path
    */
    function recentweek_help($path, $arg) {
      $output = '';
      switch ($path) {
        case "admin/help#recentweek":
          $output = '<p>'.  t("Displays links to nodes created recent week") .'</p>';
          break;
      }
      return $output;
    } // function recentweek_help
    
    /**
    * Valid permissions for this module
    * @return array An array of valid permissions for the recentweek module
    */
    function recentweek_perm() {
      return array('access recentweek content', 'administer recentweek');
    } // function recentweek_perm
    
    /**
    * Generate HTML for the recentweek block
    * @param op the operation from the URL
    * @param delta offset
    * @returns block HTML
    */
    function recentweek_block($op='list', $delta=0) {
      // listing of blocks, such as on the admin/block page
      if ($op == "list") {
        $block[0]["info"] = t('Recent Week');
        return $block;
      } else if ($op == 'view') {
    
        // our block content
        // Get today's date
        $today = getdate();
    
        // calculate midnight one week ago
        $start_time = mktime(0, 0, 0,
                             $today['mon'], ($today['mday'] - 7), $today['year']);
    
    /* wd's mod
        // we want items that occur only on the day in question, so
        // calculate 1 day
        $end_time = $start_time + 86400;
        // 60 * 60 * 24 = 86400 seconds in a day
    */
        #wd#// delete parameter $end_time, $query changed
      }
    
      //We'll use db_query() to get the records (i.e. the database rows) with our SQL query
      $limitnum = variable_get("recentweek_maxdisp", 3);
    
    /* wd's mod
      $query = "SELECT nid, title, created FROM " .
               "{node} WHERE created >= %d " .
               "AND created <= %d";
      $queryResult = db_query_range($query, $start_time, $end_time, $limitnum);
    */
      #wd#// delete parameter $end_time, $query changed
      $query = "SELECT nid, title, created FROM " .
               "{node} WHERE created >= %d ORDER BY created DESC" ;
      $queryResult = db_query_range($query, $start_time, 0, $limitnum);
      
      // content variable that will be returned for display
      $block_content = '<ul>';
      while ($links = db_fetch_object($queryResult)) {
        $block_content .=  '<li>'.l($links->title, 'node/'. $links->nid) .'</li>';
      } 
    
      // check to see if there was any content before setting up
      //  the block
      if ($block_content == '<ul>') {
        /* No content from a week ago.  If we return nothing, the block
         * doesn't show, which is what we want. */
        return;
      } 
        
      $block_content .= '</ul>';
        
      // add a more link to our page that displays all the links
      $block_content .=
        "<div class=\"more-link\">".
        l(
          t("more"),
          "recentweek",
          array(
            "title" => t("More events on this day.")
          )
        )."</div>";
      
      // set up the block
      $block['subject'] = 'Recent Week'; 
      $block['content'] = $block_content;
      return $block;
    } // end recentweek_block
    
    function recentweek_admin() {
      $form['recentweek_maxdisp'] = array(
        '#type' => 'textfield',
        '#title' => t('Maximum number of links'),
        '#default_value' => variable_get('recentweek_maxdisp', 3),
        '#size' => 2,
        '#maxlength' => 2,
        '#description' => t("The maximum number of links to display in the block."),
        '#required' => TRUE,
      );
    
      return system_settings_form($form);
    }
    
    function recentweek_menu() {
      $items = array();
    
      //this was created earlier in tutorial 7.
      $items['admin/settings/recentweek'] = array(
        'title' => 'Recent week module settings',
        'description' => 'Description of your Recent week settings control',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('recentweek_admin'),
        'access arguments' => array('access administration pages'),
        'type' => MENU_NORMAL_ITEM,
       );
    
      //this is added in tutorial 9.
      $items['recentweek'] = array(
        'title' => 'Recent week',
        'page callback' => 'recentweek_all',
        'access arguments' => array('access recentweek content'),
        'type' => MENU_CALLBACK,
      );
      
      return $items;
    }   
        
    function recentweek_admin_validate($form, &$form_state) {
      $maxdisp = $form_state['values']['recentweek_maxdisp'];
      if (!is_numeric($maxdisp)) {
        form_set_error('recentweek_maxdisp', t('You must select a number for the maximum number of links.'));
      }
      else if ($maxdisp <= 0) {
        form_set_error('recentweek_maxdisp', t('Maximum number of links must be positive.'));
      }
    } 
    
    function recentweek_all() {
      // content variable that will be returned for display
      $page_content = '';
        
      // Get today's date
      $today = getdate();
        
      // calculate midnight one week ago
      $start_time = mktime(0, 0, 0, $today['mon'], ($today['mday'] - 7), $today['year']);
      
    /* wd's
      // we want items that occur only on the day in question,
      // so calculate 1 day
      $end_time = $start_time + 86400;
      // 60 * 60 * 24 = 86400 seconds in a day
    
      #wd#// delete parameter $end_time, $query changed
      $query = "SELECT nid, title, created FROM " .
               "{node} WHERE created >= '%d' " .
               " AND created <= '%d'";
      $queryResult =  db_query($query, $start_time, $end_time);
    */
    
      $query = "SELECT nid, title, created FROM " .
               "{node} WHERE created >= '%d' ORDER BY created DESC";
    
      // get the links (no range limit here)
      $queryResult =  db_query($query, $start_time);
      $page_content = '<ul>';
       while ($links = db_fetch_object($queryResult)) {
        $page_content .= '<li>'.l($links->title, 'node/'.$links->nid).'</li>';
      }
    
      // check to see if there was any content before
      // setting up the block
      if ($page_content == '<ul>') {
        // no content from a week ago, let the user know
        $page_content = "No events occurred on this site recent week in history.";
      } else $page_content .= '</ul>';
      return $page_content;
    }
    ?>
    

    เสร็จแล้ว

    เพื่อความสุขสวัสดี อย่าลืมรัน update.php ด้วย ไม่งั้นอาจมีปัญหาเรื่อง HTTP request failed

    Topic: