2

I am using webforms and I want to add a Select Option field with the list of all authenticate user names. And if any new user add in my site than it will add automatically in that list Is that possible? if yes than how?

1

You have to look into hook_webform_select_options_info.

It is quite straight-forward. You use the hook to define a new kind of select option list and register a callback. In the callback, you return the list of all users.

Assuming your module name is webform_users_list.

/**
 * Implementation of hook_webform_select_options_info.
 */
function webform_users_list_webform_select_options_info() {
  $items = array();

  $items['users_list'] = array(
    'title' => t('List of registered users'),
    'options callback' => 'webform_users_list_get_list',
    'options arguments' => array(),
  );

  return $items;
}

function webform_users_list_get_list($component, $flat, $filter, $arguments) {
  // Get list of users through a method such as EntityFieldQuery or db_select.
  return $users;
}

If the callback is in a different file, you need to define the 'file' in the array in hook_webform_select_options_info as per regular Drupal conventions.

To use this in your webform, add a regular select option to your webform and while configuring, you will be able to select the option list as List of registered users.

  • yes, to my knowledge this requires a custom hook in a module. – tenken Jun 9 '13 at 16:58
  • as a new drupal user I'm not sure where to put this code, can you explain briefly how to add a hook? Does this just mean create a new module that uses the webform's "API" and define whatever functions I need (as above) as per module development guidelines? I certainly don't want to add this to the module code directly... any advice would be helpful – Kasapo Mar 28 '14 at 19:11
  • You just need to implement a small custom module, and then configure your webform through the UI, here's a good example:drupalsteve.com/node/57. The code here modified to your needs will generate the list of options to add to your webform – nizz0k May 9 '15 at 19:49
1

You could use the References module. This module enables you to use the results from a view as the values for a field.

  • but my question is different, I want to add authenticate user's name in a field type select option and it will add automatically when a new user register with my site and using References module is not possible I already check this one either I am missing something or its not solution of this question. – Mohit Goyal Jun 9 '13 at 15:20
  • From what I understand, you want the users that are currently logged in? Then you can do it with a custom query against the session table to get all users that are logged in. – Sorin S. Jun 9 '13 at 15:42
  • No, not currently logged in users in want all users those are register to my website – Mohit Goyal Jun 9 '13 at 16:23
1

I just did something quite similar today, with using entity form module instead of webform which can have the same results.

using Entity refrence, you can get that working in like 2 minutes.

0

You can put this code as second function to get the list of users :

function webform_users_list_get_list($component, $flat, $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;
}

So the complete file webform_users_list.module will be :

/**
 * Implementation of hook_webform_select_options_info.
 */

function webform_users_list_webform_select_options_info() {
  $items = array();

  $items['users_list'] = array(
    'title' => t('List of registered users'),
    'options callback' => 'webform_users_list_get_list',
    'options arguments' => array(),
  );

  return $items;
}

/* Get user list and return it */

function webform_users_list_get_list($component, $flat, $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;
}
  • I found lot of code for call function but couldn't figure out how arguments of function will be passing during callback?. In above example $component, $flat, $arguments is present in function argument list but in calling function only array() is passed as argument. Will you please provide any link/resouce upon this? In some examples 'options' => 'function_name' is used instead of 'options callback' => 'function_name' . Is there any difference or just a coder's approach? – kiranking Mar 16 '15 at 16:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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