Skip to content

Instantly share code, notes, and snippets.

@alex-moreno
Created June 25, 2013 08:27
Show Gist options
  • Save alex-moreno/5856893 to your computer and use it in GitHub Desktop.
Save alex-moreno/5856893 to your computer and use it in GitHub Desktop.
Creating dropdown dependant forms
; $Id$
name = Custom Form dependant dropdown
description = Dropdown dependant form
project = Dependant dropdown
core = 6.x
version = 6.x-1.0
<?php
/**
* Reference: http://xebee.xebia.in/2011/06/14/drupal-webform-add-a-dynamic-select-option-list/
* The following piece of code is based on the blog post above written by Anubhav
*/
/**
* Implement hook_menu().
*
* Note that each item has both an entry point to prepare the form, and also
* a callback function that provides and AHAH menu callback.
*/
function custom_form_menu() {
$items = array();
// Automatically generate textfields.
$items['form/aha-form-test'] = array(
'title' => 'Degrading dependent dropdown',
'page callback' => 'drupal_get_form',
'page arguments' => array('custom_form_dropdown'),
'access callback' => TRUE,
'weight' => 4,
'type' => MENU_LOCAL_TASK,
// 'file' => 'ahah_example_dependent_dropdown.inc',
);
$items['form/aha-form-test/callback'] = array(
'page callback' => 'custom_form_dropdown_callback',
'access callback' => TRUE,
// 'file' => 'ahah_example_dependent_dropdown.inc',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Form builder function to create a form showing dependent dropdowns. The
* second dropdown has its values populated based on the first.
* @param $form_state
* @param $my_values
*/
function custom_form_dropdown(&$form_state, $my_values = array()) {
$form = array();
// get the list of options to populate the first dropdown
$initial_options = _custom_form_get_first_dropdown_options();
// get the list of options to populate the first dropdown
// $initial_options = _ahah_example_get_first_dropdown_options();
$form['explanation'] = array(
'#type' => 'markup',
'#value' => '<div>' . t('This is an example of a properly degrading dynamic form. It will work correctly with or without
AJAX enabled. However, it has to provide an extra hidden button to change the values in the dependent dropdown.') . '</div>',
);
// if we have a value for the first dropdown from $form_state['values'] we use
// this both as the default value for the first dropdown and also as a
// parameter to pass to the function that retrieves the options for the
// second dropdown.
$master_selection = !empty($form_state['values']['master_dropdown']) ? $form_state['values']['master_dropdown'] : t('ASDA');
// Here is where the magic happens.
$form['master_dropdown'] = array(
'#type' => 'select',
'#title' => 'Master Dropdown',
'#options' => $initial_options,
'#default_value' => $master_selection,
'#ahah' => array(
// 'path' => 'examples/ahah_example/dependent_dropdown/callback',
'path' => 'form/aha-form-test/callback',
'wrapper' => 'dependent-dropdown-wrapper',
// 'event' => 'change', // default value: does not need to be set explicitly.
),
'#attributes' => array('class' => 'master-dropdown'),
);
$form['dependent_dropdown_holder'] = array(
'#tree' => TRUE,
'#prefix' => '<div id="dependent-dropdown-wrapper">',
'#suffix' => '</div>',
);
$form['dependent_dropdown_holder']['dependent_dropdown'] = array(
'#type' => 'select',
'#title' => t('Dependent Dropdown (changes based on master dropdown)'),
// when the form is rebuilt during processing (either AJAX or multistep),
// the $master_selction variable will now have the new value and so the
// options will change.
'#options' => _custom_form_get_second_dropdown_options($master_selection),
'#default_value' => isset($my_values['dependent_dropdown']) ? $my_values['dependent_dropdown'] : '',
);
return $form;
}
/**
* Helper function to populate the second dropdown. This would normally be
* pulling data from the database.
*
* @param key. This will determine which set of options is returned.
*
* @return array of options
*/
function _custom_form_get_second_dropdown_options($key = '') { //
$options = array(
t('shop1') => _custom_form_get_shops('shop1'),
t('shop2') => _custom_form_get_shops('shop2'),
t('shop3') => _custom_form_get_shops('shop3'),
t('shop4') => _custom_form_get_shops('shop4'),
);
if (isset($options[$key])) {
return $options[$key];
}
else {
return array();
}
// return $options;
}
/**
* Change this query to adapt to your needs
*/
function _custom_form_get_shops($key = '') {
// Query to search for ...
$sql = "SELECT * FROM %s";
$result = db_query($sql, $key);
// WE STORE THEM (FULL NODE) IN AN ARRAY
while( $row = db_fetch_array( $result ) ) {
$options[] = $row['name'];
}
return $options;
}
/**
* Helper function to populate the first dropdown. This would normally be
* pulling data from the database.
*
* @return array of options
*/
function _custom_form_get_first_dropdown_options() {
// drupal_map_assoc() just makes an array('Strings' => 'Strings'...).
return drupal_map_assoc(array(t('shop1'), t('shop2'), t('shop3'), t('shop4')));
}
/**
* The AHAH callback. It processes the form using ahah_example_callback_helper()
* and then
*/
function custom_form_dropdown_callback() {
$form = ahah_custom_form_custom_webforms_callback_helper();
$changed_elements = $form['dependent_dropdown_holder'];
// Prevent duplicate wrappers.
unset($changed_elements['#prefix'], $changed_elements['#suffix']);
$output = theme('status_messages') . drupal_render($changed_elements);
drupal_json(array(
'status' => TRUE,
'data' => $output,
));
}
/**
* Does the very standard things that must be done in any normal callback.
* Used by each callback in this example module.
*/
function ahah_custom_form_callback_helper() {
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
// Enable the submit/validate handlers to determine whether AHAH-submittted.
$form_state['ahah_submission'] = TRUE;
$form['#programmed'] = $form['#redirect'] = FALSE;
drupal_process_form($form_id, $form, $form_state);
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
return $form;
}
@priyankal
Copy link

can anyone knows how to create a dependent dropdown list in php ajax for mutiple tables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment