Last active
August 29, 2015 14:10
-
-
Save jeetonweb/7ccffdd057e1072d95d7 to your computer and use it in GitHub Desktop.
A Drupal 7 custom module that helps building dynamic select boxes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* Implements form_alter(); | |
* @param type $form | |
* @param type $form_state | |
* @param type $form_id | |
*/ | |
function common_form_alter(&$form, &$form_state, $form_id) { | |
if ($form_id == 'article_node_form') { | |
// Add Ajax callback to parent field. | |
$form['field_data_type'][LANGUAGE_NONE]['#ajax'] = array( | |
'callback' => 'common_data_type_callback', | |
'wrapper' => 'sub-data-container', | |
); | |
// Add Wrapper div to child field | |
$form['field_sub_data']['#prefix'] = '<div id="sub-data-container">'; | |
$form['field_sub_data']['#suffix'] = '</div>'; | |
// When ajax callback comes or form reloads with data in it. | |
// Child field should have appropriate options in it. | |
if (isset($form_state['values']['field_data_type'][LANGUAGE_NONE][0]['value'])) { | |
$subdata_options = array(); | |
switch ($form_state['values']['field_data_type'][LANGUAGE_NONE][0]['value']) { | |
case 'month': | |
$subdata_options = array( | |
'' => '-- Please Select Month -- ', | |
'jan' => 'January', | |
'feb' => 'February', | |
'mar' => 'Marach', | |
); | |
break; | |
case 'week': | |
$subdata_options = array( | |
'' => '-- Please Select Day --', | |
'sun' => 'Sunday', | |
'mon' => 'Monday', | |
'tue' => 'Tuesday', | |
); | |
break; | |
} | |
$form['field_sub_data'][LANGUAGE_NONE]['#options'] = $subdata_options; | |
} | |
} | |
} | |
/** | |
* Ajax callback function on change of field_data_type | |
* This function just returns the replaceable content. all logic written on form_alter. | |
* @param type $form | |
* @param type $form_state | |
* @return type | |
*/ | |
function common_data_type_callback($form, $form_state) { | |
return $form['field_sub_data']; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment