0
votes

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

1
It sounds like your additional hooks might just be the "straw that broke the camels back". You should verify that without your code, the application is not already running near to the 30 seconds; due to another bug or issue.berkes
there is no bug, i removed the above code, site working fine.Bharanikumar
" site working fine"; sure. But does it run, like 2x.xx seconds? If so, adding your code, could trigger Drupal into running certain hooks, which then pushes the total runtime just above that 30 seconds.berkes
As per your response, Is my code is correct ?Bharanikumar
Try github.com/fotuzlab/appgati to find the resources this function is using.fotuzlab

1 Answers

1
votes

I wouldn't have thought it's slowing your site down by 30 seconds but one obvious problem with your code is that you're building the roles for every single form. You should do the form id check first like so:

<?php
function bartik_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'user_register_form') {
    $result = db_query("SELECT name, rid FROM role where rid > 3");
    foreach ($result as $row){
      $option[$row->rid] = $row->name;
    }

    $form['user_type'] = array(
      '#type'=>'select',
      '#title' => t('User Type'),
      '#options' => $option,
      '#multiple' => false,
      '#attributes'=>array('size'=>0),
      '#required' => TRUE,
    );
  }
}

You could also just use bartik_form_user_register_form_alter(&$form, &$form_state) which targets that specific form.

I'd also recommend using Devel and Xhprof to see where all the execution time is been spent.