Skip to content

Instantly share code, notes, and snippets.

@vdchristelle
Created November 19, 2013 15:33
Show Gist options
  • Select an option

  • Save vdchristelle/7547165 to your computer and use it in GitHub Desktop.

Select an option

Save vdchristelle/7547165 to your computer and use it in GitHub Desktop.
Webform prebuilt (prefilled) option list with nested taxonomy tree (1 language) Webform prebuilt (prefilled) option list with node titles
/**
* Implementation of hook_webform_select_options_info().
* See webform/webform_hooks.php for further information on this hook in the Webform API.
*/
function the_aim_custom_webform_select_options_info() {
$items = array();
$items['terms'] = array(
'title' => t('Taxonomy terms'),
'options callback' => '_options_terms'
);
return $items;
}
/**
* Webform options callback.
*/
function _options_terms(){
$nested = taxonomy_get_nested_tree(2, 2);
foreach ($nested as $item) {
$options[$item->tid] = '' . $item->name;
if (isset($item->children)) {
foreach ($item->children as $child) {
$options[$child->tid] = '-- ' . $child->name;
}
}
}
return $options;
}
/**
* Get nested tree.
*/
function taxonomy_get_nested_tree($vid_or_terms = array(), $max_depth = NULL, $parent = 0, $parents_index = array(), $depth = 0) {
if (!is_array($vid_or_terms)) {
$vid_or_terms = taxonomy_get_tree($vid_or_terms);
}
foreach ($vid_or_terms as $term) {
foreach ($term->parents as $term_parent) {
if ($term_parent == $parent) {
$return[$term->tid] = $term;
}
else {
$parents_index[$term_parent][$term->tid] = $term;
}
}
}
foreach ($return as &$term) {
if (isset($parents_index[$term->tid]) && (is_null($max_depth) || $depth < $max_depth)) {
$term->children = taxonomy_get_nested_tree($parents_index[$term->tid], $max_depth, $term->tid, $parents_index, $depth + 1);
}
}
return $return;
}
///////////////////////////////////////////////////////////////
//----------------------------------------------------
//--- PREFILLED OPTION LIST NODE TITLES WEBFORM ------
//----------------------------------------------------
/**
* Implementation of hook_webform_select_options_info().
* See webform/webform_hooks.php for further information on this hook in the Webform API.
*/
function jobs_webform_select_options_info() {
$items = array();
$items['jobs'] = array(
'title' => t('Jobs'),
'options callback' => '_jobs_options'
);
return $items;
}
/**
* Webform options callback.
*/
function _jobs_options(){
$query = db_select('node', 'n')->extend('PagerDefault')
->condition('type', JOBS_CT_NAME, '=')
->condition('status', (int) TRUE, '=')
->fields('n', array('nid'))
->limit(variable_get('jobs_overview_block_limit', 12))
->orderBy('created', 'desc');
// check if there should be filtered by language
if (module_exists('i18n_node')) {
global $language;
$query->condition('language', $language->language, '=');
}
$results = $query->execute();
if($results){
$nids = array();
foreach ($results as $result) {
$nids[] = $result->nid;
}
$nodes = node_load_multiple($nids);
foreach($nodes as $key => $value){
$options[$key] = $value->title;
}
return $options;
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment