How to use different value from select options vs what's displayed? Name vs Realname For adding multiple members to a group (course in opigno), I implemented the chosen module for better UX on the select options but instead of displaying usernames I want to display realnames.

Using $account-realname in the user loading loop displays them but after submission it breaks, as I think it expects to receive name instead.


  $form['massadd']['#options'][$account->uid] = $account->realname


$gid = current($form['group_ids']['#value']); 
$node = node_load($gid);

$form['massadd'] = array(
'#type' =&gt; 'select',
'#title' =&gt; t('Select students to add to the group'),
'#required' =&gt; TRUE,
'#multiple' =&gt; TRUE,
);

$users = entity_load('user');
 foreach ($users as $uid =&gt; $account) {
  if ($uid &amp;&amp; !in_array($uid, $exclude)) {
    $form['massadd']['#options'][$account-&gt;uid] = $account-&gt;name; // tried realname instead
  }
}

...

 $form['submit']['#weight'] = 0;
  // We validate our own way.
  unset($form['#validate']);
  $form['#submit'][] = 'opigno_simple_ui_form_og_massadd_massadd_form_add_roles';
  $form['#submit'][] = 'opigno_simple_ui_form_og_massadd_massadd_form_add_state';
  $form['#submit'] = array_merge(array('opigno_simple_ui_form_og_massadd_massadd_form_prepare'), $form['#submit']);
Having changed it to $account->name I managed to find out what's going using devel command dpr($variable) which contained the users uid.

Then I just needed to replace the user lookup query accordingly to cross check from realname table with that of the users based on uid.

$query = db_select('realname','rn');
$query-&gt;join('users','usr','usr.uid=rn.uid');
$query-&gt;fields('rn', array('uid'));
$query-&gt;condition('rn.realname',$mail,'=');
$result =$query-&gt;execute()-&gt;fetchAll();

if (!empty($result)) {
$uids =array($result[0]-&gt;uid);
$account = user_load_multiple($uids);
$account = array_shift($account);
}