At the moment, even if the user does not want to specify a country, one (by default Afganisthan) will be chosen automatically. How about adding a <none> value to the drop down list of countries?

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rszrama’s picture

Hmm, the problem is that the elements on the form and the display of the field are address specific. If the user doesn't select a country, one has to be assumed - it should default to the site's default country perhaps. I'll have to revisit our available country settings - if only one country is available, then I suppose the country element could be hidden.

rszrama’s picture

Status: Active » Postponed (maintainer needs more info)

Just to follow-up here, I did see that we're defaulting to the site's country if no default value has been set on the widget edit form yet. Also, we are relying on the existence of a country for the field, so not selecting one is not an option. We could still consider whether or not there should be a default "none" value that requires customers to select a country, but I'm not really sure how helpful that would be or what it would then look like for the default form presentation...

scor’s picture

Core provides the option - None - in the "Default country" list.

jcarlson34’s picture

Status: Postponed (maintainer needs more info) » Active

I see that a none value has been added for additional addresses in #1031422: Issues with addressfield multiple fields

@rszrama could using the 'drupal_process_states' javascript in D7 work as a solution for this issue? Click here to check it out. Also see the Examples module for a working demonstration of #states.

Perhaps by forcing the user to choose a country first would then trigger the correct fields and form. That would (I think) allow for a none value for a starting value and make the form more dynamic at the same time.

robertDouglass’s picture

+1 for having a <none> option right off the bat. This detail makes the address field a required field by default, which is completely counter to many use cases, where addresses are optional.

BenK’s picture

Subscribing

mlncn’s picture

Issue tags: +bounty

Marked #1206156: Can we set the country drop down list default to "-NONE-"? as duplicate.

The ideal user experience would be for admins to be able to say that the address field is optional, but if it is used, the country is required. In this, the most common use case, the country could be filled in with a sensible default, but the module could know to throw that data out if nothing else is filled out.

Agaric pledges $200 towards that solution, but i'll buy a beverage of choice for anyone who gets in a solution that doesn't require me to hack this module!

benjamin, agaric

mlncn’s picture

Sam Kottler and i will be proposing a patch that gives it an empty string key, ' -- None -- ' value option for country and explaining this new concept to addressfield in its hook_field_is_empty().

With "is empty" it should not render at all. Come to think of it i could tell the field it "is empty" if it has only a country, and i and 99% of the people using this would be happy (and happily ignorant of the meaningless default countries scattered through the field's data table).

mlncn’s picture

Status: Active » Needs review
FileSize
316 bytes

This seems to be the sum total of the patch needed-- simply adds a '' => '- None -' option to all country selectors.

mlncn’s picture

Improved patch putting 'No address' as option at top of form and removing other address fields.

mlncn’s picture

Bump. I don't consider this the ultimate solution, but having a default country makes . Hence we are running with this patch. What is needed for a solution to make it in?

Related: #1263316: Configurable non-empty value conditions

socceronly’s picture

I would just like it to be Country and State/Province.

I would actually like all the rest to be optional.

This would be really useful for a lot of people that don't need full addresses, but just want to classify things based on country and/or provice/state.

rodych’s picture

Subscribing

nagiek’s picture

+1

nagiek’s picture

Also, the patch in #10 leaves me with an error message.

Notice: Undefined index: #handlers in addressfield_process_format_form() (line 145 of sites/all/modules/addressfield/addressfield.module).

nagiek’s picture

Component: User interface » Code
Status: Needs review » Needs work

Also, default value of "None" not accepted on the settings form.

Jax’s picture

Subscribe

wesnick’s picture

I have been able to add an addressfield without a country by writing my own plugin. It takes the site default country as the addressfield default country, then in the format callback I added in:

  $format['country'] = array(
    '#widget_type' => 'hidden',
    '#value' => $address['country']
  );
Les Lim’s picture

Per rszrama's suggestion in #1, I still think hiding the country selector if only one option is available is useful, so I opened that as a separate issue: #1316788: Do not display country in address block if only one country option is available. Patch attached there.

Les Lim’s picture

EDIT: double post. Guru, stop meditating.

Damien Tournoud’s picture

Ryan identified the main issue with this all the way back in #1:

Hmm, the problem is that the elements on the form and the display of the field are address specific. If the user doesn't select a country, one has to be assumed - it should default to the site's default country perhaps.

So, if we make the country optional, if None is selected we should not output any other field, as those are country-specific to begin with.

naught101’s picture

why not just make a generic "any country" country, that displays all elements, in the order that is most common among all countries? Then just display that data in the same order. That wouldn't need any more than an extra country defined with a special code (XX orsomething)...

wamilton’s picture

If anyone just needs a form with address fields and have country defaulted to 'US' and hidden, you can just do this instead of writing a plugin:

  $address = array(
    //supply default
    'country' => 'US',
  );                                                 
  $form['location'] = addressfield_generate($address, array('address'), array('mode'=>'form'));
  //need to do this because of the render functions attached by the above function
  $form['location']['country']['#widget_type'] = 'hidden';¬ 

actually reading the addressfield_generate function, comments, and related functions was pretty clutch here. ;)

socceronly’s picture

I see now the Location Taxonomize module is for this. Thought I can't get it to work with Address Field...

Damien Tournoud’s picture

Ok, so what about outputting the address widget in the format of the default country, when - None - is selected?

naught101’s picture

Hrm.. it might be better to have a "none" option separate to a "country undefined" option, as this would allow quick removal of an address that's already been entered, and would also prevent all the address sub-fields appearing by default.

Are there any questions about the generic order of address components? Most (all?) countries seem to use a specific -> generic order, so something like:

[name_line] OR [first_name] [last_name]
[organisation_name]
[premise] [thoroughfare]
[dependent_locality], [locality] 
[sub_administrative_area], [administrative_area] [postal_code]

Maybe?

phen’s picture

@#25,
Sounds reasonable to me.

rooby’s picture

+1 for 25. That's what I was thinking exactly.

Currently when you use this field inside a field collection, you end up with an extra row in the field collection every time you save because of the non-empty country field.

adam_b’s picture

+1 for 25, and I'm also having the same problem as described in 28.

bofrost’s picture

#28 same problem here. We need a solution for this.

nirbhasa’s picture

I tried to use #23 to display a default country, but still have the widget shown so people could change it, but that didn't work out. Instead what worked was to use a form_alter on the widget element itself:


/* Implementation of hook_widget_addressfield_standard_form_alter */
function mymodule_field_widget_addressfield_standard_form_alter(&$element, &$form_state, $context) {

      $address = array(
        'country' => 'US', 
      );
      $element['#address'] = array_merge($element['#address'], $address);

} 
mototribe’s picture

I needed to be able to import new users into my site (using migrate) with the country "none" if the countrycode in my import DB was set to NULL.
The site default country was set to "united states".
After applying the patch a new user would get created with country "united states" even if they select "none" during signup.

Only after changing the site default country to none could I import users with "none" as the country.
Line 322 of addressfield.module does that

  // Use the default country of the site if possible.
  $default_country = variable_get('site_default_country', NULL);

I would think this should get removed since you can set a default country for the address field itself.

miahon’s picture

Title: Make the country optional » Make the country optional, None value?

I use that module with geofield/geocoder module, we
are doing local event calendar. When we have event with address
it's working fine.

But sometimes we have also events without addresses.
Then we get wrong default value in our map (because of country field).

It is possible to hide that somehow or some other way to solve problem?
Is there NONE value to country field?

Or is it geofield issue or should I use Location module?

nagiek’s picture

Maybe what we need, instead of a "-None-" country value, is a checkbox which shows/hides the field. Kind of like how in the Date module, "All Day" shows/hides the time value.

klonos’s picture

I think that allowing <none> for country will eventually cause similar issues that we are now facing with "Language neutral" - every text we write is in a certain language no matter if we store this information or not (this very text I'm writing right now is in English - unless we all speak this non-existing language called "Neutral").

So, if you really think it through, you'll realize that each physical address has a country (at least it implies one) no matter if we store this information in our db or not. So we should rather:

- allow admins to set a default country for address fields
- offer the option to lock this default so it can't be changed (per field instance or content type perhaps)
- hide the field from the edit form
- *optionally* hide the address field from content display

So, the equivalent for the "Multilingual settings" tab:

Extended multilingual options provided by Internationalization module.

Extended language options
[v] Set current language as default for new content.
[v] Require language (Do not allow Language Neutral).
[v] Lock language (Cannot be changed).

Extended language support
(*) Normal - All enabled languages will be allowed.
( ) Extended - All defined languages will be allowed.
( ) Extended, but not displayed - All defined languages will be allowed for input, but not displayed in links.

...becomes our new "Address settings" tab:

Extended country options provided by the Address Field module.

Extended country options
[v] Set the site's default language as default for new content.
[v] Require country (Do not allow empty country).
[v] Lock country (Cannot be changed).

Extended country support
(*) Normal - All allowed countries will be allowed.
( ) Extended - All countries will be allowed.

(*) Hide country field from input only
( ) Hide country field from input and content display

Well, I haven't thought all of these options thoroughly, but you get the point ;)

rooby’s picture

^^^^ What he said.
That's the idea solution I think.

If you really want an address without a country just set a deafult and hide the field then hide the country value everywhere on the site.

But if you are dealing with addresses that don't have a country at all this module probably isn't for you. Just use text fields.

klonos’s picture

...hide the country value everywhere on the site.

As I said, this one should be *optional* for display. There are valid use cases when the country field might be required to be pre-filled with a default value, then hidden/locked during input but still desired to be visible in display.

adam_b’s picture

Sorry, I don't think the language analogy works.

I'm presently compiling a database which will include addresses. I don't know all of the addresses (not even the countries) at this point, and I may never know some of them. At the moment all I can do is specify "Afghanistan" as the default (chosen because it's first in the list and pretty unlikely to be chosen in real life). Why should I have to do this? A simple "-None-" or "-Select-" option, with the address widget displaying no fields, seems by far the simplest solution to me.

rooby’s picture

@adam_b:
I don't like your chances of getting a none value in.
The module is very specifically for standards based postal addresses, which must have a country.
You would likely have to implement your own address format plugin.

For a similar request see the maintainers response to #1048734: Required setting on individual fields

Zoltán Balogh’s picture

And what about, when the address field is optional, and completely not required? The user can not leave it empty, because the Country field is contains a data, and nobody can clear. So, if you leave all of other fields empty, then on the node view page the label of the field is displayed with only the name of the country. This behavior is not a good idea, because I not set this value, I posted a full empty address without any changes. In this case, the data is writed into the related table.

The default "- None -" option is a good idea, IMHO. If the user fill any of the other fields, then the country selection is required. But if all of other fields are empty, then the Country also be empty.

rooby’s picture

Yeah sorry, I got a bit mixed up.

The default "- None -" option is a good idea, IMHO. If the user fill any of the other fields, then the country selection is required. But if all of other fields are empty, then the Country also be empty.

- I agree with this.

miahon’s picture

Yes, good writing Zoltán!

I liked also those ideas:

" is a checkbox which shows/hides the field"
" *optionally* hide the address field from content display"

Is it Conditional Fields module solution for that?

adam_b’s picture

Thanks for clarifying this Zoltán - yes, I was referring to situations where the address field is optional.

I agree that as soon as you enter any data in the field you need to have a country, and Klonos's language analogy does work in that case. I'm really not worried about that part - but I'm irritated at having an optional field which doesn't allow a null value.

So perhaps there are two discussions going on here? Should this thread be split?

Zoltán Balogh’s picture

go to end of the addressfield\plugins\format\address.inc
comment out the last if, like this:

    //if (isset($context['delta']) && $context['delta'] > 0) {
      // On subsequent elements of a field, we make the country field non
      // required and add a ' - None - ' option to it, so as to allow the
      // user to remove the address by clearing the country field.
      $format['country']['#required'] = FALSE;
      $format['country']['#empty_value'] = '';
    //}

It was just a quick workaround. It is works without any notices or errors. - None - option added, and you can submit empty adrress. If you edit the node again, then the default value is set again, so it is just a temporary, half-solution.

I will working on this today (set the default value to empty too, test, etc), and I hope, I will provide something today, as patch.

Zoltán Balogh’s picture

Title: Make the country optional, None value? » Make an addressfield to optional
Assigned: Unassigned » Zoltán Balogh
Category: feature » bug
Priority: Normal » Major

It is not a feature request, that is a major bug, when a field can not empty. But, it was very easy, we just have to forget using the site_default_country system variable. I have to go now for my child, but a working version already exists on my sandbox. I hope, we can set this issue to RTBC today.

Zoltán Balogh’s picture

FileSize
2.71 KB

The initial version is attached, please test it. All feedbacks are welcome.

But please, do not set this issue to RTBC, because we need a new validation. If the user filled any field, but the Country field has left blank (or changed to empty), then that address will not be saved (or that will be removed in edit phrase). This is a simple data loss. I will working on this validation tomorrow.

klonos’s picture

Title: Make an addressfield to optional » Add a special -none- country entry in the "Available countries" list in order to make the address field really optional.
Category: bug » feature

I see the problem here and it is specific to use cases where the address filed needs to be optional:

...if no data is entered, there should be nothing saved in the db and no address shown in display. *But*, we don't have a way to leave the country field empty no matter what. That's why in the revised issue title I state that the address field is not *really* optional.

I believe that the proposed solution to add a -none- option in the drop-down widget, so it can be set as default for such cases is the wrong approach. It breaks use cases where we need to use the "Hide the country when only one is available" option (there will always be at least two countries/entries: -none- and whichever we choose as the default).

What we should do instead of adding an extra option in the drop-down widget is to add a -none- country entry to the list of available countries. This way:

- People that want to set a default country (required address field) and hide the widget can simply keep doing things the way we do it now.

- People that want to have the address field *truly* optional so that users can leave it "empty" can select their desired country(ies) + this "special-purpose" -none- country. They can then set this as their default. Users that leave the address fields untouched will not save anything in the db unless they select a specific country (other than -none-) from the drop-down widget.

Does this satisfy both sides?

klonos’s picture

...this of course solves the storage and UI part of the issue. There is also the requirement for both use cases to be able to hide the field from display.

When the country field is set to a default and the address is required, the "Hide the country when only one is available" setting only hides the field from the edit form. We need to either add a second option that allows us to also hide it from display, or change the behavior of the existing setting to do that too for us. (I personally like things to be as optional as possible, so I like the first solution).

When the address field is not required, people need a way to *conditionally* hide the field from display if it is empty (hide its label actually). The pin-on-the-map-even-if-no-address-is-set issue will be solved (I believe/hope so) as a "pleasant side-effect/bonus" if we manage to not save anything in the db the way I proposed in my previous comment.

So, in other words we need a way to:

a) hide the country (sub)field from display if the country is set to a default (in a required setup).
b) hide the whole address_field label if no data is entered (in an optional setup).

Does this sum the need to hide feature for both sides too?

PS: ...this feature request deserves its own separate issue anyways.

rooby’s picture

I would still argue this is a bug, because non-required fields are saving values when they are left untouched.
Not that it matters either way really.

klonos’s picture

Category: feature » task

If the issue title was something like:

"Setting the Postal address field as optional still saves a value in the db because there is no way to have an empty country"

...then sure it could be classified as a bug. I don't really mind, but since we've figured a/the solution, setting that as the issue's title makes it feel more like a feature request. Would you settle with "task" then?? ;)

Zoltán Balogh’s picture

@klonos, We know, the general behavior of the fields: If a field is not required, and the user not set any value, then it is not storing and not displaying any data. Could you make this general Field API behavior with the Addressfield? NO, Addressfield always storing and displaying something without any user assistance. This is a major *bug*. No other way to solving this problem, only adding an empty option, and forget to using the site's default language. If a field is not required, then it's default value *must be* empty. And since all fields can be optional, no other way. It is *not* an Addressfield specific behavior, it is the main concept of the Field API.

Everything else is may be feature request, or task or anything else independent from the title and any other properties. But, until I can not save node with an optional, leaved blank field (any type), because that is saving and displaying data, then that is a *major bug*.

adam_b’s picture

Can I agree with Klonos here and repeat my suggestion: this is actually two separate issues, and this thread should be split.

Zoltán Balogh’s picture

Assigned: Zoltán Balogh » Unassigned

Ok, I submit a new issue.

Zoltán Balogh’s picture

The separated issue for the really empty address field: #1443674: A not required and empty Addressfield let really empty

rooby’s picture

The title of this issue and the title of #1443674: A not required and empty Addressfield let really empty mean the same thing am I right?

klonos’s picture

...actually, if you take a look at this issue's summary you'll see that from the beginning it was about introducing a <none> country entry. So if it was to split this issue, then it's only fair that we should be "leaving" this and branching off someplace else. We do not need to file a separate issue for the feature of hiding the country from display if only one country is available though, because there is one filed already: #1316788: Do not display country in address block if only one country option is available

No need to file a new issue for solving the <none> country entry either. It should be solved here. I kindly asked Zoltán to "return" and continue his work here. I made it clear that nobody would object in whatever solution he comes up with as long as he makes sure that it doesn't break the current functionality (allowing us to require the address + define the default country + hide/lock it from the edit form).

Zoltán Balogh’s picture

Assigned: Unassigned » Zoltán Balogh

Well, in this case we would require to add a new checkbox to the field edit form. If:

  • The format is Address form (country-specific)
  • and the field is not required
  • and the new checkbox is checked

then we will add the - None - as new option on the entity edit form, and always set this as the default value os the Addressfield. A new verifcation also required in this case, because if the user has left blank the Country field, but enter any data into other fields, then we will lost that datas in the current state. In this case we must prevent the submission with "The country is required if you entered any data to !label field." error message.

What do you think about this solution?

klonos’s picture

That does sound like a proper solution indeed. How about "Make country optional" for that checkbox's label?

As for the check to avoid data loss: yes, there should be a check to see if data has been entered in *any* of the address (sub)fields. As a matter of fact, that check should be in place anyways and not specifically for this issue here. If any of the fields contain any value on form save (besides <none> for the country), then the whole address_field should be considered non-empty and saved in the db.

naught101’s picture

I'd just like to point out out that even taking into account the removal of empty field values, there is still a potential use of address field that doesn't require a "country" field: named places not owned by any country (eg. https://en.wikipedia.org/wiki/Bir_Tawil), and (potentially) disputed territories.

I guess there's a fair chance that places like Bir Tawil don't have an operating postal service, so Address field shouldn't take any account of them. I guess something like this is actually more suited to a module that allows the input of general locations, and isn't intended solely for postal addresses (then you could also include places like the Marianas Trench, that are named, but are in international waters). But I don't know if any of the other location modules would allow that either...

Zoltán Balogh’s picture

Status: Needs work » Needs review
FileSize
786 bytes
2.5 KB

Then let's dance, first try is here. I attached a patch for the addressfield.module and a new plugin for Make country optional (zipped, because .inc can not uploaded). Please test in your environment. Feedbacks are welcome.

adam_b’s picture

#60 works for me, but I'm not sure about the logic.

In order to make the default country "none" and thereby make the field truly optional, you need to:
- tick the "country optional" checkbox
- save the field
- re-open the field
- select "- None -" as the default country
- save the field again
This is really convoluted, and certainly doesn't follow the usual Drupal processes.

Could I suggest this:
- there is always a "- None -" option which can be selected as the default country in the field config
- if the "country optional" checkbox *is* ticked, then the fields appear as at present, and you can save addresses with "- None -" as the country
- if the "country optional" checkbox *is not* ticked, then the other field elements don't appear unless a country is selected

Does this make sense?

Zoltán Balogh’s picture

@adam_b if you check the new chekcbox, then addressfield_optional_country_validate() function in the new plugin, clears the default country, or not? It is the only task for that function.

adam_b’s picture

Hi Zoltán - no, it doesn't clear the default country for me. I have to save the field config and then re-open it to see the "None" entry.

Zoltán Balogh’s picture

Status: Needs review » Needs work

Here, too.

Zoltán Balogh’s picture

Thanks @adam_b for feedback. The addressfield_optional_country_validate function is called too late, just second time. I just need an other point to clear the default, if the Make country optional is checked.

Always adding the - None - option is not a good idea IMHO, because - I think -, that is breaks the current functionality.

adam_b’s picture

I'm not sure it breaks the current functionality, because anybody who's using the module at present must have selected a default country, right? The option would only appear if people went in and changed the configuration.

However, as long as changing the values doesn't take too many steps then I can live with it this way.

Zoltán Balogh’s picture

Then please insert one line at the end of your addressfield.module:

  $countries[''] = t('- None -');
  return $countries;
}

If with this modification it is works well, then I post a new patch.

Zoltán Balogh’s picture

@klonos said above, there is an existing "Hide the country when only one is available" option. I think, if we always add a new item to countries list, then that will breaks the existing functionality, because we always add a new "country".

adam_b’s picture

Sorry, #67 doesn't seem to make any difference for me when setting up the field.
However, the display now always shows the country, even if the "country optional" checkbox is ticked.

Zoltán Balogh’s picture

There is a new solution, it is works for me at first sight. I just test some cases, and make a new patch and a new plugin soon.

Zoltán Balogh’s picture

Status: Needs work » Needs review
FileSize
532 bytes
3.66 KB

Second try.

adam_b’s picture

Works for me, thanks. When you tick "Make country optional", the "None" option doesn't appear in the default list - but if you save the config and open it again, it is there and selected. So it's not totally obvious, but it does work. Anyone else have an opinion?

Zoltán Balogh’s picture

Yes, yes we need more feedback. A lot of websites using this module.

klonos’s picture

Once you have a well-tested patch that works for your case for enough people, then please ping me (or simply set the issue to RTBC) and I'll check if it doesn't break my sites and the current functionality. Then you'll really have a broadly accepted solution ;)

Zoltán Balogh’s picture

@klonos, please test it, if you have a little time. We have a possible solution, the current state is needs review. :)

klonos’s picture

ok, let me get make some time and I'll test it then.

On another note, I am following a very similar issue for the Field Collection module: #1239946: Embedded field collection items with a default value result in new items on save. So I think we should have a generic solution by changing the way core behaves with fields that are optional/required and with/without default values set for them. Here's my proposal there:

The "Add another" button when a field is set to have multiple values is a core feature AFAIK, but what if the "Add another" button was in fact a "Add one" button initially and changed to "Add another" only after one value was added? I'm not talking about the button's label here but rather about the logic behind it and the way it works/behaves...

That way:

- Having a field (or a field_collection field for that matter) multi-value-enabled would simply present a "Add one" button in the edit form (no field/widget shown on the form by default). It's like telling the user: "This field is not required, so you can omit it, but the add button is here if you want to enter a value." Even if the field has default values defined, these are not pre-filled (there's no instance of any field/widget) until only after the user hits the "Add one" button.

- Now, only if you also mark the field as required would the form contain a field/widget with a "Add another" button. It's like telling the user: "This field is required! That's why by default there is an instance of it in the form for you to fill in. You cannot opt-out!" If the field has default values, then these are pre-filled when the form (and the field/widget) is shown to the user.

adam_b’s picture

Makes sense to me... but are you proposing this for any fields, or only for entities within a node? The former would mean a big change in the default edit-form structure.

klonos’s picture

I'm pretty sure it might involve changes that will possibly prevent this from being done in D7 (perhaps it can live in contrib then), but I might be wrong - in fact I wish I am wrong ;)

One thing is certain though: this issue needs to be addressed in a more generic way since we are not the only module facing the same issue. Letting each module hack its own way out of this problem seems more prone to problems to me if it so happens that more than one of these modules happen to be in the same installation. Is anybody aware of any issue filed against core for what I'm proposing? I find it hard to believe that I am the one to think of this first :P

naught101’s picture

Can that discussion please be separated out into a separate issue in the core queue?

Marty2081’s picture

The patch in #71 caused problems in our case.

We have a node type containing an unlimited field collection field with an addressfield inside and had problems with additional fields being added every time a node was edited. So we tried the patch in #71. It worked fine in the sense that no additional fields were being saved anymore when a node was edited.

However, whenever we submitted a file or imagefield change in a node (other fields in the node type), the changes were not saved and the user was returned to the edit form without any message (not even in the Drupal or Apache logs). We spent a lot of time trying to figure out what was causing this behavior and eventually found out that by reverting the patch on Addressfield we no longer have the problem.

I have no clue what the relation between the patch and the strange behaviour of submitting file field changes is.

Thanks for your work on the patch, but unfortunately it doesn't work for us.

We now use the patch from #1263316: Configurable non-empty value conditions which works for our situation.

Zoltán Balogh’s picture

Status: Needs review » Needs work

@Marty2081 thank you for your feedback. I'll try to fix it when I have enough time.

klucid’s picture

Does anybody have a verdict on the patch or should I use the patch that Marty2081 linked to here: http://drupal.org/node/1263316 ?

adam_b’s picture

This patch is working fine for me. I'm not sure that the one in #1263316: Configurable non-empty value conditions is for the same purpose.

Marty2081’s picture

@klucid Like adam_b says: you will have to check if the other patch is suitable for your situation. It was in our case, but it might not be in your specific case.

Zoltán Balogh’s picture

Assigned: Zoltán Balogh » Unassigned

I am sorry, but currently I do not have enough time for this issue, I set to unassigned.

ryan.gibson’s picture

The patch in #71 worked for our use case.

ryan.gibson’s picture

Oh, and thanks @Zoltán Balogh.

bunthorne’s picture

My own use case is that I have a field for "Place of birth" and "Place of death" to be used for both historical and contemporary persons of note--who may still be alive. The simplest description of this issue is to have a "no address" option, as was described earlier, which blanks out all address field components including country. Once you actually have an address, you can make a case for needing a country.

I will try #71 to see if it works for me.

rooby’s picture

Patch from #71 is working for me.

It would be better without the zip file though, that way it is easier to review & commit.
You can add the new file in your patch by doing this:

git add [path.to.new.file]
git diff --staged > [project_name]-[short_description]-[issue-number]-[comment-number].patch

- Taken from http://drupal.org/node/707484

A couple of minor things I found were:

The plugin file name doesn't really follow the standards of the other existing ones.
It might be better named as optional-country.inc

+++ b/addressfield.module
@@ -427,7 +432,8 @@ function addressfield_field_widget_form(&$form, &$form_state, $field, $instance,
+  // we have to change the condition but on the field edit form $items is an empty array

Comment should start with a capital, end in a full stop. It also shouldn't be longer than 80 characters.

+++ b/addressfield.module
@@ -484,6 +490,21 @@ function addressfield_standard_country_validate($element, &$form_state) {
+  // On the field edit form we check the state of "Make country optional".
+  // If chekced, we always delete the default country on the field edit form.
+  // It is finds the name, the langcode and the delta of the field, and clears the country.

Longer than 80 characters.
Typo for chekced.

Having to save before you can select none:
I know it isn't ideal but I don't think it is that big of a deal.
It is not uncommon for you to have to reload the page to be able to select a default value based on previous selections. For example, changing the available options for a select field.

Maybe a little description could be added with the checkbox just so people know this is how it works.
Alternatively there could be some javascript added to do some fanciness for those who have it, but I don't think it is a necessity.

As to #80. I don't really know how this patch could affect file uploads.
I will test for this later.

@Marty2081:
Is it possible you have other customisations, that in combination with this patch cause issues?

jeffschuler’s picture

Besides the "chekced" typo rooby mentioned, there's another in the next line...

-  // It is finds the name, the langcode and the delta of the field, and clears the country.
+  // It finds the name, the langcode and the delta of the field, and clears the country.

I'm using Geocoder to geocode an Addressfield into a Geofield. This patch helped clear out the addressfield, but unfortunately the Geofield is maintaining its last location. Not a shortcoming of this patch, though; it's a Geocoder/Geofield issue.

duckzland’s picture

This is the same as #71, I just merged the zip files into a single patch file.

duckzland’s picture

Sorry forgot to use N in diff, here is the correct one

David Stosik’s picture

Hello,

Thanks duckzland, but the patch does not follow Drupal's community guidelines. You can find hints on how to make a patch on following pages :

If you don't use Git to create a patch (but you should !), be sure to use diff command with options -uP (unified, and include created/deleted files).
Anyways, be sure that the "diffed" folders only contain needed files (your last patch contains the previous patch file).

I rerolled the patch, based on #71 and including #89 and #90 remarks.

Regards,

David

duckzland’s picture

Thanks for the update, I'll keep the guide in mind for next patch.

joey-santiago’s picture

Patch works, but you should move the file optional-country.inc to the directory plugins/format/ in module's dir.

thanks

greta_drupal’s picture

[stupid, useless supposed threading] Me either. Cannot get Location Taxonomize to work with Address Field for any possible help.

vlkff’s picture

Using of patch of #93 (same to #71) causing two issues to me:
http://drupal.org/node/1787734
http://drupal.org/node/1787744

David Stosik’s picture

This patch fixes optional-country.inc file's location (#95, thanks joey-santiago), but still needs work.

japerry’s picture

Status: Needs work » Needs review

#98 works for me, in terms of solving ("make addressfield completely optional by having nothing saved if -none- is selected for country")

If I select the country can be optional -- the field shows up as empty and doesn't display Afghanistan (or anything else).

osopolar’s picture

Title: Add a special -none- country entry in the "Available countries" list in order to make the address field really optional. » Make the address field really optional, otherwise it may break node-/user-form submit.
Category: task » bug
Priority: Major » Critical
Status: Needs review » Needs work

Patch in 98 works for me.

@David Stosik: You said in #98: "but still needs work" ... may you clarify what else is needed to fix this issue?

@japerry: I think, when David Stosik says that this still needs work you can't set this to needs review (#99).

@vlkff: The related bugs you opened are bugs introduced by patches in this issue or do they already exists?

My problem why I came here was, that I use Address-Field in a user profile. The address is optionally. Users who registered on my clients site where not able to save the password, as described by vlkff in #1787744: Validation bug after node preview -- only if they filled in the password a second time. It took me a whole day or more to figure out that address field is causing this problem (the site and the profile are more complex with tons of modules involved).

Setting this issue to critical, because the sites default behavior is broken without this fix.

BTW: The patch in #98 solves my bug.

rszrama’s picture

Title: Make the address field really optional, otherwise it may break node-/user-form submit. » Add a special -none- country entry in the "Available countries" list in order to make the address field really optional.
Category: bug » task
Priority: Critical » Major
Status: Needs work » Needs review

If the patch works, not sure why this was downgraded to "needs work." Resetting the metadata to major; it's not a critical bug if it's simply an unsupported use case. Will take a look at the patch shortly.

TimG1’s picture

Tried patch #98 on addressfield 7.x-1.0-beta3 (2012-May-29) and 7.x-1.x-dev (2011-Dec-13). Neither worked for me. I'm supposed to see a checkbox to "Make country optional" on the addressfield edit page correct? Did I miss a step?

Drupal 7.17
Entity API 7.x-1.0-rc3+11-dev (2012-Oct-23)

By the way, why is the date stamp for beta3 more recent than the -dev version? Shouldn't the -dev always be more recent to be sure the latest changes have been rolled in?

Thanks for reading,
-Tim

David Stosik’s picture

If I refer to the repository viewer, both dev and beta3 point to the same commit, which was made Mon, 12 Dec 2011, that is 11 months ago.
Only the tag beta3 has been added later, I guess.

Path works like a charm form me, I just tried again.

$ tar xvzf addressfield-7.x-1.0-beta3.tar.gz
$ cd addressfield
$ patch -p1 < ../addressfield-nocountry_option-968112-98.patch

results in

patching file addressfield.module
patching file plugins/format/optional-country.inc

If patch applied successfully, did you try to clear caches? (You never know...)

Regards,
David

joelcollinsdc’s picture

It seems this patch only works if the available countries filter isn't used, correct? That seems unfortunate.

Also, is it just me or is the work being done here almost completely superceded by the configuration options that are available in #1263316: Configurable non-empty value conditions? Making the country 'optional' is really just a workaround to make the field empty if only the country field is filled in?

alexrayu’s picture

Thanks, the patch is very helpful.

Cauliflower’s picture

Status: Needs review » Reviewed & tested by the community

Patch in #98 tested and works as expected (don't forget to enable 'Make country optional').

JurgenR’s picture

Patch in #98 works as expected, only received a warning using beta3 version:
warning: addressfield.module has type 100755, expected 100644

sheldonkreger’s picture

Status: Reviewed & tested by the community » Needs work

#98 effectively makes the country optional, but if the user does not select a country, then the field for "State" disappears from both input and display. This is probably only a problem with addresses in the United States.

I believe that a valid use-case is to eliminate display of the "Country" field, but still display the "State" field on the node.

pfrenssen’s picture

@sheldonkreger, care to explain why this is a problem? If a user would choose any other country the 'State' field would disappear too, so why shouldn't it disappear when you choose none?

If it would work the other way around, if your default country is anything other than the US it would add a 'state' field out of the blue when the user does not choose a country, that is very strange behavior.

mossy2100’s picture

Sorry for not reading this whole thread before posting. This solution may have already been suggested.

The site I'm currently working on is international and I don't want to have a default country. Also, with unlimited value fields, if you have a country selected by default the address is counted as not null and gets saved, which means you add a new address every time you edit and save a node, even if nothing is changed.

This was my solution:
- In hook_form_alter(), added an option array('' => '- Please select -') to the top of the #options array for the country.
- If the addressfield is new, set default_value to ''.
- In some JS/JQ, for each addressfield country selector, if it's blank remove the other addressfield fields from the page. You could also just hide them. It makes no difference because the fields are replaced via AJAX once you select a country.
- (Tricky bit) If the country is non-null then set to '', I wanted the addressfields to disappear. But an AJAX callback is triggered (haven't tried interrupting that) so I have to do some setTimeout magic to remove those fields after the AJAX returned.

Let me know if you want me to share any code.

Chris Graham’s picture

I know this is a really dirty workaround, but for anyone who wants a single value address field to be truly optional then you can use a hook_node_presave to alter the node before it gets saved. This of course will get a bit unwieldy if you have multiple address fields across multiple content types, but I only needed it in one content type.

Pop this code in a custom module, it basically resets the address field array when only country has been set (I also reset the location geofield I am using as it is geocoded from my address field and I don't want it showing up on maps either):

function MYMODULE_node_presave($node) {
  if ($node->type == 'MYNODETYPE') {
    $address = $node->field_address[LANGUAGE_NONE][0];
    unset($address['country']);
    unset($address['element_key']);
    
    $found = false;
    
    // Cycles all submitted address field components for the country to see if any have been filled
    foreach ($address as $value) {
      if (!empty($value) > 0) { $found = true; }
    }
    
    // Empty the address and location arrays if the fields were blank
    if (!$found) {
      $node->field_address = array();
      $node->field_location = array();
    }
  }
}

Now, obviously this is not going to work with multiple value address fields and I'm not developing a multi-lingual website which would complicate the code even more, but it is a quick workaround for the problem for an individual optional address field.

__cj’s picture

Status: Needs work » Reviewed & tested by the community

Works for me. Setting back to RTBC.

sheldonkreger - #108 - Sounds like a slight different issue.

johnv’s picture

Status: Reviewed & tested by the community » Postponed

I've been reviewing all issues with required/optional subfields.

resuming status of this issue:
- #98 is RTBC. It adds a new 'optional country' option and appends _is_empty().
- #111 is an alternative solution for issue 1844846

But I think this issue - which is a pretty big patch - should be last in line, and might be obsolete:
1. For some cases (like #108) , you do not want to make the country optional, you want it to be hidden if only one is valid. The following issue resolves that: #1316788: Do not display country in address block if only one country option is available
#1316788-#1 resolves the widget and is committed on October 21, 2011 , áfter the creation of this issue.
#1316788-#9 resolves the formatter and is RTBC.

2. For most cases, if user does not touch the addressfield, you do not want it to be stored.
That is discussed here:
#1844846: Make Addressfield optional, (even if country is required)
#1031422: Issues with addressfield multiple fields

If both issues are addressed, #98 will 'need work', and I doubt if it is still a relevant feature request.

I'll just be bold, and set the status accordingly, and invite people to assess the other issues.

Qandeel’s picture

Priority: Major » Critical

Hi,

I second #100 in this thread, the default behavior of resetting a password or user creation through email verification is broken because user has to submit the form twice to change his password if the country field is optional.. this is a critical issue.. need to be resolved soon.

Qandeel’s picture

Hi, the patch mentioned here http://drupal.org/node/1844846#comment-7118330 does not help me at all.. I think its an issue with the validation funciton associated with address field. Because Afghanistan is by default at the top of the list and also does not have a post code so the field gets wiped out if we save a user.. and on second attempt the password gets changed.

I am going to test the patch mentioned in this thread at #98 to make it sure this works

Qandeel’s picture

David,

The patch worked fine, without any errors, what should i see in the field edit page... am i going to see a new option "Make country field optional" ???

giuvax’s picture

I know this is really ugly as workaround but I gave up finding a solution with empty saved profile because of mandatory country field. I'm tired of searching solutions.
So I decided to create a rule, and here it is:


{ "rules_address_field_wtf_solution" : {
    "LABEL" : "Address Field WTF solution",
    "PLUGIN" : "reaction rule",
    "REQUIRES" : [ "rules", "entity" ],
    "ON" : [ "commerce_customer_profile_insert" ],
    "IF" : [
      { "data_is" : { "data" : [ "commerce-customer-profile:type" ], "value" : "billing" } },
      { "data_is_empty" : { "data" : [ "commerce-customer-profile:commerce-customer-address:locality" ] } },
      { "data_is_empty" : { "data" : [ "commerce-customer-profile:commerce-customer-address:postal-code" ] } }
    ],
    "DO" : [ { "entity_delete" : { "data" : [ "commerce-customer-profile" ] } } ]
  }
}

I put two conditions just to be sure.

deanflory’s picture

#98 Patch currently exhibits these results when attempting to apply to the beta4 2013-May-07 and May-08 dev versions:

patch < addressfield-nocountry_option-968112-98.patch
patching file addressfield.module
Hunk #1 succeeded at 322 (offset 8 lines).
Hunk #2 succeeded at 337 (offset 10 lines).
Hunk #3 succeeded at 358 (offset 10 lines).
Hunk #4 succeeded at 443 (offset 11 lines).
Hunk #5 FAILED at 456.
Hunk #6 succeeded at 509 (offset 18 lines).
1 out of 6 hunks FAILED -- saving rejects to file addressfield.module.rej
patching file optional-country.inc

Am I missing the actual fix in one of the 5 issues about this same simple thing?

fgm’s picture

FWIW, this patch is not included in 7.x-1.0-beta4.

mlncn’s picture

Priority: Critical » Major
Status: Postponed » Reviewed & tested by the community

The patch in comment 98 is indeed reviewed and tested by the community and fixes the issue most people have, which is simply to make the whole field optional. Other improvements can come later. (The two year anniversary of my patch in comment 10 is coming up, and it still works too. I just don't want to have to reapply any patch after another security update.)

steinmb’s picture

Status: Reviewed & tested by the community » Needs work

Not sure if we are RTBC here, #113 contains some very valid opinions that we still not have addressed and
#1844846: Make Addressfield optional, (even if country is required) #40 contain a really nice summary that I encourage all followers of this tread to read, at least did I find it very useful.

fgm’s picture

Status: Needs work » Needs review
FileSize
5.76 KB

Rerolled on top of 7.x-1.0-beta4, for which it no longer applied.

BTW this module would need tests, but this is another issue.

deggertsen’s picture

Status: Needs review » Reviewed & tested by the community

I personally agree with @mlncn. Progress is important. If the patch works, it should be applied unless it causes other issues. If there are other options/features that need to be considered, they should be created as separate feature requests. If we wait until every opinion and every possible feature is considered and added to the patch, the patch will probably never be ready. Of course again, if this patch breaks something that was working before, that should indeed be fixed before the patch is applied. However, I am not aware of any problems like that with this patch, though I admit that I'm new to this issue.

Patch in #122 works great.

mrmikedewolf’s picture

Hey all, I had to modify #98 in order to update to the newest version of address field. Hunk 5 was failing on account of new lines of code being added in the source.

colan’s picture

Status: Reviewed & tested by the community » Needs review
Yuri’s picture

With the latest dev and patch #124, I don't see what has changed..still can't make country optional. Am i overlooking something?

fgm’s picture

@Yuri: did you try #122 ? It works for me and, apparently, for @deggertsen.

deggertsen’s picture

Right, number #122 and #124 appear to be very different patches. I'm not sure what #124 does as I have not looked into it. #122 fixes the problem in the issue title. Test it and mark RTBC if it works.

Martijn Houtman’s picture

#122 with -beta4 works fine for me, solves the problem.

rolfmeijer’s picture

Issue summary: View changes
FileSize
7.44 KB

Not sure if I do something wrong but I cannot make #122 work, not with -beta4 nor with -dev (2013-Nov-14).
I add a screenshot, as I expect, reading this thread, that I miss a checkbox in the Format Handlers.
Screenshot of format handlers without the optional country option

guedressel’s picture

#122 works basically. It added the "Make country optional" Checkbox in the settings. But - since my field allows unlimited values - I get a new empty address value each time I save the node.

guedressel’s picture

Turned out that the patch of #122 was missing the "_watch" element in the values-array which gets submitted during node editing.
With my patches the empty fields get detected cleanly.
I uploaded two patches: one for 7.x-1.x and one for the 1.0-beta4 release.

Angry Dan’s picture

Status: Needs review » Reviewed & tested by the community

#132 works just fine for me.
Edit: I should probably say that I also reviewed the patch and all is fine there as well.

davisben’s picture

The patch works for me as well. Here it is re-rolled against the latest dev.

deanflory’s picture

Was that against the latest dev 2013-Nov-24, or against the newer beta5 2013-Dec-06 release?

bmango’s picture

I tried the patch in #134 but it would not apply cleanly to the latest dev.

klonos’s picture

I recently came across Field Suppress. I haven't actually used the module, so I don't know in what shape it is, but from its description:

Two new field settings are provided.
field_suppress: The following three states are provided.
never: Never suppress field data.
always: Always suppress field data. Any changes to field data will be ignored during entity update and insert.
load: Only suppress field data during entity_load() so any data added will be saved during entity update or insert.

field_suppress_blank: In some cases it may be useful to have a "blank" entry be added for a field that is suppressed. An example is when using this module with Field Group Views which uses field_group to replace the fields that are children of the group. If none of the fields have a value then the group will not be displayed. Keep in mind this is somewhat hackish since the field API does not provide a standard array structure so adding a "blank" row may not always work.

I'm not sure if or how, but the field_suppress_blank field setting seems that might prove useful if we used it for our cause.

PS: cross-posting this to the other related issues for those that don't follow here.

robcarr’s picture

I rolled the patch at #134 against the current beta5 release - works just fine. For some reason DEV is currently older than beta5.

As soon as the 'Make country optional' box is checked in the Field's Format Handlers (and the Field saved), then the -None- option appears in the Select list. Fantastic solution. Seen no problems yet.

I hope this patch can get committed soon.

MXT’s picture

I can confirm that

patch provided in #134 works very well and is the best choice for me of all the following related:

Please commit this ASAP

Thank you very much for your work

flatfeat’s picture

I too can confirm that the patch in #134 works for me, too, and solves the problem of making the address field optional. Thanks for developing this patch.

rolfmeijer’s picture

FWIW, #134 works for me too, on -beta5
(just make sure you patch with -p1, so the optional-country.inc file gets placed in the plugins/format-directory, hence removed the screenshot from #130).

adam-delaney’s picture

I tested patch 134 against the beta5 release and it applied cleanly and resolves the issue. However this patch does not retroactively fix nodes that had empty address fields before the patch was applied.

steve65140’s picture

I too can confirm that the patch in #134 works for me and solves the problem of making the address field optional. Many thanks for developing this patch.

Elin Yordanov’s picture

@steve65140: To subscribe please use the 'Follow' button on the upper right side of the issue pages. So that all other subscribers of the issue won't be informed about your subscription. Thanks.

Leeteq’s picture

@pc-wurm: that was not just a "subscription comment" from @steve65140, it contained a valuable confirmation that a patch works also for him, so he was right posting a comment instead of just "following" the issue.

Elin Yordanov’s picture

@Leeteq, he has updated his post after mines. It was only a one-word 'Subscribing' post, otherwise I wouldn't advice him to use the 'Follow' button.

claudiu.cristea’s picture

It's RTBC and good to go. Bumping!

jantoine’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
4.54 KB

I updated the patch with the following changes:

  • Refined default address code to be more efficient (performance improvement, no effect on functionality).
  • Changed the addressfield_field_is_empty() function to return after finding a single non-empty field, rather than processing all fields (performance improvement, no effect on functionality).
  • Cleaned up the clearing of the country default value on the widget form as there will only ever be a single address form and field setting forms are language agnostic (performance improvement, no effect on functionality).
  • Cleaned up code comments.
  • Fixed the addressfield_format_address_optional_country() signature to not pass $address by reference (bug fix).

I am still uncertain about the $ignore_item_keys variable and associated functionality in the addressfield_field_is_empty() function? I don't see these elements in my array.

/**
 * Implements hook_field_is_empty().
 */
function addressfield_field_is_empty($item, $field) {
  $ignore_item_keys = array('element_key', '_weight');
  foreach ($item as $field_name => $field_value) {
    if (in_array($field_name, $ignore_item_keys)) {
      continue;
    }
    if (!empty($field_value)) {
      return FALSE;
    }
  }

  return TRUE;
}
Leeteq’s picture

#147/@pc-wurm: ah, ok, I did not pay close attention to the time stamp. Then my comment was moot.

ergophobe’s picture

The patch in 149 seems to be coming into play too late to make this work with geofield.

The specific problem I'm having is that an empty location nevertheless creates a geofield entry when the geofield is set to pull location from an addressfield. This creates a fatal PHP error. I'm not sure yet whether this should be considered an issue with this patch or with geofield, geophp or geocoder. But definitely applying this patch results in a fatal error

Notice: Undefined index: in geoPHP::load() (line 62 of D:\Documents\htdocs\BBANZ\public_html\sites\all\modules\contrib\geophp\geoPHP\geoPHP.inc).
Exception: geoPHP could not find an adapter of type in geoPHP::load() (line 65 of D:\Documents\htdocs\BBANZ\public_html\sites\all\modules\contrib\geophp\geoPHP\geoPHP.inc).

ergophobe’s picture

I think the problem I came across in 151 is better solved on the geofield end of things.

The issue that I hae come across with the current patch that is relevant is essentially the issue highlighted by Ryan in #1 and Damien in #23, which is that ideally it would be possible to still have a default country and show the correct fields based on that or, failing that, to have it use the site default country.

This comes up for me because the addressfield module still gets Australian and New Zealand addresses wrong as per the outstanding issue here #1713698: Correct address labels for Australia for which I've offered a patch.

It seems like a better approach here would be to say that if the country is optional and only the country field has a value, that the field be considered empty, similar to how this problem is handled by https://drupal.org/project/address_empty because of #1263316: Configurable non-empty value conditions.

What are the chances that someone would have "country optional" set to true, but would consider an address consisting only of the country to be valid?

This would be an option to unsetting the default country as happens with the current patch.

steinmb’s picture

There is also a link in my #121 comment that is worth a read.

ergophobe’s picture

The problem in #151 is fixed by the patch in #4 in #1992762: Programmatic Update of an entity empties a geofield due to regression introduced/exposed in issue #1777934

The other issue I mentioned is fixed (I hope) by the attached patch. Basically, this patch uses the plugin created by jantoine unchanged.

However, it also allows the admin to set the default value for the country on the field. I think it's a bad UI with the current patch in that you can change the country, but when you save it reverts to the old value and, in any case, the default setting for country on the field instance gets thrown away, so it's pretty confusing.

This patch uses the following simple logic: if the field instance has the "optional-country" flag set AND the current country on the submitted form is the same as the default country for the field instance, consider the field empty.

A further refinement would be along the lines that Damien mentioned further up, which is that if a default country is set, the "None" value would use the same settings as the default country. That would make for a nicer UI, but maybe is a different and somewhat less critical issue.

ergophobe’s picture

Posted my patch while steinmb was posting hsi comment.

The discussion he mentions is here (#40 in #1844846: Make Addressfield optional, (even if country is required) )

The most significant suggestion there relative to this issue is that the admin be able to set which fields can be filled out and still have the field be considered empty.

The patch I submitted makes some progress toward that in that it extends the addressfield_field_is_empty() function by grabbing the field instance info so it gives access to all the field settings.

To satisfy the issue mentioned there would require adding some sort of UI (checkboxes of fields to ignore I suppose) and otherwise would be pretty easy to fold in... but it's way past my bedtime.

Also in the referenced comment, mbrakken says of the patches in this issue that

It has some admin UI issues, leading to site builder uncertainty about how to configure it. Also, it still registers the field as populated, so values get saved.

At least in my testing, my patch solves the problem of the values getting saved - that was actually the issue that got me working on this in the first place, because the existing patch was causing problems for me with geofield geocode values from addressfield.

SocialNicheGuru’s picture

updated related issues

ergophobe’s picture

Just a quick note - the work being done in #1263316: Configurable non-empty value conditions is promising, but currently it actually makes this problem worse. If one of the patches here and one of the patches there got combined, the end result would solve a lot of Addressfield issues!

ergophobe’s picture

Add check to make sure "optional-county" array element is not empty, causing an undefined index notice under specific circumstances.

Robin Millette’s picture

As #117, I also used a rule to get rid of default adress fields. In my case, "Québec, Canada" (Québec = QC) is the default value. To remove that field value, when it's the default, I have the following, it's very explicit to make sure I don't remove other values.


{ "rules_cleanloc" : {
    "LABEL" : "cleanloc",
    "PLUGIN" : "reaction rule",
    "OWNER" : "rules",
    "REQUIRES" : [ "rules" ],
    "ON" : { "node_presave--journal" : { "bundle" : "journal" } },
    "IF" : [
      { "data_is" : { "data" : [ "node:field-adresse:country" ], "value" : "CA" } },
      { "AND" : [
          { "data_is" : { "data" : [ "node:field-adresse:administrative-area" ], "value" : "QC" } },
          { "data_is_empty" : { "data" : [ "node:field-adresse:name-line" ] } },
          { "data_is_empty" : { "data" : [ "node:field-adresse:first-name" ] } },
          { "data_is_empty" : { "data" : [ "node:field-adresse:last-name" ] } },
          { "data_is_empty" : { "data" : [ "node:field-adresse:organisation-name" ] } },
          { "data_is_empty" : { "data" : [ "node:field-adresse:sub-administrative-area" ] } },
          { "data_is_empty" : { "data" : [ "node:field-adresse:locality" ] } },
          { "data_is_empty" : { "data" : [ "node:field-adresse:dependent-locality" ] } },
          { "data_is_empty" : { "data" : [ "node:field-adresse:postal-code" ] } },
          { "data_is_empty" : { "data" : [ "node:field-adresse:thoroughfare" ] } },
          { "data_is_empty" : { "data" : [ "node:field-adresse:premise" ] } }
        ]
      }
    ],
    "DO" : [
      { "data_set" : { "data" : [ "node:field-adresse" ], "value" : [ "" ] } },
      { "data_set" : { "data" : [ "node:field-localization-geofield" ], "value" : [ "" ] } }
    ]
  }
}
deggertsen’s picture

Could somebody who knows where we are at with this issue update the summary? I'm a bit lost as to the progress on this one and don't really want to read through all the many comments... Files need to be cleaned up as well to only show the files that are still relevant.

Thanks.

rooby’s picture

deanflory’s picture

Unless someone else takes the time to combine all sorts of patches into one solution against the latest dev for D7, I'm guessing this module is dead based on the fact that most of these issues are 3-4 years old, are not fixed, and the latest release was almost a year old and didn't attempt to fix these major issues with the module (none, none selected, delete location button/allow multiple locations). I'm unable to find a solution between all the patches that don't fail when combined. The status of this module's maintenance needs to be changed. To go from the Location module in D6 to Address Field in D7 is a sure fail, tired of looping back to the same UX issues.

steinmb’s picture

Ref. 160. This issue is getting long with with different pron and cons.

rszrama’s picture

@deanflory This module isn't dead, fwiw. It's just complicated.

Yazzbe’s picture

I also rolled the patch at #134 against the current beta5 and it solves the issue.

andrewigman’s picture

same here, #134 with the current beta5 worked for me too.

deggertsen’s picture

Looks like @bojanz is doing some good work on this module now with the recent commits. What do we need to test and do on this issue to get it ready to commit?

twooten’s picture

I just wanted to add that the patch in #134 is working well for me with beta5. I have 8 content types, some with multiple values some with single values.

Bill Choy’s picture

Patch 158 ...almost works. But if you have 'Make country optional' NOT CHECKED, in the Format handlers section of manage field page, then you have a problem.

+ // @TODO - $optional_country should be a boolean to match similar settings
+ $ignore_item_keys = array('element_key', '_weight');
+ if ($optional_country && $default_country && $default_country == $item['country']) {
+ $ignore_item_keys[] = 'country';
+ }

Should be

+ // @TODO - $optional_country should be a boolean to match similar settings
+ $ignore_item_keys = array('element_key', '_weight');
+ if (($optional_country && empty($item['country'])) || ($default_country && $default_country == $item['country'])) {
+ $ignore_item_keys[] = 'country';
+ }

bojanz’s picture

Title: Add a special -none- country entry in the "Available countries" list in order to make the address field really optional. » Allow addressfield to be optional
Category: Task » Feature request
Status: Needs review » Needs work
Issue tags: -Needs issue summary update

I have just closed #1263316: Configurable non-empty value conditions and #1844846: Make Addressfield optional, (even if country is required) , and will be solving the optional addressfield issue here, since it's the closest in spirit to a desired solution.
Setting to a "feature request" because this is a use case addressfield never supported or was designed for.

Since there are over 200 comments in 3 issues detailing wildly different solutions to wildly different problems, I'm going to narrow it down:

Every address, at minimum MUST have a country. But, the addressfield SHOULD not need to be required.
Therefore, if the addressfield is optional, a "- None -" option should exist for the Country.
While "- None -" is selected, no other field is rendered, and nothing is entered into the database.
Once a country is selected, the rest of the widget is rendered.

Setting to "needs work" and working on a patch.

gmclelland’s picture

What are your thoughts about #76.

Seems like just displaying an "Add new" button would solve all of the problems.

Pros:
- Address field wouldn't take up some much space on a node edit form. All that is displayed would be an "Add new" button.
- A Default country can be selected in the field settings
- Works for single or multivalued fields

Cons:
- It would still need a "- None -" country option (like @bojanz mentioned) for people that don't want to set a default country. -This could be split into a different issue
- It doesn't fix all the existing nodes that have empty rows saved in the database with just a country

My use cases is that I have an "event" content type where the addressfield is optional. This field is a US based. I don't want the user to have to fill in the City, State, and Zip Code. I would like those to have defaults for the City, State, Zip Code, and Country set and the Country field should be hidden because it will always be a US based address. I believe having an "Add new" button would fulfill these requirements and keep things simple.

  • bojanz committed 6371ea8 on 7.x-1.x
    Issue #968112 by Zoltán Balogh, ergophobe, mlncn, duckzland, David...
bojanz’s picture

Status: Needs work » Fixed
FileSize
37.41 KB
74.96 KB

And committed:
http://cgit.drupalcode.org/addressfield/commit/?id=6371ea8
http://cgit.drupalcode.org/addressfield/commit/?id=34e3d67

Attaching screenshots of an optional single-value addressfield, on first load, and after the country change.
Also tested with multivalue fields, works fine.

I disregarded the "Add new" button because it would mean two radically different interfaces for required => TRUE and required => FALSE, and because the current implementation fits better with existing drupal UI patterns. The current implementation has the same pros & cons you listed for the button.

rszrama’s picture

  • bojanz committed 34e3d67 on 7.x-1.x
    Fix regression introduced by #968112 that caused the addressfield on the...
deggertsen’s picture

YAY! I'll finally be able to update without applying a patch. Thank you!

rooby’s picture

Excellent, this has been problematic for a long time.

I really like this solution too.

cybermache’s picture

Perhaps my use needs are too simple for this awesomely complex module. I'll only be accepting addresses form the US. I liked before that I when I selected only US and the 'Hide country if only one is selected' option, the country would not be available. But now I think because the code for the hiding of the field works the way it does, it includes '- None -' as an option and therefore there are two countries and not one. So, the country is still visible in the form. Does that make sense? Is there anyway that the value '- None -' can be ignored or not identified as a country choice?

bojanz’s picture

The module is awesomely complex because of everyone's simple needs :)

@cybermache
It's easy to hide the country if the field is required.
If you want to have both the country hidden and the field optional, you will need to invent a new UX for adding field values, possibly the one proposed in #2392033: Add an "Add new or Add address" button.

Revalis’s picture

After updating, I have 2 problems; one of which is mentioned by cybermache. I was only accepting US addresses with the field, but now that there is a "- None -" option available, I have a country list option that has the 2 options. And this has broken the 'hide if 1 country selected' item.

Also, my fields have become 'REQUIRED' regardless of the option being checked or not.

bojanz’s picture

After updating, I have 2 problems; one of which is mentioned by cybermache. I was only accepting US addresses with the field, but now that there is a "- None -" option available, I have a country list option that has the 2 options. And this has broken the 'hide if 1 country selected' item.

As I said, that's the intended behavior.

Also, my fields have become 'REQUIRED' regardless of the option being checked or not.

Open a new issue with a screenshot, and I'll try to reproduce it.
Keep in mind that if a country is selected, other fields (state, postal code, etc) will of course be required.

Revalis’s picture

As I said, that's the intended behavior.

That doesn't seem very intuitive, unfortunately. And creates 'broken' behavior for the 'Hide the country when only one is available' option.
edit: I say this because, technically, you can't ever select 1 country anymore; as '- None -' is added forcibly elsewhere and not an option to exclude from the list.

Keep in mind that if a country is selected, other fields (state, postal code, etc) will of course be required.

That's the problem, then. Because I only have 1 country selected in the 'available countries' list. And since the addition of - None - (which I can't unselect as an option from the country list), it pre-selects 'United States' on the form, thus overriding my desire for the field to be optional.

bojanz’s picture

That doesn't seem very intuitive, unfortunately. And creates 'broken' behavior for the 'Hide the country when only one is available' option.

See my answer in #179 for the only way to fix that.

That's the problem, then. Because I only have 1 country selected in the 'available countries' list. And since the addition of - None - (which I can't unselect as an option from the country list), it pre-selects 'United States' on the form, thus overriding my desire for the field to be optional.

Sounds like you might be running into #2392059: Choosing -None- still saves a row in the database, which is fixed in -dev.
I suggest updating from -rc1 to -dev, we fixed the bug there, you can switch the country from "United States" back to "- None -", thus hiding the other required fields.

cybermache’s picture

@bojanz
Thanks for the quick response.

When you say

It's easy to hide the country if the field is required.

are you talking about hiding with CSS? Or is there an admin setting that I am missing.

Thanks again

Revalis’s picture

are you talking about hiding with CSS? Or is there an admin setting that I am missing.

He means that you should mark the field as a 'Required Field' (The option under the label), have 1 country selected, and select the 'Hide the country when only one is available' option.

In this use case, the select option for Country is automatically hidden. It's only when 'Required Field' is not selected, that the 'None' option is appended and creates 2 options.

cybermache’s picture

Thanks @Ravalis, that did it and I think will work fine for my use case.

emilorol’s picture

This issues is back live. Now even when not marking the field as required the fields are still required.

bojanz’s picture

I won't be responding to comments in this issue anymore. I've used it to solve what I thought the central issue is.

Open a new issue for any additional concerns or questions you might have.

Status: Fixed » Closed (fixed)

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

mibfire’s picture

When "Make country optional" is set and entity is saved there is a validation error:

Notice: Undefined index: #default_value in addressfield_standard_country_validate() (line 633 of /var/www/sites/all/modules/contrib/addressfield/addressfield.module).

I created a patch to fix this.

rszrama’s picture

Status: Closed (fixed) » Needs review

Reopening for review.

bojanz’s picture

Status: Needs review » Closed (fixed)

#190 is a reroll of a patch I said I won't commit. Let this issue rest in peace.

rszrama’s picture

hah, nice. : P

mibfire’s picture

@bojanz, what is the problem?(I havent really read the comments about it) Why cant we solve this issue properly?

rszrama’s picture

Please read the thread before asking Bojan to spend time rehashing it. We don't have time to do that on issues.