You are here

README.txt in Webform Views Select 7

INTRODUCTION
------------

This module will let you populate a webform select component with data from a
view. 


REQUIREMENTS
------------

This module requires the following modules:

 * Webform (https://www.drupal.org/project/webform)
 * Views (https://www.drupal.org/project/views)


INSTALLATION
------------

Install as you would normally install a contributed Drupal module. Visit:
https://drupal.org/documentation/install/modules-themes/modules-7 
for further information.


CONFIGURATION
-------------

The module has no menu or modifiable settings. There is no configuration.

Don't disable this module if there are webforms that use a select list from
this module, since it will break those webforms.


TROUBLESHOOTING
---------------

 * Creating the View
  - Create a new view
  - Create a "Webform Options" display
  - Select the Webform Select List format
  - Add the fields you want to use as key and value
  - Filter and sort as needed
  - In the format settings, select the field to use as key and value.

 * Using the View
  Add the select component to your webform and pick your view under "Load a 
  Pre-Built Option List".

 * Caveat
  Please keep in mind that the list that will loaded on demand from the view is
  not a hard coded list. When the webform is viewed, the component will get its
  data directly from the view.

  Also note that you can no longer see the item in the webform results when the
  item is removed from the View results.

 * Using view arguments in a multi-part webform
  Using a hook_form_alter, it is possible to change the option list to make use
  of view arguments. Here's an example on how to do that (thanks jonny5th!):

// Form alter to select options from views based on input
function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'webform_client_form_####'
      && isset($form_state['submitted'])
      && $form_state['submitted'] == TRUE
      && isset($form_state['webform_completed'])
      && $form_state['webform_completed'] == FALSE
      && $form_state['webform']['page_num'] == 2) {

    // Set the view and display.
    $view = 'your_view';
    $display = 'webform_select_1';

    // shorthand variable
    $submitted = $form_state['input']['submitted'];
    $options = array();
    // Check if input field on first page is filled in:
    if(isset($submitted['your_field']) && $submitted['your_field'] != "") {
      // Get data the way Views likes its arguments. $arguments must be an
      // array!
      $view_arguments = MYMODULE_get_view_args($submitted['your_field']);
      $args = array(
				'view' => $view,
				'display_id' => $display,
				'view_args' => $arguments
      );
      $options = webform_views_select_options(NULL, FALSE, $args);
    }

    // If there are no options returned uusing our views arguments above, use
    // this default argument:
    if(sizeof($options) < 1) {
      $args = array(
        'view' => $view,
        'display_id' => $display,
        'view_args' => array('all')
      );
      $options = webform_views_select_options(NULL, FALSE, $args);
    }

    // Set the #options array for our webform component.
    $form['submitted']['select_component']['#options'] = $options;
  }
}

File

README.txt
View source
  1. INTRODUCTION
  2. ------------
  3. This module will let you populate a webform select component with data from a
  4. view.
  5. REQUIREMENTS
  6. ------------
  7. This module requires the following modules:
  8. * Webform (https://www.drupal.org/project/webform)
  9. * Views (https://www.drupal.org/project/views)
  10. INSTALLATION
  11. ------------
  12. Install as you would normally install a contributed Drupal module. Visit:
  13. https://drupal.org/documentation/install/modules-themes/modules-7
  14. for further information.
  15. CONFIGURATION
  16. -------------
  17. The module has no menu or modifiable settings. There is no configuration.
  18. Don't disable this module if there are webforms that use a select list from
  19. this module, since it will break those webforms.
  20. TROUBLESHOOTING
  21. ---------------
  22. * Creating the View
  23. - Create a new view
  24. - Create a "Webform Options" display
  25. - Select the Webform Select List format
  26. - Add the fields you want to use as key and value
  27. - Filter and sort as needed
  28. - In the format settings, select the field to use as key and value.
  29. * Using the View
  30. Add the select component to your webform and pick your view under "Load a
  31. Pre-Built Option List".
  32. * Caveat
  33. Please keep in mind that the list that will loaded on demand from the view is
  34. not a hard coded list. When the webform is viewed, the component will get its
  35. data directly from the view.
  36. Also note that you can no longer see the item in the webform results when the
  37. item is removed from the View results.
  38. * Using view arguments in a multi-part webform
  39. Using a hook_form_alter, it is possible to change the option list to make use
  40. of view arguments. Here's an example on how to do that (thanks jonny5th!):
  41. // Form alter to select options from views based on input
  42. function MYMODULE_form_alter(&$form, &$form_state, $form_id) {
  43. if($form_id == 'webform_client_form_####'
  44. && isset($form_state['submitted'])
  45. && $form_state['submitted'] == TRUE
  46. && isset($form_state['webform_completed'])
  47. && $form_state['webform_completed'] == FALSE
  48. && $form_state['webform']['page_num'] == 2) {
  49. // Set the view and display.
  50. $view = 'your_view';
  51. $display = 'webform_select_1';
  52. // shorthand variable
  53. $submitted = $form_state['input']['submitted'];
  54. $options = array();
  55. // Check if input field on first page is filled in:
  56. if(isset($submitted['your_field']) && $submitted['your_field'] != "") {
  57. // Get data the way Views likes its arguments. $arguments must be an
  58. // array!
  59. $view_arguments = MYMODULE_get_view_args($submitted['your_field']);
  60. $args = array(
  61. 'view' => $view,
  62. 'display_id' => $display,
  63. 'view_args' => $arguments
  64. );
  65. $options = webform_views_select_options(NULL, FALSE, $args);
  66. }
  67. // If there are no options returned uusing our views arguments above, use
  68. // this default argument:
  69. if(sizeof($options) < 1) {
  70. $args = array(
  71. 'view' => $view,
  72. 'display_id' => $display,
  73. 'view_args' => array('all')
  74. );
  75. $options = webform_views_select_options(NULL, FALSE, $args);
  76. }
  77. // Set the #options array for our webform component.
  78. $form['submitted']['select_component']['#options'] = $options;
  79. }
  80. }