Download & Extend

Add a special -none- country entry in the "Available countries" list in order to make the address field really optional.

Project:Address Field
Version:7.x-1.x-dev
Component:Code
Category:task
Priority:major
Assigned:Zoltán Balogh
Status:needs work
Issue tags:bounty

Issue Summary

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?

Comments

#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. I'll have to revisit our available country settings - if only one country is available, then I suppose the country element could be hidden.

#2

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...

#3

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

#4

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.

#5

+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.

#6

Subscribing

#7

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

#8

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).

#9

Status:active» needs review

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

AttachmentSize
allow-none-country-968112-9.patch 316 bytes

#10

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

AttachmentSize
allow-none-country-968112-10.patch 961 bytes

#11

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

#12

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.

#13

Subscribing

#14

+1

#15

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).

#16

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

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

#17

Subscribe

#18

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']
  );

#19

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.

#20

EDIT: double post. Guru, stop meditating.

#21

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.

#22

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)...

#23

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:

<?php
  $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. ;)

#24

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

#25

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

#26

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?

#27

@#25,
Sounds reasonable to me.

#28

+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.

#29

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

#30

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

#31

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:

<?php
/* 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);

}
?>

#32

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

<?php
 
// 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.

#33

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?

#34

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.

#35

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 ;)

#36

^^^^ 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.

#37

...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.

#38

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.

#39

@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

#40

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.

#41

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.

#42

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?

#43

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?

#44

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.

#45

Title:Make the country optional, None value?» Make an addressfield to optional
Category:feature request» bug report
Priority:normal» major
Assigned to:Anonymous» Zoltán Balogh

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.

#46

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.

AttachmentSize
addressfield.patch 2.71 KB

#47

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 report» feature request

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?

#48

...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.

#49

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.

#50

Category:feature request» 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?? ;)

#51

@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*.

#52

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

#53

Assigned to:Zoltán Balogh» Anonymous

Ok, I submit a new issue.

#54

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

#55

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?

#56

...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).

#57

Assigned to:Anonymous» 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?

#58

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.

#59

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...

#60

Status:needs work» needs review

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.

AttachmentSize
968112_60_addressfield_1.patch 2.5 KB
address-optional-country.zip 786 bytes

#61

#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?

#62

@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.

#63

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.

#64

Status:needs review» needs work

Here, too.

#65

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.

#66

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.

#67

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.

#68

@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".

#69

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.

#70

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.

#71

Status:needs work» needs review

Second try.

AttachmentSize
968112_71_addressfield_2.patch 3.66 KB
address-optional-country.zip 532 bytes

#72

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?

#73

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

#74

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 ;)

#75

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

#76

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.

#77

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.

#78

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

#79

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

#80

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.

#81

Status:needs review» needs work

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

nobody click here