-
-
Save rfay/1473074 to your computer and use it in GitHub Desktop.
Modify user_register form to add custom form for adding to og_users_roles table.
This file contains 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 = RCG Register | |
description = Demonstrates how to alter the registration form with an AHAH element. | |
core = 6.x |
This file contains 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 | |
/** | |
* 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 rcg_register_menu() { | |
$items = array(); | |
$items['og_register_stuff'] = array( | |
'page callback' => 'rcg_register_og_register_callback', | |
'access callback' => TRUE, | |
'type' => MENU_CALLBACK, | |
); | |
return $items; | |
} | |
// @wonder95: | |
// We'll *have* to use hook_form_alter() instead of hook_form_FORM_ID_alter, | |
// because we need the last shot at this. | |
// Also, for testing, I set the module weight to 100. | |
function rcg_register_form_alter(&$form, &$form_state, $form_id) { | |
// Note that user_register is used for anon signup as well. Careful about | |
// this. | |
if ($form_id != 'user_register') { | |
return; | |
} | |
// 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. | |
// @wonder95: We will have to kill validation for this somehow to work around the | |
// bug in validation. Then these validation attributes will have to be handled in | |
// a separate validation handler provided for the form (or for the main submit button). | |
// Note that #required is just a special validation handler added on later, | |
// so it can be done with a specific validation handler. | |
unset($form['account']['name']['#required'], $form['account']['mail']['#required'], $form['account']['pass']['#required']); | |
// For now I'm going to just remove the pass field. You'll need to figure | |
// out a way to handle this. | |
unset($form['account']['pass']); | |
$form['og_register']['ahahbutton'] = array( | |
'#type' => 'submit', | |
'#value' => t('Select Roles'), | |
'#ahah' => array( | |
'wrapper' => 'og-register-ahah-wrapper', | |
'path' => 'og_register_stuff', | |
'method' => 'append', | |
), | |
'#validate' => array(), | |
'#submit' => array(), | |
); | |
$form['og_register']['account_roles'] = array( | |
'#prefix' => '<div id="og-register-ahah-wrapper">', | |
'#suffix' => '</div>', | |
'#type' => 'fieldset', | |
); | |
if (!empty($form_state['values']['op']) && $form_state['values']['op'] == t('Select Roles')) { | |
// @wonder95 here $form_state['values']['og_register'] is an array, so this has to be adjusted. | |
$gids = array_keys($form_state['values']['og_register']); | |
// $gid = $form_state['values']['og_register']; | |
foreach ($gids as $gid) { | |
$form['og_register']['account_roles'][] = rcg_register_add_roles_form($gid); | |
} | |
} | |
// Add custom submit function to handle added roles form. | |
$form['#submit'][] = 'rcg_register_og_roles_submit'; | |
} | |
function rcg_register_og_register_callback() { | |
$form = rcg_register_callback_helper(); | |
$fieldset = $form['og_register']['account_roles']; | |
// 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(); | |
} | |
/** | |
* Does the very standard things that must be done in any normal callback. | |
* Used by each callback in this example module. | |
*/ | |
function rcg_register_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; | |
} | |
/** | |
* Form builder function for form to add roles. | |
*/ | |
function rcg_register_add_roles_form($gid) { | |
$account_node = node_load($gid); | |
// Get roles that are defined for accounts in og. | |
// @wonder95: I don't see this variable ever getting set, so this returns "0", and gets filtered out. | |
$og_account_roles = variable_get('og_user_roles_roles_account', ''); | |
$og_account_roles = array_filter($og_account_roles); | |
foreach($og_account_roles as $rid) { | |
$role_name = db_result(db_query("SELECT name FROM {role} WHERE rid = %d", $rid)); | |
$options[$rid] = $role_name; | |
} | |
$form['account_' . $gid] = array( | |
'#type' => 'checkboxes', | |
'#title' => t('Available roles for: ' . $account_node->title), | |
'#options' => $options, | |
); | |
return $form; | |
} | |
/** | |
* Custom submit function to submit og_roles form. | |
*/ | |
function rcg_register_og_roles_submit($form, &$form_state) { | |
foreach ($form_state['values'] as $key => $data) { | |
if (substr($key, 0, 8) == 'account_') { | |
$gid = substr($key, strpos($key, '_')+1); | |
foreach (array_filter($data) as $rid) { | |
og_user_roles_role_add($gid, $form_state['user']->uid, $rid); | |
} | |
} | |
} | |
$form_state['#redirect'] = 'user'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment