Hi,

I need to change the checkboxes from registration form to radio buttons to be sure that the user only select one role.

I've tried to modify this function, but appears the error "Fatal error: Unsupported operand types in ..."

// Generate the form items.
  $form['account']['roles_change'] = array(
--    '#type' => 'checkboxes',
++    '#type' => 'radios',
    '#title' => isset($form['account']['roles']['#title']) ? $form['account']['roles']['#title'] : t('Roles'),

Could somebody help me?

Thank you!

Comments

dromansab’s picture

Priority: Major » Normal

Nobody nows how restrict users to select only one role?

Taxoman’s picture

Perhaps there is some relevant code in the OG Registration Format module:
http://drupal.org/sandbox/madmatter23/1287462

This module will let you change the format of the 'Groups' field on the User Registration form to:
Checkbox (default)
Select List
Autocomplete Textfield

rudy2342’s picture

Has anyone figured this out? Did OG Registration Format help? I was close with https://drupal.org/sandbox/wolmi/2022403.
However the checkbox from role delegation is still there for users without the admin toolbar

<?php
function one_role_form_alter(&$form, &$form_state, $form_id){
    switch($form_id){
        case 'user_register_form':
        case 'user_profile_form':
            $form['account']['roles']['#type'] = 'radios';
            $form['account']['roles']['#required'] = TRUE;
            foreach($form['account']['roles']['#default_value'] as $key => $default){
                if($default != 2){
                    $form['account']['roles']['#default_value'] = $form['account']['roles']['#default_value'][$key];
                }
            }
            break;
    }
}

/**
 * Implements hook_user_presave()
 * Hack for registration & profile forms, attaches the authenticated role
 */
function one_role_user_presave(&$edit, $account, $category) {
    if (isset($edit['roles'])) {
        $edit['roles'] = array ( $edit['roles'] => $edit['roles'], 2 => '1' );
    }
}
?>
mattyy21’s picture

HI I use a JQuery script and checkboxes in "user.module" line 1127. WORK!

$checkbox_authenticated = array(
    '#type' => 'checkbox',
    '#title' => $roles[DRUPAL_AUTHENTICATED_RID],
    '#default_value' => TRUE,
    '#disabled' => TRUE,
  );
  unset($roles[DRUPAL_AUTHENTICATED_RID]);
   $form['account']['roles'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Roles'),
    '#default_value' => (!$register && isset($account->roles) ? array_keys($account->roles) : array()),
    '#options' => $roles,
    '#access' => $roles && user_access('administer permissions'),
    DRUPAL_AUTHENTICATED_RID => $checkbox_authenticated,
  );
  echo '<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  <script>
  $(document).ready(function(){
  	$("#edit-roles input").change(function(e){
  		console.log("changed");
		var i = 0;
  		$("#edit-roles input").each(function(){
  			if(i == 0){
  				i++
  			} else {
	  			$(this).attr("checked",false);
  			}
  		})
		console.log(e)
  		e.currentTarget.checked = true;
  	})
  })
	  </script>';
  $form['account']['notify'] = array(
    '#type' => 'checkbox',
    '#title' => t('Notify user of new account'),
	'#default_value' => TRUE,
    '#access' => $register && $admin,
  );
dalin’s picture

Issue summary: View changes
Status: Active » Closed (works as designed)

This ticket is very old, but for anyone who comes across it, take a look at
https://www.drupal.org/project/single_user_role