How to implement System Settings (admin/settings/your_settings)
Submitted by valerio on Fri, 07/02/2010 - 09:36
If your module needs settings such as external logins, paths and so on you can use the Drupal system_settings_form() APIs.
First you need a menu hook.
function your_module_menu() {
$items = array();
$items['admin/settings/your_module_your_settings'] = array(
'title' => 'Your settings',
'description' => 'Your settings description',
'page callback' => 'drupal_get_form',
'page arguments' => array('your_module_admin_your_settings'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
Then you need to implement the function that builds the form for editing the settings. This is the page argument value.
function your_module_admin_your_settings(){
$form = array();
$form['your_module_loginname'] = array(
'#type' => 'textfield',
'#title' => t('Login name'),
'#default_value' => '',
'#description' => t("The login name"),
'#required' => TRUE,
);
$form['your_module_host'] = array(
'#type' => 'textfield',
'#title' => t('Host'),
'#default_value' => variable_get('your_module_host',''),
'#description' => t("The Host to connect to."),
'#required' => TRUE,
);
return system_settings_form($form);
}
Note that the values will be saved using the field name so it must be unique system-wide -- prefix with your module name to avoid conflicts. Leave the default value blank if you don't want to show the existing value to the user.
Anywhere in your code you can retrieve the value using the function variable_get()
If you want to have a hierarchical menu (ex. in the admin menu) then use this construct.
function your_module_menu() {
$items = array();
$items['admin/settings/your_module'] = array(
'title' => 'Your Module',
'description' => 'Your settings description',
'page callback' => 'drupal_get_form',
'page arguments' => array('your_module_admin_your_settings'),
'access arguments' => array('access administration pages'),
);
$items['admin/settings/your_module/your_settings'] = array(
'title' => 'Your settings',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);
$items['admin/settings/your_module/your_other_settings'] = array(
'title' => 'Your other settings',
'description' => 'Your other settings description',
'page callback' => 'drupal_get_form',
'page arguments' => array('your_module_admin_your_other_settings'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
- Login to post comments