Drupal dynamic selection on the form

I'm sure this is easy for jQuery experts, but I am a backend and cannot find a better way to do this. I have two arrays in Drupal, one of them is a list of view names, and the other is an array containing a list of displays for each view. Here's the code to populate two arrays:

 //load list of views in to array for select lists
$views = views_get_all_views();
$viewnames = array();
$viewdisplays = array();
foreach ($views as $view) {
  $viewnames[$view->name] = $view->name; 
  foreach ($view->display as $k) {
    $id = $k->id;
    $title = $k->display_title;
    $viewdisplays[$view->name]['id'] = $id;
    $viewdisplays[$view->name]['title'] = $title;
  }
}

Here is a snippet of the form I'm working with:

$form['view'] = array(
  '#type' => 'select',
  '#title' => t('Select the view to be used for display'),
  '#options' => $viewnames,
);
$form['view_display'] = array(
  '#type' => 'select',
  '#title' => t('Select the display of the gallery view to be used'),
  '#options' => array(),
);

What I want to do is dynamically populate the view_display select box with the appropriate values. If the user selected "My favorite view" from the "view", I want to display the $ viewdisplays ['My Favorite View'] array as the #options field in the view_display field.

+5
source share
2

, .

  • Drupal , , . , , , , . , , :

    array(
      1 => 'a-a',
      2 => 'a-b',
      3 => 'a-c',
      4 => 'a-d',
      1 => 'b-a',
      2 => 'b-b',
    )
    
  • , ajax-, js. . :

    $("#edit-view").change(function(){
        $("#edit-view_display").html(Drupal.settings.myVariable[$(this).val()]);
    }).change();
    

    js . js, $(document).ready drupal_add_js

+4

? CCK.

( ):

... "hierarchical_select", , .

. , . , , , Dropbox, . !

+1

All Articles