Drupal 7, Problemer med Hierarkisk dropdown lists
Hej jeg har nedenstående formular med 2 dropwornlist i. Den anden dropdown skal udfyldes efter vi har valgt en værdi fra den første dropdown. Men den ser ikke ud til at køre koden.---------------------------------------------------------
function find_tryk_filter_form($form, &$form_state) {
// Now the fun begins. For this simple example,
// we will only provide two form elements.
// 1. an input for the friend's email address
// 2. a submit button
// You can add as much or as little to this form as
// needed. See the forms api for all possible elements.
//Query DB for Rows
$results = db_query("SELECT DISTINCT ttd.tid,ttd.name FROM taxonomy_term_data AS ttd JOIN field_data_field_producent AS fp
ON ttd.tid = fp.field_producent_tid JOIN field_data_field_branche ffb ON fp.entity_id = ffb.entity_id JOIN node
ON node.nid = ffb.field_branche_target_id
WHERE node.title = 'Trykluft'");
$options = array();
foreach ($results as $result) {
$options[$result->tid] = $result->name;
}
//Default value for 1st select
$selected = isset($form_state['values']['find_tryk_filter']['level1']) ? $form_state['values']['find_tryk_filter']['level1'] : key($options);
$form['find_tryk_filter']['level1'] = array(
// #type tells drupal what kind of element to build
//'#default_value' => 34,
'#type' => 'select',
'#options' => $options,
'#ajax' => array(
//Call function that rebuilt select2 field
'event' => 'change',
'callback' => 'ajax_dropdown_callback',
'wrapper' => 'second-option',
),
);
$form['find_tryk_filter']['level2'] = array(
// #type tells drupal what kind of element to build
//'#default_value' => 34,
'#type' => 'select',
'#options' => populateSecondDropdown($selected) ,
//populateSecondDropdown($selected),
'#default_value' => isset($form_state['values']['find_tryk_filter']['level2']) ? $form_state['values']['find_tryk_filter']['level2'] : '',
'#prefix' => '<div id="dropdown-second-replace">',
'#suffix' => '</div>',
);
$form['submit'] = array(
'#type' => 'submit',
// #value is the text to display inside the button
'#value' => t('Find filter element'),
);
return $form;
}
function ajax_dropdown_callback($form, &$form_state) {
return $form['find_tryk_filter_level2'];
}
function populateSecondDropdown($key='')
{
$selectedKey = array();
$results = db_query("SELECT ttd.tid,ttd.name FROM taxonomy_term_data AS ttd
JOIN field_data_field_flow AS ff
ON ff.field_flow_tid = ttd.tid
JOIN field_data_field_producent AS fp
ON fp.entity_id = ff.entity_id
JOIN node
ON fp.entity_id = node.nid
WHERE fp.field_producent_tid = '".$key."'");
$options = array();
foreach ($results as $result)
{
$options[$result->tid] = $result->name;
}
$selectedKey[$key] = $options;
if(isset($selectedKey[$key]))
{
return $selectedKey[$key];
}
}
---------------------------------------------