0

function user_form_edit_form_alter(&$form,&$form_state,&$form_id) {


    if ($form_id=="user_register_form") {



        $form['profile_user_profile']['field_user_group']['und']['#ajax'] = array(

                                                                        'event' => 'change',
                                                                        'wrapper' => 'edit-profile-user-profile-field-user-type-und',
                                                                        'callback' => 'get_user_type',
                                                                        'method' => 'replace',
                                                                        'effect' => 'fade'

                                                                        );



        $form['#submit'][] = 'user_form_edit_form_alter_submit';


 } 
}



function get_user_type(&$form, &$form_state) {

      $selected_user_group = $form_state['values']['profile_user_profile']['field_user_group']['und'][0]['tid'];

  //  return '<pre>' . print_r($form, 1) . '</pre>';    

   // 14= USB , 15= Primary Contractor , 16 = Subcontractor

     $output = "";
     $output .= '<select class="form-select required" name="profile_user_profile[field_user_type][und]" id="edit-profile-user-profile-field-user-type-und">';

    //  if USB then Director, USB Staff, USB Project Manager, Subcontractor 
        if ($selected_user_group == 15) {

                $output .= '<option value="8">PC Staff</option>';
                $output .= '<option value="9">PC Project Manager</option>';
                $output .= '<option value="10">SC</option>';


        }

        else if ($selected_user_group == 16) {

                $output .= '<option value="10">SC</option>';


        }

        else {
            $output .= '<option value="5">Board Member</option>';
            $output .= '<option value="6">USB Staff</option>';
            $output .= '<option value="7">USB Project Manager</option>';


        }

     $output .= '</select>';
     return $output;

 } 



//  Submit Handler Function  

function  user_form_edit_form_alter_submit($form, &$form_state) {

        if (isset($form_state['values']['profile_user_profile']['field_user_type']['und'][0]['value'])) {

            $user_type_name = $form_state['values']['profile_user_profile']['field_user_type']['und'][0]['value'];




            $user_roles = db_query("SELECT name, rid FROM role where name='%s'",$user_type_name);


                $LastCreatedUsername = $form_state['values']['name'];
                $fetch_uid_arr = db_select('users', 'u')
                    ->fields('u', array('uid'))
                    ->condition('name', $LastCreatedUsername, '=')
                    ->execute()
                    ->fetchAssoc();

                $uid = $fetch_uid_arr['uid'];
                $rid = $user_roles['rid'];
                $nid = db_insert('users_roles') 
                    ->fields(array('uid' => $uid,'rid' => $rid,))
                    ->execute();
        }


}

?>enter code here

  • 4
    You can't inject arbitrary HTML into a form, or integrity checks will (correctly) fail. For ALL elements, use the API – Clive Apr 30 '14 at 10:28
  • 1
    Please follow Drupal coding standards when posting here. It will make it easier for us to read and understand your code. – Mołot Apr 30 '14 at 10:53

Browse other questions tagged or ask your own question.