Creating a simple jump menu module from taxonomy terms using the Form API

If you come from a background where you have created your own forms directly in HTML, you may find Drupal’s approach a bit confusing at first. Drupal presents an application programming interface (API) for generating, validating, and processing HTML forms. The form API abstracts forms into a nested array of keys (referred to as properties) and values. Rather than output HTML, we create an array and let the engine generate the HTML.

Scenario

I had to work on a project with a small section having country pages for say 10+ countries. Each country page will list nodes from these content types news, reports, announcement, and projects. One requirement was that, upon creation of a node of any of these content types, the country field should automatically be assigned a value (a country name) based on the user's country. In the end, the site should have a jump menu to the country pages that will lists all post from these node types (news, reports, announcement, projects) on one page dynamically.

Solution

I created a vocabulary called country with the country names as terms. I then created a term reference field and assigned to the node types (news, reports, announcement, and projects) and also a term reference field for the user entity . I created a view that takes an argument from the taxonomy term to list all nodes for the node types. Panels and views were used to build the dynamic country pages which takes taxonomy terms as argument. Now, the client wants a jump menu in a block were users can select a country and be directed to a country page that lists all post from these node types (news, reports, announcement, projects) for the selected country on one page. A module was written for the jump menu.

Module development code

The country jump menu module

* Implements hook_block_info().
*/
function country_jump_menu_block_info(){
	$blocks['country_menu'] = array(
    'info' => t('Country Jump menu'),
  );

  return $blocks;
}
 
/**
* Implements hook_block_view().
*/
function country_jump_menu_block_view($delta = '') {
  $block = array();

 
  if ($delta == 'country_menu') {
	$block['subject'] = t('');
	$block['content'] = drupal_get_form('select_form_country');
	return $block;

    
    }
    }

/**
* A function to fetch all the taxonomy terms.
*/
function get_countries(){
$countries = array();
	
		$terms = taxonomy_get_tree( vocabulary_ID_goes_here);
		foreach ($terms as $term) {
		$countries[$term->tid] = $term->name;
		}
		return asort ($countries);
}

function select_form_country (){
  
$form['choose_country'] = array(
	'#title' => t('Choose a country'),
	'#type' => 'select',
	'#description' => '',
	'#empty_option' =>t('- Select a country -'),
	'#options' => get_countries(),
	'#default_value' => variable_get('choose_country', array()),
	'#attributes' => array('onchange' => "form.submit('select_form_country')"),
	);

$form['submit'] = array(
	'#type' => 'submit',
	'#value' => 'Go',
	'#attributes' => array('style' => 'display:none;'),
	);	
	return $form;
}

function select_form_country_submit ($form_id, $form_state) {
	$name = $form_state['values']['choose_country'];
    $url = 'country-page/'.$name;
	drupal_goto($url);
}	

Assign a country to node form

This code snippet automatically assigns a country to the country field of a content type upon node creation.

function country_jump_menu_form_alter(&$form, &$form_state, $form_id){

global $user;
$user = user_load($user->uid);
	if ($form_id =='form_id_here'') {
		$term_id = $user->field_country_new['und'][0]['tid'];
		$term = taxonomy_term_load($term_id);
		$country_name = $term->name;

		//assign the value to the country field ;
		$form['field_select_country_page']['und']['#default_value'] = $country_name;
	
		}
}
Share