i am just adding the user_type element in the signup form. i want to insert that user_type in the user_roles table.
Getting error
Fatal error: Maximum execution time of 30 seconds exceeded
function bartik_form_alter(&$form, &$form_state, $form_id) {
$result = db_query("SELECT name, rid FROM role where rid > 3");
foreach ($result as $row){
$option[$row->rid] = $row->name;
}
if ($form_id == 'user_register_form') {
$form['user_type'] = array(
'#type'=>'select',
'#title' => t('User Type'),
'#options' => $option,
'#multiple' => false,
'#attributes'=>array('size'=>0),
'#required' => TRUE,
);
$form['#submit'][] = 'bartik_form_alter_submit'
}
}
/**
* Additional handler for user_login form submit.
*/
function bartik_form_alter_submit($form, &$form_state) {
if (isset($form_state['values']['user_type'])){
$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 = $form_state['values']['user_type'];
$nid = db_insert('users_roles')
->fields(array('uid' => $uid,'rid' => $rid,))
->execute();
}
}
Updated Status: As per @Ben suggestion, i have modified the snippet, in registration page selected roles are updated into table.
Please provide your suggestion / feedback for my updated code. is it the right way to insert roles into drupal table.
i have tried the above snippet in freshly installed drupal application. To save the singup form system tooks nearly 4min. Drupal version 7.24