知識社群登入
位置: Web Programming > 文件區 > drupal > Module Dev.
by 蘇德宙, 2011-05-08 19:53, 人氣(1184)
 
1. name:   onthisdate
2. folder: sites/all/modules/onthisdate (the preferred place for non-core modules)
3. file:   onthisdate.module
by 蘇德宙, 2011-05-08 19:57, 人氣(1183)
 
name = On this date
description = A block module that lists links to content created one week ago.
core = 6.x
dependencies, package (optional)
by 蘇德宙, 2011-05-08 22:27, 人氣(1289)
 
Allow modules to interact with the Drupal core.
Drupal's module system is based on the concept of "hooks". A hook is named moduleName_hookName
by 蘇德宙, 2011-05-08 20:10, 人氣(1536)
 
function onthisdate_help($path, $arg) {
 
$output = ''//declare your output variable
 
switch ($path) {
    case
"admin/help#onthisdate":
     
$output = '<p>'t("Displays links to nodes created on this date") .'</p>';
      break;
  }
  return
$output;
}
by 蘇德宙, 2011-05-08 22:18, 人氣(1312)
 
define module permissions (Administer / User management / Permissions page)
function onthisdate_perm() {
  return array(
'access onthisdate content');
}
by 蘇德宙, 2011-05-08 22:38, 人氣(1204)
 
function onthisdate_block($op = 'list', $delta = 0, $edit = array()) { 
  if (
$op == "list") { // admin/block
   
$block = array();
   
$block[0]["info"] = t('On This Date');
    return
$block;
  }
}
by 蘇德宙, 2011-05-08 22:49, 人氣(1338)
 
function onthisdate_block($op = "list", $delta = 0) {
  if ($op == "view") {
    $block["subject"] = t("Subject of On This Date");
    $block["subject"] = t("Content of OnThisDate");
    return $block;                  
}
by 蘇德宙, 2011-05-08 23:30, 人氣(1365)
 
1. Create the configuration function
 
function onthisdate_admin() {
 
$form
= array();

 
$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
);
}
 
 
2. define a URL by hook_menu
 
function onthisdate_menu() {

 
$items
= array();

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

  return
$items
;
}
** clear the menu cache to recognize the new URL (Administer >> Site Configuration >> Performance)
 
 
3. Validate the user input (a "_validate" suffix)
 
function onthisdate_admin_validate($form, &$form_state) {
 
$maxdisp = $form_state['values']['onthisdate_maxdisp'];
  if (!
is_numeric($maxdisp)) {...}
}