First, thanks for your code, which is without doubt a great saving in terms of effort and time, and sorry in advance for my bad English.
I found that the code of ahah_helper_render is unable to handle the case of changes to the ahah properties (i.e. the event) on the same field.
Take the case where we want a field is represented with an autocomplete textbox when the value is not in a certain range, or with a selectbox when the interval is defined.
In the first case we will use a textbox autocomplete and an event type blur. In the second, a combobox with an event change.
Please note that the field that is changed has always the same name and id. In the sample code below has been the case described:
if ($my_range_value != '') {
$options =get_options_for_my_range($my_range_value);
$form['my_wrapper']['myrange'] = array(
'#type' => 'select',
'#title' => t('The Range'),
'#default_value' => $my_range_value,
'#options' => $options,
'#required' => TRUE,
'#ahah' => array(
'event'=>'change',
'wrapper' => 'my-wrapper',
'path'=>ahah_helper_path(array('my_wrapper')),
'method' => 'replace',
),
);
} else {
$form['my_wrapper']['myrange'] = array(
'#type' => 'textfield',
'#title' => t('The Range'),
'#default_value' => $my_range_value,
'#autocomplete_path' => 'my_range/autocomplete',
'#required'=>TRUE,
'#ahah' => array(
'event'=>'blur',
'wrapper' => 'my-wrapper',
'path'=>ahah_helper_path(array('my_wrapper')),
'method' => 'replace',
),
);
}
When the textfield with an ahah event of type "blur" is dynamically replaced in the form with a selectbox with an ahah event of type "change", Drupal.settings is not updated with data from the new element: infact, the code in the function ahah_helper_render generate a json vector with settings data using the previous layout of the form:
// Get the JS settings so we can merge them.
$javascript = drupal_add_js(NULL, NULL, 'header');
$settings = call_user_func_array('array_merge_recursive', $javascript['setting']);
drupal_json(array(
'status' => TRUE,
'data' => theme('status_messages') . drupal_render($form_item),
'settings' => array('ahah' => $settings['ahah']),
));
The first solution I've found to solve this problem was to slightly modify the function ahah_helper_render, adding the code that was able to read the new field structure with ahah attribute and eventually to merge the changes to the settings array.
I know that this is a partial solution, because it doesn't care of nested form elements. Improvements are welcome!
Check the attached patch file.
| Comment | File | Size | Author |
|---|---|---|---|
| #1 | ahah_change.patch | 2.09 KB | d4rkstar |
| ahah_change.patch | 2.01 KB | d4rkstar |
Comments
Comment #1
d4rkstar commentedPlease, use this version of the patch. I didn't see the bug filed at http://drupal.org/node/480472.