I have multiple addresses in one form. The first province field in the page is updated by ajax call, but the second not.

This is because bug in javascript:

// BUG
// Load province select element onLoad
performProvinceAjax($('.addresses-country-field'));

// FIX
// Load province select element onLoad
$('.addresses-country-field', context).each( function(){
  performProvinceAjax(this);
});

performProvinceAjax() function processes only one element, not array of them. So when you send array of objects to it as argument, only first element in array is processed.

But I suggest bigger rewrite. Please use ".address-processed" class and "content" variable in Drupal behaviors - it can cause conflicts when you need to 1) reapply Drupal behaviors and/or 2) apply them on a particular content of the page.

See my patch against HEAD which is your 1.x-dev as I suggest from the revision graph.

CommentFileSizeAuthor
#9 addresses.patch14.29 KBjaxxed
#6 addresses.js_.gz952 bytesjaxxed
#1 addresses.js_.patch1.02 KBwojtha
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

wojtha’s picture

FileSize
1.02 KB

Don't know why, but attached patch disappeared after submitting the issue. So another try..

ISPTraderChris’s picture

Status: Needs review » Reviewed & tested by the community

I have confirmed this issue -- when multiple addresses fields are present, the province field is transformed to a select box via javascript on the first field during page load, and not subsequent addresses fields. I can also confirm that the above patch fixes this problem.

AlexisWilke’s picture

Status: Reviewed & tested by the community » Fixed

ISPTraderChris,

Thank you. I checked in the patch. Makes sense. 8-)

Alexis

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

jaxxed’s picture

This code doesn't allow for form theming using the drupal theme, if the elements are rearranged at all. The method of finding a country from .parent().siblings().child() is very unreliable.

If we can get a class into the parent address, and assume/allow any theming of the form to keep the class as a parent to any individual address set, then we can run the same operation but from the address parent.
Optimally it would be best to add an address type object with all of the methods, instead of keeping them in the Drupal.behaviour.

I will work on something for this.

jaxxed’s picture

FileSize
952 bytes

Attached is an in progress transition of the address.js, which doesn't rely on .parent().sibling().children().

Note that it requires a parent class of .addresses.form on an individual set of addresses.

@TODO
- finish writing it (it ain't finished)
- address object needs to take the first province and country fields found (in case of whacky implementation)
- exceptions

This in progress but feel free to comment/suggest something.

AlexisWilke’s picture

Title: Country-Province ajax isn't called during page load » Country-Province ajax isn't called during page load and uses relative names to access DOM objects
Status: Closed (fixed) » Needs review

Could I ask you to provide a patch as well? (diff -du org new)

jaxxed’s picture

Alex: I will provide a patch, I am still working on it.

I have a refactored js implementation, and a related modification to the _process hook for the element. I think that this implementation is a bit better for best practices, and normal conventions.

Address.js:

  • changed to use a Drupal js object,
  • added extendable system for adding js processes (php address system allows addition of fields, so the processing should do so as well.)
  • uses Drupal.behaviours after ajax operations on address element (to re-attach any binds on items that were replaced.

hook_process:

  • add js settings for the new object,
  • added two lines of code that add css classes to any fields added to the element (2 lines required as fields are added in two bathes.

I will post a patch by the end of the day.

jaxxed’s picture

FileSize
14.29 KB

Alexis: Here is a patch of my running version.

Note that I had to include a php5.1 compatibility method for the the missing JSON library.

I didn't realize that Drp7 won't get an address module, so I likely overcoded the JS implementation. I wrote it so that it can be extended, which now I realize it likely won't be.

I had to change:

  • addresses.module: modified the elements processor for adding classes to address fields, and make more js settings for the js implementation.
  • addresses.js : full new implementation using a more standardized drupal behaviour and object approach, with an extendible approach to fields and binds, and which reimplements the behaviour on AJAX changes.
  • addresses.settings.inc : modified the province/ajax page handler, to add the appropriate class to the new ajax field. It now also uses theme('select') instead of manually building the field.

Things to do:
- dammit, I forgot to remove the class="text" when recreating the new fields

AlexisWilke’s picture

That looks pretty good already. I guess I'll need to find some time to test and check that in. It's a bit big so I better do that!

Thank you.
Alexis Wilke