Ajax con combobox dependientes Drupal 7
http://atur-lalai.blogspot.com/2011/10/drupal-7-ajax-dependent-drop-down.html
function filtro_buenaspracticas_form($form, &$form_state, $month, $year, $area)
{
$start_date = format_date(mktime(0,0,0,$month,1,$year),'custom', 'Y-m-d');
//get the list of locations from taxonomy to use in the dropdown
$dropdown_source = taxonomy_get_tree(141, $parent = 0, $max_depth = 1, $load_entities = FALSE);
//print_r($dropdown_source);
$dropdown_array = array('0' => '--none--');
foreach ($dropdown_source as $item) {
$key = $item->tid;
if ($item->depth == 0){
$value = $item->name;
}else{
$value = '--'.$item->name;
}
$dropdown_array[$key] = $value;
}
$info = node_load(775);
//print_r($info);
$form = array();
$form['conte'] = array(
'#prefix' => '<div class="contenido-pedeterminado">'. $info->body['und'][0]['value'].'',
'#suffix' => '</div>',
);
$form['area'] = array(
'#key_type' => 'associative',
'#type' => 'select',
'#options' => $dropdown_array,
'#title' => 'Empresa',
'#default_value' => $area,
'#ajax' => array(
'callback' => 'ajax_dropdown_callback',
'wrapper' => 'second-option',),
);
$selected = isset($form_state['values']['area']) ? $form_state['values']['area'] : key($dropdown_array);
$form['select2'] = array(
'#options' => _select2_dropdown_options($selected),
'#default_value' => isset($form_state['values']['select2']) ? $form_state['values']['select2'] : '',
'#prefix' => '<div id="second-option">',
'#suffix' => '</div>',
'#title' => 'Area',
'#type' => 'select',
);
$form['month'] = array(
'#type' => 'date_select',
'#date_format' => 'F',
'#date_type' => DATE_DATETIME,
//'#date_year_range' => '-0:+1',
'#default_value' => $start_date,
);
$form['year'] = array(
'#type' => 'date_select',
'#date_format' => 'Y',
'#date_type' => DATE_DATETIME,
'#date_year_range' => '-5:-0',
'#default_value' => $start_date,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Filter'),
'#submit' => array('datos_buenaspracticas_submit'),
);
$form['reset'] = array(
'#type' => 'submit',
'#value' => t('Reset'),
'#submit' => array('reset_datos_buenaspracticas_submit'),
);
return $form;
}
//Funciones
function _select2_dropdown_options($key=Null) {
$tid = $key;
$a = taxonomy_get_children($tid);
//print_r($a);
$dropdown_array_2 = array('0' => '--none--');
//$dropdown_array_2 = array("2"=>'demo 2');
foreach ($a as $item) {
$key = $item->tid;
$value = $item->name;
/* if ($item->depth == 0){
$value = $item->name;
}else{
$value = '--'.$item->name;
}*/
$dropdown_array_2[$key] = $value;
}
return $dropdown_array_2;
}
function ajax_dropdown_callback($form, &$form_state) {
return $form['select2'];
}
function filtro_buenaspracticas_form($form, &$form_state, $month, $year, $area)
{
$start_date = format_date(mktime(0,0,0,$month,1,$year),'custom', 'Y-m-d');
//get the list of locations from taxonomy to use in the dropdown
$dropdown_source = taxonomy_get_tree(141, $parent = 0, $max_depth = 1, $load_entities = FALSE);
//print_r($dropdown_source);
$dropdown_array = array('0' => '--none--');
foreach ($dropdown_source as $item) {
$key = $item->tid;
if ($item->depth == 0){
$value = $item->name;
}else{
$value = '--'.$item->name;
}
$dropdown_array[$key] = $value;
}
$info = node_load(775);
//print_r($info);
$form = array();
$form['conte'] = array(
'#prefix' => '<div class="contenido-pedeterminado">'. $info->body['und'][0]['value'].'',
'#suffix' => '</div>',
);
$form['area'] = array(
'#key_type' => 'associative',
'#type' => 'select',
'#options' => $dropdown_array,
'#title' => 'Empresa',
'#default_value' => $area,
'#ajax' => array(
'callback' => 'ajax_dropdown_callback',
'wrapper' => 'second-option',),
);
$selected = isset($form_state['values']['area']) ? $form_state['values']['area'] : key($dropdown_array);
$form['select2'] = array(
'#options' => _select2_dropdown_options($selected),
'#default_value' => isset($form_state['values']['select2']) ? $form_state['values']['select2'] : '',
'#prefix' => '<div id="second-option">',
'#suffix' => '</div>',
'#title' => 'Area',
'#type' => 'select',
);
$form['month'] = array(
'#type' => 'date_select',
'#date_format' => 'F',
'#date_type' => DATE_DATETIME,
//'#date_year_range' => '-0:+1',
'#default_value' => $start_date,
);
$form['year'] = array(
'#type' => 'date_select',
'#date_format' => 'Y',
'#date_type' => DATE_DATETIME,
'#date_year_range' => '-5:-0',
'#default_value' => $start_date,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Filter'),
'#submit' => array('datos_buenaspracticas_submit'),
);
$form['reset'] = array(
'#type' => 'submit',
'#value' => t('Reset'),
'#submit' => array('reset_datos_buenaspracticas_submit'),
);
return $form;
}
//Funciones
function _select2_dropdown_options($key=Null) {
$tid = $key;
$a = taxonomy_get_children($tid);
//print_r($a);
$dropdown_array_2 = array('0' => '--none--');
//$dropdown_array_2 = array("2"=>'demo 2');
foreach ($a as $item) {
$key = $item->tid;
$value = $item->name;
/* if ($item->depth == 0){
$value = $item->name;
}else{
$value = '--'.$item->name;
}*/
$dropdown_array_2[$key] = $value;
}
return $dropdown_array_2;
}
function ajax_dropdown_callback($form, &$form_state) {
return $form['select2'];
}

Comentarios
Publicar un comentario