Custom option list in drupal webform D7

Webforms is a great module to built a form on your drupal site.
When you want to use a select list element in your form and need to populate it with your custom values, here is what you need to do:

function custommodule_webforms_webform_select_options_info(){


    $items = array();
    $items['products'] = array(
        'title' => t('Products'),
        'options callback' => 'custommodule_webforms_webform_list_products'
    );

    return $items;
}

function custommodule_webforms_webform_list_products() {
    $nodes = node_load_multiple(array(), array('type' => 'product'));

    $contentarray = array();
    $contentarray[0] = t("Select product");

    foreach ($nodes as $node) {
        $contentarray[$node->title] = t($node->title);
    }

    return $contentarray;

}

Add new comment