Use a view as source for a webform select component

Public

This code allows you to use the results of a view as options in a webform component. For instance, if you have a view of nodes with type 'product', you can let the end user select a product from a select list.

Get raw version
php
  1. /**
  2.  * Implements hook_webform_select_options_info().
  3.  */
  4. function MYMODULE_webform_select_options_info() {
  5. $items = array();
  6.  
  7. $items['products'] = array(
  8. 'title' => t('Products'),
  9. 'options callback' => '_MYMODULE_get_product_options',
  10. );
  11.  
  12. return $items;
  13. }
  14.  
  15. /**
  16.  * Helper function, loads and executes a view, returns an array of $nid => $title pairs.
  17.  */
  18. function _MYMODULE_get_product_options() {
  19. $options = array();
  20.  
  21. $results = views_get_view_result('products', 'default');
  22. foreach ($results as $result) {
  23. // This code assumes that the view contains a node_title field.
  24. $options[$result->nid] = $result->node_title;
  25. }
  26.  
  27. return $options;
  28. }