How to add a vocabulary (taxonomy) to variable via administration settings pages

This is how you can add a vocabulary (taxonomy) to variable via administration settings pages.

  • First you need a menu hook admin/settings/[whatever]
  • Then a function to do the presentation of forms
  • A function to create the forms
  • A submit function storing your values in the varaible tabel

<?php
/**
 * Menu hook
 *
 * @return unknown
 */
function forumblock_menu() {
 
$items = array();

 
$items['admin/settings/forumblock'] = array(
   
'title' => 'Forum block',
   
'description' => 'Edit Forum block settings',
   
'page callback' => 'forumblock_admin',
   
'access arguments' => array('access administration pages'),
   
'type' => MENU_NORMAL_ITEM,
  );

  return
$items;
}


/**
 * Forum block administration view
 *
 */
function forumblock_admin(){

 
$output .= "<p>Edit settings for Forum block";
 
$output .= drupal_get_form('forumblock_admin_form');
  return
$output;
}

/**
 * Forum block link admin form
 *
 */
function forumblock_admin_form(){

 
$voc = taxonomy_get_vocabularies();

  foreach (
$voc as $key => $vocab) {
     
$options[$vocab->vid] = $vocab->name;
  }

   
$form['forumblock']['user_taxonomy'] = array(
       
'#type' => 'select',
       
'#title' => t('User Taxonomy'),
       
'#weight' => 1,
       
'#default_value' => variable_get('forumblock_user_taxonomy', 0),
       
'#options' => $options,
       
'#description' => t('Select taxonomy used on user in forum'),
    );

   
/*****************************************************/

   
$form['forumblock']['submit'] = array(
       
'#type' => 'submit',
       
'#value' => 'Submit',
       
'#weight' => 20,
    );


    return
$form;
}

/**
*   Save submited forms
*
**/
function forumblock_admin_form_submit($form, &$form_state) {

   
variable_set("forumblock_user_taxonomy", trim($form_state['values']['user_taxonomy']));

   
drupal_set_message("Forum block setting has been saved.");

}
?>
Knowledge keywords: