Drupal 7 : How to add role selection field to the registration form

 If any module doesn't fit your requirement then you can use the below code You can create any custom module and put code in .module file.

function custom_code_form_alter(&$form, &$form_state, $form_id) {
	 
	
	if($form_id == 'user_register_form') {
		
		$form['account']['roles']['#title'] = t('User type');
		$form['account']['roles']['#access'] = TRUE;
		$form['account']['roles']['#required'] = TRUE;
		$form['account']['roles']['#type'] = 'select';
		foreach ($form['account']['roles']['#options'] as $key => $value) {
			if($value == 'administrator') {
				unset($form['account']['roles']['#options'][$key]);
			}
		}
		
		
	}
		
		
		
}
function custom_code_user_presave(&$edit, $account, $category) {
  if (isset($edit['roles'])) {
    is_array($edit['roles']) ? '' : $edit['roles'] = array ( $edit['roles'] => $edit['roles'], 2 => '1' );
    $edit['roles'] = array_filter($edit['roles']);
  }
}



 

No comments:

Write a program in PHP to reverse a number

A number can be written in reverse order. For example 12345 = 54321 <?php   $ num = 23456;   $ revnum = 0;   while ($ num > 1)   {   $...