Skip to content

Instantly share code, notes, and snippets.

@daggerhart
Created January 4, 2017 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daggerhart/b4aeaecd642d3f839740f0d9fa3d1bfc to your computer and use it in GitHub Desktop.
Save daggerhart/b4aeaecd642d3f839740f0d9fa3d1bfc to your computer and use it in GitHub Desktop.
Drupal 7 Webform dynamic select option list for providing taxonomy terms as an option list
<?php
/**
* Implements hook_webform_select_options_info().
*/
function MODULE_webform_select_options_info() {
$items = array();
$vocabularies = taxonomy_get_vocabularies();
foreach( $vocabularies as $vocabulary ){
$items['vocabulary_'.$vocabulary->machine_name] = array(
'title' => 'Vocabulary: ' . $vocabulary->name,
'options callback' => 'vocabulary_terms_webform_select_options_callback',
'options arguments' => array(
'vid' => $vocabulary->vid,
),
);
}
return $items;
}
/**
* Option callback for vocabulary select options
*
* @param $component
* @param $flat
* @param $arguments
*
* @return array
*/
function vocabulary_terms_webform_select_options_callback( $component, $flat, $arguments ){
$options = array();
$terms = entity_load('taxonomy_term', FALSE, array('vid' => $arguments['vid']));
foreach( $terms as $term ){
$options[ $term->tid ] = $term->name;
}
return $options;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment