知識社群登入
Creating a module configuration (settings) page
by 蘇德宙, 2011-05-08 23:30, 人氣(1599)
 
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)) {...}
}