background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount

Create an admin settings page for your module

Eric.London's picture

Here's a code snippet to show you how to create an admin settings page for your module. In this example, I'll create a form that allows the user to enable/disable checkboxes for each node type. This might be useful if you wanted to modify the functionality of a node and allow the user to choose which nodes.

<?php
function MYMODULE_menu() {
 
$items = array();
 
// ...code...
 
$items['admin/settings/MYMODULE'] = array(
   
'title' => t('MY TITLE'),
   
'description' => t('MY DESCRIPTION'),
   
'page callback' => 'drupal_get_form',
   
'page arguments' => array('MYMODULE_admin_settings'),
   
'access arguments' => array('administer site configuration'),
  );
 
// ...code...
 
return $items;
}

function
MYMODULE_admin_settings() {
 
$form = array();

 
// get a list of node types
 
$nodeTypes = node_get_types();
   
 
// loop through node types and create an array of options
 
$options = array();
  foreach (
$nodeTypes as $k => $v) {
   
$options[$k] = $v->name;   
  }
   
 
// add form input element
 
$form['MYMODULE_enabled_node_types'] = array(
   
'#type' => 'checkboxes',
   
'#title' => t('Enabled node types'),
   
'#options' => $options,
   
'#default_value' => variable_get('MYMODULE_enabled_node_types', array()),
  );
   
  return
system_settings_form($form);
}
?>

Screenshot: