Our wrapper doesn't contain the field delta and language, so the AJAX replacement will fail as soon as the field is multiple.

Additionally, we need to remove the '_weight' element added by the Field API when rendering the replacement.

Comments

damien tournoud’s picture

Status: Active » Fixed
StatusFileSize
new1.17 KB

Committed the attached patch.

damien tournoud’s picture

Title: AJAX fails for multiple fields » Issues with addressfield multiple fields
Status: Fixed » Active

Actually, we also need to add a "None" country to the subsequent elements, in order to make sure that the user can remove them.

damien tournoud’s picture

Status: Active » Fixed
StatusFileSize
new998 bytes

Committed the attached patch.

Status: Fixed » Closed (fixed)

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

tommychris’s picture

If I select a default country, on every save there will be a new row, IF the user not select the 'None'. So maybe only the first instance has the default value, and the other instances get the 'None' value.

tommychris’s picture

I cahnged this in addressfield.module line 328:

-      '#default_value' => $address['country'],
+      '#default_value' => ($delta == 0) ? $address['country'] : '',
mrbrookman’s picture

Component: Code » Documentation
Status: Closed (fixed) » Needs review
StatusFileSize
new501 bytes

can somebody please commit the patch of ThommyChris in post #6.
It seems a better solution than the current behaviour of the field.

Patch attached

cotto’s picture

StatusFileSize
new435 bytes

Here's an updated version of the patch. I'm not 100% sure if this is the right place to put that kind of code, but it seems to work with 7.x-1.x.

mikeytown2’s picture

Status: Needs review » Needs work

#8 has bad logic

R.J. Steinert’s picture

With HEAD (contains Damien's patch), save a node with two addresses, then try to delete the second address by deleting all of the fields for that address and choosing "none" for the country. The second address will persist as a location with only "United States" as the country (confirmed by checking devel output).

Patch in #7 I don't think was generated from HEAD so applying will fail.

Using #8, second locations country always results in "none" even if a country is chosen.

Debugging now...

mikeytown2’s picture

I've done this in a different module for now. Our field is called "additionallocation" and the node type is "profile"


/**
 * Implements hook_node_presave().
 */
function my_utility_node_presave($node) {
  // Remove empty additional locations before saving.
  if (isset($node->field_profile_additionallocation['und']) && is_array($node->field_profile_additionallocation['und'])) {
    foreach ($node->field_profile_additionallocation['und'] as $delta => $location) {
      // Unset if no state is selected.
      if (empty($location['administrative_area'])) {
        unset($node->field_profile_additionallocation['und'][$delta]);
      }
    }
    // Normalzie the array.
    $node->field_profile_additionallocation['und'] = array_values($node->field_profile_additionallocation['und']);

    // Create default if array is empty.
    if (empty($node->field_profile_additionallocation['und'])) {
      $node->field_profile_additionallocation['und'][0] = array(
        'element_key' => 'node|profile|field_profile_additionallocation|und|0',
        'thoroughfare' => '',
        'premise' => '',
        'locality' => '',
        'administrative_area' => '',
        'postal_code' => '',
        'country' => variable_get('site_default_country', NULL),
      );
    }
  }
}

R.J. Steinert’s picture

I see there has been some major refactoring of addressfield_field_widget_form() since http://drupalcode.org/project/addressfield.git/blob/c9414f98f5b0a4f27f2d.... Looks like we're kind of starting over on this one.

R.J. Steinert’s picture

@Mikeytown I just tested your code on my own installation (modifying the field machine names) and I'm seeing good results. Did you go with that approach because it was quick and easy or because of a particularly difficult barrier to fixing in the addressfield module?

mikeytown2’s picture

Quick and easy. Getting a proper fix shouldn't be that hard to do; putting the logic in hook_node_presave might be the right idea as we need to know about all deltas in this field; removing ones that only have the country set. My logic is quick as I only check if the state (administrative area) is empty.

R.J. Steinert’s picture

putting the logic in hook_node_presave might be the right idea as we need to know about all deltas in this field

I think hook_entity_presave would be better for cases when an addressfield is on something that isn't a node. I'm not sure if there is a generic way to load the field definitions for entities though. We need to check which fields are addressfields so we know which fields on the entity being saved to modify. Perhaps a presave hook is not our best option.

mikeytown2’s picture

Doing a mini code audit here and I have a modified version of #8 applied. I've attached that here for now, not sure if it is needed or good/bad.

fearlsgroove’s picture

Status: Needs work » Needs review
StatusFileSize
new2.14 KB

Here's a generified version #11, applied to hook_entity_presave in addressfield. Not extensively tested, but it seems to work fine on a content type with both one addressfield with cardinality 1 and an unlimited addressfield.

mikeytown2’s picture

StatusFileSize
new2.23 KB

New patch based off of #16 & #17.

Note that this no longer creates a blank entry.

johnv’s picture

I am not sure what the subject of this issue is ATM.
OP is fixed in #3.

Do patches resolve "An address is generated, even if user does not enter an address" ?
If so, we might better continue in #1844846: Make Addressfield optional, (even if country is required) .
That contains similar patches, but using hook_field_presave() instead of hook_entity_presave() , and has a better description.

hook_field_presave is more performant, and you can check against the default value in the field instance settings, instead of checking only the empty values.

johnv’s picture

Status: Needs review » Fixed

I'm pretty sure that #1844846: Make Addressfield optional, (even if country is required) is the same.
Resetting status to fixed, as per #3, fixing the original issue. Please reset the status if you think it is not.

Status: Fixed » Closed (fixed)

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