Skip to content

Instantly share code, notes, and snippets.

Created December 12, 2011 20:46
Show Gist options
  • Save anonymous/1469010 to your computer and use it in GitHub Desktop.
Save anonymous/1469010 to your computer and use it in GitHub Desktop.
Modify user_register form to add custom form for adding to og_users_roles table.
name = RCG Register
description = Demonstrates how to alter the registration form with an AHAH element.
core = 6.x
<?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;
}
function rcg_register_form_user_register_alter(&$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('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')) {
$gid = $form_state['values']['og_register'];
$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.
$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