What I have is a Drupal form, written in PHP of course which has two selectors. The first one has a list of Drupal themes and the second is a list of available regions. In Drupal, certain themes have certain regions which are available to put blocks into so when a theme is chosen, I want the region selector to be populated by the available options associated to the selected theme.
I want my javascript file to use jQuery to listen to the theme selector so that on change, it will update the region selector.
I have the module PHP set up so that there is a 2D array of values such that array['theme'] = array() of possible region values. Like this:
Array
(
[garland] => Array
(
[left] => left
[] =>
[right] => right
)
[zen] => Array
(
[] =>
[sidebar_first] => sidebar_first
[sidebar_second] => sidebar_second
)
)
That is done by this code:
$themeQuery = db_query('SELECT DISTINCT theme from {blocks}');
while($row = db_fetch_object($themeQuery)){
$regionQuery = db_query('SELECT DISTINCT region from {blocks} WHERE theme="%s"',$row->theme);
while($regions = db_fetch_object($regionQuery)){
$regionOptions[$row->theme][$regions->region] = $regions->region;
}
$themeOptions[$row->theme] = $row->theme;
}
And here is the form:
$form = array();
$form['title'] = array(
'#type' => 'markup',
'#value' => '<br/><h4>Update block: <strong>'.$module.': '.$delta.'</strong></h4>'
);
$form['theme'] = array(
'#title' => 'Theme',
'#type' => 'select',
'#options' => drupal_map_assoc($themeOptions),
'#default_value' => $currentTheme,
);
$form['region'] = array(
'#title' => 'Region',
'#type' => 'select',
'#options' => array(''),
'#default_value' => $currentRegion,
);
All I really need to do is to get the 2D array $regionOptions into the javascript! I tried drupal_to_js and drupal_add_js but it wouldn't work!
The javascript is this so far:
$(document).ready(function(){
$('#edit-theme').change(function(){
alert(Drupal.settings.regions[$(this).val()]);
});
});
Here is my attempt at drupal_add_js (in the PHP code):
drupal_add_js(array('regions' => $regionOptions),'setting');
This function works if I try to do:
drupal_add_js(array('regions' => 'test'),'setting');
And then call in the javascript:
alert(Drupal.settings.regions);
It does alert 'test'.
I hope I asked this right, thank you :)