1

I have a webform with a select option type field. I need an authenticated user to view other available users in the website in this list. So, it needs to be dynamic by communicating with the database. What is the best way to do this?

Thanks in advance!

2 Answers 2

1

You have to use hook_webform_select_options_info() in your custom module.

I made my first module with that hook because i needed to achieve a similar thing but with people who were linked thanks to module relation. I show you what i did (i change the query to retrieve all users from the site, i'm not an expert in query). It was my first code, so it's not very optimize but it's work.

function my_module_webform_select_options_info() {
  $items['membres'] = array(
  'title' => t('Membres'), 
  'options callback' => 'webform_membres_options', 
  );
return $items;
}

function webform_membres_options($node)  {
  global $user;
  $store = array();

  $result = db_query("SELECT users.name AS users_name, users.uid AS uid, users.mail AS  users_mail
                      FROM {users} users
                      WHERE (( (users.status <> '0') ))");


 //Store mail|username in an array (good format for select options) 
 foreach ($result as $row) {
   $store[$row -> users_mail] = $row -> users_name;
 }
 //All users don't have relationship so we have to check the length of array            
 if (count($store > 0)) {
   return $store;
 } 
     else {
   return false;
 }
 }

You'll see your list in Load a pre-built option list when you'll build your webform (after selecting "select options") Don't forget to put a default value for the select list. After you'll see your list in the webform : webform select

4
  • Thanks. I am just a beginner to Drupal and hence I am not that wee versed with stuff. If I am not asking too much, please let me know if I am right. I should now go to the webform.api.php.... add the lines of the code for querying the DB and this should take care of things. Right?
    – user9890
    Sep 11, 2012 at 5:14
  • @user9890 welcome to drupal answers, when you post a question here please ensure it does not ask for a "From scratch implementation".. no one likes to do others' homework, you need to try things on your own first and in case you are stuck ask for help. The FAQ should help you on what you can and can't ask here. Secondly its taboo to modify/hack a core or contributed module in drupal, you should look up the internet on writing a 'custom module' and usage of hooks in drupal.. there are plentiful resources that would help you with it and get your task done. Sep 11, 2012 at 7:07
  • There're plenty of resources when you want to create your first module : drupal.org and this one is easy to follw
    – Epok
    Sep 11, 2012 at 8:00
  • @optimusprime619 Sorry about that. This is not my homework, it was just a personal website which I am trying to do, with forums and discussions. I wanted to make sure, because I did not want to break the integrity of the site or create any errors by modifying any wrong file. Thank you for the input.
    – user9890
    Sep 11, 2012 at 13:49
1

Here is another aproach. I just used this on a project i am working on.

/**
 * Same as example above
 */
function YOUR-MODULE_webform_select_options_info() {
  $items = array();
  $items['users'] = array(
    'title' => t('Users'),
    'options callback' => '_YOUR-MODULE',
  );

  return $items;
}

/**
 * Get a list of all uids except uid 1
 * This is easier to read.
 */
function _YOUR-MODULE($component, $flat, $filter, $arguments) {

  $all_users = entity_load('user');
  foreach($all_users as $value) {
    $user_list = (array)$value;
    if($user_list['uid'] > 1) {
      $user_names[$user_list['uid']] = $user_list['name'];
    }
  }

  return $user_names;
}

/**
 * Set the default value of the select list to the current user
 */
function YOUR-MODULE_form_alter( &$form, &$form_state,$form_id ){
  global $user;
  $uid = $user->uid;

  // 
  if ($form_id == 'webform_client_form_YOUR-WEBFORM-ID' && $uid > 1) {
    // manipulate component values like this:
    $form['submitted']['YOUR-WEBFORM-FIELDNAME']['#default_value'] = $uid;
  }
}

Edit: I added some more comments and a solution for setting the default value of the select list to the current ID.

2
  • Hello and welcome. Answers with code only may work, but they are "doing things by the rote" if they work indeed. If the code is not exactly fit for that particular purpose, they are outright useless. It is much better to describe approach in human readable language and only back it up with code. And if code is most important part of the answer, please at least add some comments to explain what and how it is supposed to do.
    – Mołot
    Jan 9, 2014 at 13:03
  • I just updated my example. Hope it helps.
    – Mike
    Jan 9, 2014 at 13:34

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.