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

Randy Fay randy at randyfay.com
Fri Mar 5 16:02:59 UTC 2010


I do confirm that http://drupal.org/node/684846 works with your submit
function as is, and it would be nice if you'd chime in on that issue and say
so.

However, this simple fix is probably a better use of the Form API:

   if ($location->display_type == 'user') {
-    drupal_goto('user/' . $location->display_number . '/weather');
+    $form_state['redirect'] = 'user/' . $location->display_number .
'/weather';
   }
   else {
-    drupal_goto('admin/config/user-interface/weather');
+    $form_state['redirect'] = 'admin/config/user-interface/weather';

-Randy

On Fri, Mar 5, 2010 at 8:36 AM, Ken Winters <kwinters at coalmarch.com> wrote:

> Note that the linked issue is marked critical (so it will be fixed before
> D7 is released) and already has a patch.  Rather than change your form,
> first try applying the patch and see if that fixes it for you.
>
> - Ken Winters
>
> On Mar 5, 2010, at 10:33 AM, Randy Fay wrote:
>
> Hi Tobias -
>
> The problem is the drupal_goto() in your submit function. The submit
> function is being processed during the AJAX callback... and the
> drupal_goto() takes control away from the callback.
>
> If I'm not mistaken, this behavior will change when
> http://drupal.org/node/684846 lands, but for now, you'll need to not do a
> drupal_goto() in your submit function.
>
> -Randy
>
> On Fri, Mar 5, 2010 at 4:19 AM, Tobias Quathamer <t.quathamer at gmx.net>wrote:
>
>> 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.)
>>
>>
>
>
> --
> Randy Fay
> Drupal Development, troubleshooting, and debugging
> randy at randyfay.com
> +1  970.462.7450
>
>
>


-- 
Randy Fay
Drupal Development, troubleshooting, and debugging
randy at randyfay.com
+1  970.462.7450
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20100305/1801813d/attachment-0001.html 


More information about the development mailing list