dependent_dropdown.inc

  1. examples
    1. 6 ahah_example/dependent_dropdown.inc

Show/hide textfields based on checkbox clicks.

Functions & methods

NameDescription
ahah_example_dropdownForm builder function to create a form showing dependent dropdowns. The second dropdown has its values populated based on the first.
ahah_example_dropdown_callbackThe AHAH callback. It processes the form using ahah_example_callback_helper() and then
ahah_example_dropdown_continueSubmit handler for 'continue_to_dependent_dropdown'.
ahah_example_dropdown_submitDefault submit handler for form. This one happens when the main submit button is pressed.
_ahah_example_get_first_dropdown_optionsHelper function to populate the first dropdown. This would normally be pulling data from the database.
_ahah_example_get_second_dropdown_optionsHelper function to populate the second dropdown. This would normally be pulling data from the database.

File

ahah_example/dependent_dropdown.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Show/hide textfields based on checkbox clicks.
  5. */
  6. /**
  7. * Form builder function to create a form showing dependent dropdowns. The
  8. * second dropdown has its values populated based on the first.
  9. * @param $form_state
  10. * @param $my_values
  11. */
  12. function ahah_example_dropdown(&$form_state, $my_values = array()) {
  13. $form = array();
  14. // get the list of options to populate the first dropdown
  15. $initial_options = _ahah_example_get_first_dropdown_options();
  16. $form['explanation'] = array(
  17. '#type' => 'markup',
  18. '#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>',
  19. );
  20. // if we have a value for the first dropdown from $form_state['values'] we use
  21. // this both as the default value for the first dropdown and also as a
  22. // parameter to pass to the function that retrieves the options for the
  23. // second dropdown.
  24. $master_selection = !empty($form_state['values']['master_dropdown']) ? $form_state['values']['master_dropdown'] : t('Strings');
  25. $form['master_dropdown'] = array(
  26. '#type' => 'select',
  27. '#title' => 'Master Dropdown',
  28. '#options' => $initial_options,
  29. '#default_value' => $master_selection,
  30. '#ahah' => array(
  31. 'path' => 'examples/ahah_example/dependent_dropdown/callback',
  32. 'wrapper' => 'dependent-dropdown-wrapper',
  33. // 'event' => 'change', // default value: does not need to be set explicitly.
  34. ),
  35. '#attributes' => array('class' => 'master-dropdown'),
  36. );
  37. // The CSS for this module hides this next button if JS is enabled.
  38. $form['continue_to_dependent_dropdown'] = array(
  39. '#type' => 'submit',
  40. '#value' => t('Choose'),
  41. '#attributes' => array('class' => 'next-button'),
  42. '#submit' => array('ahah_example_dropdown_continue'),
  43. );
  44. $form['dependent_dropdown_holder'] = array(
  45. '#tree' => TRUE,
  46. '#prefix' => '<div id="dependent-dropdown-wrapper">',
  47. '#suffix' => '</div>',
  48. );
  49. $form['dependent_dropdown_holder']['dependent_dropdown'] = array(
  50. '#type' => 'select',
  51. '#title' => t('Dependent Dropdown (changes based on master dropdown)'),
  52. // when the form is rebuilt during processing (either AJAX or multistep),
  53. // the $master_selction variable will now have the new value and so the
  54. // options will change.
  55. '#options' => _ahah_example_get_second_dropdown_options($master_selection),
  56. '#default_value' => isset($my_values['dependent_dropdown']) ? $my_values['dependent_dropdown'] : '',
  57. );
  58. $form['submit'] = array(
  59. '#type' => 'submit',
  60. '#value' => t('Save'),
  61. );
  62. return $form;
  63. }
  64. /**
  65. * The AHAH callback. It processes the form using ahah_example_callback_helper()
  66. * and then
  67. */
  68. function ahah_example_dropdown_callback() {
  69. $form = ahah_example_callback_helper();
  70. $changed_elements = $form['dependent_dropdown_holder'];
  71. // Prevent duplicate wrappers.
  72. unset($changed_elements['#prefix'], $changed_elements['#suffix']);
  73. $output = theme('status_messages') . drupal_render($changed_elements);
  74. drupal_json(array(
  75. 'status' => TRUE,
  76. 'data' => $output,
  77. ));
  78. }
  79. /**
  80. * Submit handler for 'continue_to_dependent_dropdown'.
  81. */
  82. function ahah_example_dropdown_continue($form, &$form_state) {
  83. $values = $form_state['values'];
  84. unset($form_state['submit_handlers']);
  85. form_execute_handlers('submit', $form, $form_state);
  86. $form_state['my_values'] = $values;
  87. $form_state['rebuild'] = TRUE;
  88. }
  89. /**
  90. * Default submit handler for form. This one happens when the main submit
  91. * button is pressed.
  92. */
  93. function ahah_example_dropdown_submit($form, &$form_state) {
  94. // If an AHAH submission, it's just the dependent dropdown working.
  95. if (!empty($form_state['ahah_submission'])) {
  96. return;
  97. }
  98. if ($form_state['clicked_button']['#id'] == 'edit-submit') {
  99. $form_state['rebuild'] = FALSE;
  100. drupal_set_message(t('Your values have been submitted. master_dropdown=@first, dependent_dropdown=@second', array('@first' => $form_state['values']['master_dropdown'], '@second' => $form_state['values']['dependent_dropdown_holder']['dependent_dropdown'])));
  101. }
  102. // edit-next or anything else will cause rebuild.
  103. $form_state['rebuild'] = TRUE;
  104. }
  105. /**
  106. * Helper function to populate the first dropdown. This would normally be
  107. * pulling data from the database.
  108. *
  109. * @return array of options
  110. */
  111. function _ahah_example_get_first_dropdown_options() {
  112. // drupal_map_assoc() just makes an array('Strings' => 'Strings'...).
  113. return drupal_map_assoc(array(t('Strings'), t('Woodwinds'), t('Brass'), t('Percussion')));
  114. }
  115. /**
  116. * Helper function to populate the second dropdown. This would normally be
  117. * pulling data from the database.
  118. *
  119. * @param key. This will determine which set of options is returned.
  120. *
  121. * @return array of options
  122. */
  123. function _ahah_example_get_second_dropdown_options($key = '') {
  124. $options = array(
  125. t('Strings') => drupal_map_assoc(array(t('Violin'), t('Viola'), t('Cello'), t('Double Bass'))),
  126. t('Woodwinds') => drupal_map_assoc(array(t('Flute'), t('Clarinet'), t('Oboe'), t('Bassoon'))),
  127. t('Brass') => drupal_map_assoc(array(t('Trumpet'), t('Trombone'), t('French Horn'), t('Euphonium'))),
  128. t('Percussion') => drupal_map_assoc(array(t('Bass Drum'), t('Timpani'), t('Snare Drum'), t('Tambourine'))),
  129. );
  130. if (isset($options[$key])) {
  131. return $options[$key];
  132. }
  133. else {
  134. return array();
  135. }
  136. }
Login or register to post comments