Search code examples
drupaldrupal-6drupal-fapi

Drupal creating a select box and populating it with user reference


I am creating a custom form in Drupal. In this form I wish to create a select box. In this box I wish to list all users on the site. When saving my form this will populate a CCK field which references users.

I know to create the select list I use something like:

$form['access']['timer'] = array(
    '#type' => 'select',
    '#title' => t('Discard logs older than'),
    '#default_value' => variable_get('timer', 259200),
    '#options' => $period,
    '#description' => t('The timer.'),
  );

http://drupal.org/node/751826

What I need to know is how I get the correct data to set #options.


Solution

  • Creating the select field is really the easiedt thing to do of what you need to do. You should also take a look at the drupal form API reference. There is a link to it at api.drupal.org.

    Anyways what you want is to display the username in the select but have the value in form be the user id, this is what CCK needs. To do this you need to created a keyed array

    $options = array(
      $uid => $name,
       ...
    );
    

    To create the options you need to query the user table using db_query and add all the users to the array.