ขอตัวอย่างโค๊ด helloworld ที่มี helloworld_block หน่อยคับ(เพิ่งเริ่มศึกษาคับ)
June 18, 2009
ขอตัวอย่างโค๊ด helloworld ที่มี helloworld_block หน่อยคับ(เพิ่งเริ่มศึกษาคับ)
ขอตัวอย่างโค๊ด helloworld ที่มี helloworld_block หน่อยคับ(เพิ่งเริ่มศึกษาคับ)
เอา code ที่ไม่มี block
เอา code ที่ไม่มี block ก่อนละกัน
save เป็นไฟล์ชื่อ hello.php เอาไว้ใน / ของ drupal ใช้งานโดย http://yoursite/hello.php
<?php // Bootstrap Drupal require 'includes/bootstrap.inc'; drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); drupal_maintenance_theme(); drupal_set_title(t('Hello, World!')); print theme('maintenance_page', t('Hello World again.')); ?>ขอ helloworld ที่เป็น module
ขอ helloworld ที่เป็น module หนะคับ
http://api.drupal.org/api/fil
http://api.drupal.org/api/file/developer/examples/block_example.module/6
มือใหม่อย่าเพิ่งไปสนใจ case
มือใหม่อย่าเพิ่งไปสนใจ case 'configure': กับ case 'save': ครับ ^ ^
module จะประกอบด้วยไฟล์หลัก
module จะประกอบด้วยไฟล์หลัก อย่างน้อย 3 ไฟล์ - .info - .module - .install
ตัวอย่าง module: helloworld จะพิมพ์ข้อความ Hello World ทุกครั้งที่ load page ของเว็บ โดยใช้ function drupal_set_message ผ่านทาง hook_init() ส่วน block จะส่งผ่านทาง hook_block()
ให้ลองหาเอกสารเกี่ยวกับ hook ต่าง ๆ มาศึกษา จะทำให้เข้าใจมากขึ้น
file helloworld.info
file helloworld.install
<?php /** * Implements hook_install(). */ function helloworld_install() { // Do nothing.. } ?>file helloworld.module
<?php /** * Implementation of hook_init(). */ function helloworld_init() { drupal_set_message(t('@@ Hello World!!!! @@')); } /** * Implementation of hook_block(). */ function helloworld_block($op = 'list', $delta = 0, $edit = array()) { switch ($op) { case 'list': $blocks[0]['info'] = t('Hello World'); return $blocks; case 'view': default: switch ($delta) { case 0: $block['subject'] = t('Hello World block title'); $block['content'] = t('Hello world block content'); break; } return $block; } } ?>ชัดเจนครับ ^
ชัดเจนครับ ^ ^
ขอเสริมนิดนึงว่า $delta เอาไว้กรณีที่โมดูลมีหลายๆบล็อค เป็นตัวบอกว่าบล็อคไหนกันแน่ครับ (ตั้งได้ตามใจ)