[development] Help needed to convert a module to D7 with AJAX in forms

Tobias Quathamer t.quathamer at gmx.net
Fri Mar 5 11:19:05 UTC 2010


Hi all,

I've run into a problem with D7 and AJAX-enabled forms. What I try to do
is to have a selection list use options depending on another selection
list. I've looked at the code of the AJAX examples module, but I could
not find why my code is not working. A part of my code follows, the
complete code can be found here (lines 356 onwards):
<http://drupalcode.org/viewvc/drupal/contributions/modules/weather/weather.forms.inc?view=markup&pathrev=DRUPAL-7--1>

I've separated those function calls into their own file, they are not in
the main .module file. Could this be a cause of problems? I would
appreciate any hints or pointers.

Regards,
Tobias




function weather_location_settings_form($form, &$form_state, $display_type, $display_number, $location_id=NULL) {
  $mode = 'edit';
  // Handle the addition of a new location.
  if ($location_id == 'add') {
    $mode = 'add';
    $location_id = NULL;
  }
  // If the location exists, get the settings. If it does not exist,
  // get the default location settings.
  $settings = weather_get_location_settings($location_id);
  $settings->places = weather_get_places($settings->country);
  $form['country'] = array(
    '#type' => 'select',
    '#title' => t('Country'),
    '#description' => t('Select a country to narrow down your search.'),
    '#default_value' => $settings->country,
    '#options' => drupal_map_assoc(weather_get_countries()),
    '#ajax' => array(
      'callback' => 'weather_location_settings_form_callback',
      'wrapper' => 'weather_place_replace',
    ),
  );
  $form['place'] = array(
    '#type' => 'select',
    '#title' => t('Place'),
    '#description' => t('Select a place in that country for the weather display.'),
    '#default_value' => $settings->icao,
    '#options' => $settings->places,
    '#prefix' => '<div id="weather_place_replace">',
    '#suffix' => '</div>',
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
  );
  // Do not show the 'delete' button if not in 'edit' mode.
  if ($mode == 'edit') {
    $form['delete'] = array(
      '#type' => 'submit',
      '#value' => t('Delete'),
      '#submit' => array('weather_location_delete_submit'),
    );
  }
  return $form;
}

function weather_location_settings_form_callback($form, $form_state) {
  return $form['place'];
}


-- 
Tobias Quathamer | Quidquid latine dictum sit, altum viditur.
Hamburg, Germany | (Whatever is said in Latin sounds profound.)

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
Url : http://lists.drupal.org/pipermail/development/attachments/20100305/9d5da256/attachment.bin 


More information about the development mailing list