background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount
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:

Eric.London's picture

The following will show you how to add a terms and conditions form element and admin settings page to display a required terms and conditions form element on the user registration form. First, I added an admin settings page.

<?php
function MYMODULE_admin_settings_form() {
 
$form = array();
 
// ...code...
 
$form['MYMODULE_terms_conditions'] = array(
   
'#type'             => 'textarea',
   
'#title'            => t('Terms and Conditions'),
   
'#default_value'    => variable_get('MYMODULE_terms_conditions', ''),
   
'#required'         => FALSE,
  );
 
// ...code...
 
return system_settings_form($form);
}
?>

Next, I modified the user registration form.

<?php
function MYMODULE_form_alter($form_id, &$form) {
 
// ...code...
 
if ($form_id == 'user_register') {
   
$terms = variable_get('MYMODULE_terms_conditions', '');
    if (
strlen($terms)) {
     
// add terms and conditions
     
$form['terms'] = array(
       
'#type'    => 'checkbox',
       
'#required' => TRUE,
       
'#weight' => 9,
       
'#title' => "I agree to the <a id='termsAndConditionsLink'>terms and conditions</a><div style='display:none;' id='termsAndConditionsText'>$terms</div>",
      );
    }
  }
 
// ...code...
}
?>

Lastly, I added some jQuery to my module's javascript include file.

<?php
$(document).ready(function(){
  $(
'a#termsAndConditionsLink').click(function() {
    if ($(
'div#termsAndConditionsText').css('display') == 'none') {
      $(
'div#termsAndConditionsText').slideDown();
    } else {
      $(
'div#termsAndConditionsText').slideUp();
    }
  });
});
?>

Syndicate content