Created
December 7, 2011 10:55
-
-
Save rfay/1442392 to your computer and use it in GitHub Desktop.
Form alter the user form with AHAH
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
| name = User register alter example | |
| description = Demonstrates how to alter the registration form with an AHAH element. | |
| core = 6.x |
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 | |
| function user_register_alter_example_form_user_register_alter(&$form, &$form_state) { | |
| // This is an example of just injecting some spare form | |
| $form['og_register']['x'] = user_register_alter_example_spare_form($form_state); | |
| // This is how to add a section with a #ahah button. It's pretty much | |
| // identical to the stuff in the ahah example, but it is being added | |
| // with a form alter. | |
| $form['og_register']['ahahbutton'] = array( | |
| '#type' => 'submit', | |
| '#value' => t('Add more stuff'), | |
| '#ahah' => array( | |
| 'wrapper' => 'og_register_ahah_stuff', | |
| 'path' => 'og_register_stuff', | |
| ), | |
| '#validate' => array(), | |
| '#submit' => array(), | |
| ); | |
| $form['og_register']['where_extra_stuff_goes'] = array( | |
| '#prefix' => '<div id="og_register_ahah_stuff">', | |
| '#suffix' => '</div>', | |
| '#type' => 'fieldset', | |
| ); | |
| if (!empty($form_state['values']['op']) && $form_state['values']['op'] == t('Add more stuff')) { | |
| $form['og_register']['where_extra_stuff_goes']['myselect'] = array( | |
| '#type' => 'select', | |
| '#title' => t('A select element'), | |
| '#options' => array('one' => 'One', 'two' => 'Two'), | |
| ); | |
| // You have to add to the form submission something to work with your extra | |
| // form elements. | |
| // $form['#submit'][] = 'some_submit_function_that_will_process_extra_stuff'; | |
| } | |
| // To do the above, though, I think we have to relax the #required | |
| } | |
| function user_register_alter_example_og_register_callback() { | |
| $form = user_register_alter_example_callback_helper(); | |
| $fieldset = $form['og_register']['where_extra_stuff_goes']; | |
| // Remove the wrapper so we don't double it up. | |
| unset($fieldset['#prefix'], $fieldset['#suffix']); | |
| $output = theme('status_messages'); | |
| $output .= drupal_render($fieldset); | |
| // Final rendering callback. | |
| drupal_json(array('status' => TRUE, 'data' => $output)); | |
| exit(); | |
| } | |
| function user_register_alter_example_spare_form(&$form_state) { | |
| $form['sometitle'] = array( | |
| '#type' => 'textfield', | |
| '#title' => t('This is a spare form being injected'), | |
| ); | |
| return $form; | |
| } | |
| /** | |
| * 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 user_register_alter_example_menu() { | |
| $items = array(); | |
| $items['og_register_stuff'] = array( | |
| 'page callback' => 'user_register_alter_example_og_register_callback', | |
| 'access callback' => TRUE, | |
| 'type' => MENU_CALLBACK, | |
| ); | |
| return $items; | |
| } | |
| /** | |
| * Does the very standard things that must be done in any normal callback. | |
| * Used by each callback in this example module. | |
| */ | |
| function user_register_alter_example_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; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment