01
<?php function mytracer_menu() { $items = array(); $items['admin/config/mytracer'] = array( 'title' => 'My Tracer', 'description' => 'MY tracer des', 'access callback' => true, 'page callback' => 'mytracer_admin', ); return $items; } //全局变量的使用 function mytracer_admin() { global $base_path; global $base_url; global $user; $output = ''; $output .='<p>'. $base_path . '</p>'; $output .='<p>'. $base_url . '</p>'; dpm($user); return $output; }
02 ,永久变量的使用
function mytracer_admin() { //定义一个mytracer_setting1 为10 $var1 = variable_get('mytracer_setting1', 10); $output = ''; $output .= '变量'. $var1; //把mytracer_setting1值加1 variable_set('mytracer_setting1', $var1+1); //删除mytracer_setting1变量 //variable_del('mytracer_setting1'); return $mytracer_setting1; }
03 arg()函数的使用
function mytracer_admin() { // 使用 arg() 函數实例,返回一个数组 $args = arg(); $output = ''; $output .= '<p>Current url\'s arguments -> ' . implode(', ', $args); $output .= '<p> arg(1) = ' . arg(1); $output .= '<p> arg(1) in http://www.google.com/a/b/c = ' . arg(1, 'http://www.google.com/a/b/c'); $output .= '<p> arg(1) in admin/config/mytracer = ' . arg(1, 'admin/config/mytracer'); return $output; }
04,drupal_set_messag()实例
function mytracer_admin() { global $user; $s = format_string('User: "@username" has entered this page', array('@username' => $user->name)); drupal_set_message($s); drupal_set_message($s, 'status', false); return ''; }
05,t函数实例
function mytracer_admin() { // 使用 t 函數示範 $txt = '<p style="color:red;">A String</p>'; $output = ''; // 以下這二個都叫做 title 的可翻譯字串,因為大小寫不同,其實是二個要翻譯的字串 $output .= '<p>' . t('Title'); $output .= '<p>' . t('title'); //!号直接正常显示 $output .= '<p>' . t('use ! as a placeholder: !str', array('!str' => $txt)); //@ % 过滤html显示 % 斜线重点 $output .= '<p>' . t('use @ as a placeholder: @str', array('@str' => $txt)); $output .= '<p>' . t('use % as a placeholder: %str', array('%str' => $txt)); return $output; }
06,item_list()函数实例
/* mytraceer 模組的管理介面網頁 * */ function mytracer_admin() { // 使用 theme() 函數,並使用 item_list 版型化 global $user; $output = ''; $output .= theme('username', array('account' => $user)); /* * 1. 這是簡單的寫法 */ $title = 'An Item List'; $items = array(); $items[] = 'this is line 1'; $items[] = 'this is line 2'; $items[] = 'this is line 3'; // 使用 theme_item_list 時,需要三個資料 (items, title, type, attributes),利用 array 打包起來 $output .= '<p>' . theme('item_list', array('items' => $items, 'title' => $title, 'type' => 'ol')); /* * 2. 比較複雜的例子 * 第二列:增加了 class 與 id * 第三列:增加了下一層的 list */ $items = array(); $items[] = 'this is a line'; $items[] = array('this is a line', 'class' => 'a-item', 'id' => 'line-2'); $items[] = array('this is a line', 'children' => array('c1', 'c2', 'c3')); // 使用 theme_item_list 時,需要三個資料 (items, title, type, attributes),利用 array 打包起來 $output .= '<p>' . theme('item_list', array('items' => $items, 'title' => $title, 'type' => 'ul')); /* * 3. 常用的方法 * 準備一個 $items 的 array, 並將 list 的各種元素打包起來 */ // 變數 $items 我們準備提供給 theme('item_link, ...) 進行版型化的輸出 // 在這裡並未指定它的 type, 因此使用預設的 ul (unordered list) $items = array( 'items' => array( l('Manage User Accounts', 'admin/people'), l('Manage Modules', 'admin/modules'), l('Manage MyTracer', 'admin/config/mytracer'), ), 'title' => 'Useful Management Links', 'attributes' => array('class' => 'management'), ); // 這個 item_list 的例子,我們增加了 attributes 資料, 這個 attributes 是套用在 ul 或 ol 上面 $output .= '<p>' . theme('item_list', $items); return $output; }
07。使用 theme() 函數,並使用 table 的版型化
/* mytraceer 模組的管理介面網頁 * */ function mytracer_admin() { // 使用 theme() 函數,並使用 table 的版型化 $output = ''; /* * 1. 最簡單的 table 作法 */ // table 要有 header, 如果某個欄位允許排序的話,要提供 'field' 屬性 $header = array('col 1', 'col 2', 'col 3', 'col 4'); // table 要有 row, 習慣上, 使用 $rows 變數, cell 中如果有其它屬性的話, 該 cell 以 array 方式來定義, // 並將相關屬性打包進來 $rows = array( array( 'Cell 11', 'Cell 12', 'Cell 13', 'Cell 14' ), // Row with attributes on the row and some of its cells. array( 'Cell 21', 'Cell 22', 'Cell 23', 'Cell 24' ) ); $items = array( 'header' => $header, 'rows' => $rows, ); $output .= '<p>' . theme('table', $items); /* * 2. 帶有屬性旳 table 作法 */ // table 要有 header, 如果某個欄位允許排序的話,要提供 'field' 屬性 $header = array('header 1', 'header 2', 'header 3', 'header 4'); // table 要有 row, 習慣上, 使用 $rows 變數, cell 中如果有其它屬性的話, 該 cell 以 array 方式來定義, // 並將相關屬性打包進來 $rows = array( // Simple row array( 'Cell 1', 'Cell 2', 'Cell 3', array('data' => 'Cell 4', 'class' => 'highlight') ), // Row with attributes on the row and some of its cells. array( 'data' => array('Cell 1', 'Cell 2', array('data' => 'Cell 3,4', 'colspan' => 2)), 'class' => array('funky') ) ); $output .= '<p><p>' . theme('table', array('header' => $header, 'rows' => $rows)); /* * 3. 常用的作法 */ $output .= '<p>' . '<h3>General Table Usage</h3>'; $header = array('標題', '網址', '操作'); // 通常, rows 會由資料庫讀取資料後, 一列一列排出來, 還沒有介紹讀取資料庫之前, // 我們先使用簡單的一一填入方法 $rows = array(); for ($i=1; $i<4; $i++) { $node = node_load($i); // node_load(): 載入 id 為 $i 的內容節點 $row = array(); $row[] = $node->title; $row[] = url('node/' . $i); $row[] = l('編輯', 'node/' . $i . '/edit'); $rows[] = $row; } $output .= theme('table', array('header'=>$header, 'rows'=>$rows)); return $output; }