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:

This code snippet will group your search results in collapsible fieldsets by node type.

<?php
function MYTHEME_search_page($results, $type) {
 
$html = '<dl class="search-results">';

 
// get a list of node types
 
$nodeTypes = node_get_types();
   
 
// loop through results, group by type
 
$resultTypes = array();
  foreach (
$results as $result) {
   
$resultTypes[$result['node']->type][] = $result;   
  }
   
 
// create fieldsets for each type
 
foreach ($resultTypes as $resultType => $resultTypeResults) {
   
$value = "";
   
// loop through entries
   
foreach ($resultTypeResults as $entry) {
     
$value .= theme('search_item', $entry, $type);
    }
       
   
// add fieldset
   
$html .= theme('fieldset',
      array(
       
'#title' => $nodeTypes[$resultType]->name,
       
'#collapsible' => TRUE,
       
'#collapsed' => FALSE,
       
'#value' => $value,
      )
    );
       
  }
   
 
$html .= '</dl>';
 
$html .= theme('pager', NULL, 10, 0);
   
  return
$html;
}
?>

Syndicate content