Monday, September 26, 2011

Drupal AHAH Delete button not working properly

1) Click on create content
2) Upload an image
3) and press Delete button

This causes the form to submit, instead of doing the ajax call, and the item is not deleted.
Seems that when you upload an image, the image_upload_js function rebuilds the form, and the ahah of the Delete button does not work.
You can check this because after uploading an image the class of the Delete button isclass="form-submit" instead of class="form-submit ahah-processed".

When the form is fully submitted, then the Delete button class is class="form-submit ahah-processed" and it works.

Maybe the del button ahah content doesn't work if it hasn't been initialized in the $(document).ready because it didn't exists in the .ready event.
Perhaps loading initially the button as "hidden" may be a solution ?

Ans:

Before sending the response to JSON...Add the following lines...
==========
// AHAH is not being nice to us and doesn't know the "other" button (that is,
// either "Upload" or "Delete") yet. Which in turn causes it not to attach
// AHAH behaviours after replacing the element. So we need to tell it first.

// Loop through the JS settings and find the settings needed for our buttons.
$javascript = drupal_add_js(NULL, NULL);
$filefield_ahah_settings = array();
if (isset($javascript['setting'])) {
foreach ($javascript['setting'] as $settings) {
if (isset($settings['ahah'])) {
foreach ($settings['ahah'] as $id => $ahah_settings) {
if (strpos($id, 'ahah-more') || strpos($id, 'ahah-remove')) {
$filefield_ahah_settings[$id] = $ahah_settings;
}
}
}
}
}

// Add the AHAH settings needed for our new buttons.
if (!empty($filefield_ahah_settings)) {
$output_settings = 'jQuery.extend(Drupal.settings.ahah, '. drupal_to_js($filefield_ahah_settings) .');';
}

No comments:

Post a Comment