I altered the username field in login block into a select field and now I'm trying to filter the usernames by their user roles via another Select Field.

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

    $users = entity_load('user');
    $user_names = array();
    $acc = array("PH","SL");
    $rolePH = array();
    $roleSL = array();
    foreach ($users as $user_id => $user) {
    $user_names[] = $user->name;
      if (in_array('PH', array_values($user->roles))) {
        $rolePH[] = $user->name;
      elseif (in_array('SL', array_values($user->roles))) {
        $roleSL = $user->name;
    $user_arr = array_combine($user_names, $user_names);
    $form['roles']['#type'] = "select";    
    $form['roles']['#weight'] = -0;
    $form['roles']['#size'] = 'auto';
    $form['roles']['#options'] = $acc;
    $form['roles']['#title'] = t('Role');
    $form['name']['#type'] = "select";
    $form['name']['#options'] = $user_arr;
    $form['name']['#required'] = true;
    $form['name']['#size']= 'auto';
    $form['name']['#title']= t('Organization');


I'm a bit stuck, as you can see, I already separated users depending on their roles. My problem is how do I set the $form['name'] options when a role is selected. Right now I'm just showing all users in the DB.


  • What is design_form? Where does it come from? How does it look like when you dpr($form) or var_dump($form) at the beginning of your alter code? To help you with how, we need to know what we are working on.
    – Mołot
    Commented Sep 8, 2014 at 7:53
  • design is the name of the theme I'm using and I think $form is a parameter of the method hook_form_alter.
    – Richard
    Commented Sep 8, 2014 at 7:55
  • you got the parameters right, but I'm asking what's in them. Also, logic like that should rather belong to module. Are you looking for Form API AJAX? Last but not least: are you altering all the forms now? Because you are not testing if form ID means login form...
    – Mołot
    Commented Sep 8, 2014 at 8:03
  • Tried var_dump($form) it just displayed an array looking like this pastie.org/9535700. Yes, I'm altering the form when its loaded in /user. I actually tested if($form_id=="user_login") its there.
    – Richard
    Commented Sep 8, 2014 at 8:18
  • Please post the actual code you are using, because now it's trimmed too much and misleading a bit. And have you read about Ajax and states in form api?
    – Mołot
    Commented Sep 8, 2014 at 8:35

1 Answer 1


Borrowed From HERE:

If you want to do this hardcoded, you might try to use the #ajax property of the Form API

You can reload your second dropdown on change of the value of the first dropdown and change the value while reloading the form.

I made a little example to give you an idea :

$form['number'] = array(
  '#type' => 'select',
  '#title' => t('Select Number'),
  '#options' => array(
      0 => 1,
      1 => 2,
      2 => 3,
  '#default_value' => 0,
  '#ajax' => array(
    'callback' => 'ajax_replace_dropdown_default_value_callback',
    'wrapper' => 'wrapper-dropdown',
    'method' => 'replace',
    'effect' => 'fade',

$default_value = !empty($form_state['values']['number']) ? $form_state['values']['number'] : 0;

$form['open_closed'] = array(
  '#type' => 'select',
  '#title' => t('Open / Closed ?'),
  '#options' => array(
      0 => t('Open'),
      1 => t('Closed'),
      2 => t('Progress'),
  '#value' => $default_value,
  '#prefix' => '<div id="wrapper-dropdown">',
  '#suffix' => '</div>',

function ajax_replace_dropdown_default_value_callback($form, $form_state) {
  return $form['open_closed'];

In your case you will have to do it in a hook_form_alter and add the #ajax property to your first dropdown and add prefix and suffix to give the second dropdown the proper value.

You can also check this link about AJAX with the FORM API :

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.