Download & Extend

Do not display country in address block if only one country option is available

Project:Address Field
Version:7.x-1.x-dev
Component:User interface
Category:bug report
Priority:normal
Assigned:Unassigned
Status:fixed

Issue Summary

This is related to #968112: Add a special -none- country entry in the "Available countries" list in order to make the address field really optional. but not dependent on it.

AttachmentSize
addressfield-single_country_option.patch596 bytes

Comments

#1

Status:needs review» fixed

I think that's a good idea, but we should keep it optional. I merged the attached.

AttachmentSize
1316788-single-country.patch 1021 bytes

#2

Status:fixed» needs review

Thanks for adding this, Damien. It's exactly what I need. But the patch has no effect in "render" mode because the options list is not restricted in that mode.

The attached patch fixes this by moving the relevant code out of the "if(mode == 'form')" block. Is this the right way to do it?

AttachmentSize
0001-Restrict-country-list-for-render-as-well-as-form-con.patch 1.71 KB

#3

Title:Hide country selection dropdown if only one country option is available» Do not display country in address block if only one country option is available

Bumping, and changing the title to more accurately describe the latest issue.

My rationale for this change is that if a site makes just one country available to its users, then that country should be implied in all contexts where addresses are used. My use case is a teacher's union in the US -- all addresses are for members or schools within a district. We don't need "United States" printed on every address label.

#4

I applied the patch in #2 because I need the same feature. Regardless of whether or not that's The Right Way To Do It, I think it's the way the "hide country" feature should work.

#5

Tested #2 and it works.

#6

I think it should be changed to a bug as (especially with the display settings), it is expected behaviour for it to be hidden.

As instance settings count potentially change, is it not better to make the adjustment in the address-hide-country plugin so that it doesn't break previously saved fields? So changing the #access check to something like the attached patch...

That means that if there is only one country, it will ignore rendering it unless the saved country isn't in the allowed list (ie is different from normal), in which case it will show it.

AttachmentSize
addressfield-hide-country-render-1316788-6.patch 1.11 KB

#7

Is this patch backported to the official release?

#8

The improved logic behind #6 is perfect! This really made my day Andrew ;)

PS: ...while at it, there's a minor typo in address-hide-country.inc (doesn't deserve its own issue):

/**
* @file
* Hide then country when only one country is available.
*/

...should be:

/**
* @file
* Hide the country when only one country is available.
*/

("the" - not "then")

#9

Rerolled #6 with unix whitespace. For me, this is important for drush make patches to work. http://drupal.org/coding-standards#indenting

AttachmentSize
addressfield-hide-country-render-1316788-9.patch 1.09 KB

#10

I am having trouble applying either patch #6 or #9. I am fairly new to git and I got this message when I tried to apply either patch.

"can't find file to patch at input line 5"

#11

You probably just need to supply "-p1" on the command line. Example:

$ git clone --recursive --branch 7.x-1.x http://git.drupal.org/project/addressfield.git
$ cd addressfield/
$ wget -q http://drupal.org/files/addressfield-hide-country-render-1316788-9.patch
$ patch -p1 < addressfield-hide-country-render-1316788-9.patch
patching file plugins/format/address-hide-country.inc

What's going on is, if you look at the top of the patch file, you'll see paths that look like a/foo and b/foo. The a and b are really just dummy directories; if you're sitting in the directory you'd like to patch, you don't need them. So, you want to strip them off. The "-p1" argument to patch simply means "strip off the first path component."

#12

mjorlitsky,

Thank you for your help. I was able to use Git to install the branch 7.x-1.x on my local machine.

(I was a little confused because the repository showed no dev version, even though the dev version is shown on the project page. The dev is also the version that this thread is shown under. But the 7.x-1.x version that you showed in your example was in the repository and not on the project page. )

I am using a CygWin teminal with Git Bash. It says: sh:exe" wget command not found. I have searched all over the web and I haven't figured out how to have wget work. So I haven't been able to use Git to get the patch.

#13

I was using wget to download the patch, but it was just to make the command-line example clearer. Where I used wget, you can open your browser and download the patch file manually into the addressfield directory.

The rest should work the same.

#14

Okay thanks. I wondering about that - if I could just put the patch in the directory. I ended up putting the patch in addressfield/plugins/format dir because I continued to get the error message about line 5. So I tried to patch with it in the same dir as the .inc but got this message: patch**** malformed patch at line 22.

#15

Well, if you re-run the cygwin setup, you should be able to install wget. Then you can run the example exactly =)

#16

Version:7.x-1.x-dev» 7.x-1.0-beta3

I've installed 7.x-1.0-beta3, checked the new "Hide the country when only one is available" and yet the one country (United States) is still rendered in the address field of the nodes. Am I misunderstanding the nature of this enhancement?

#17

If I remember correctly, the checkbox was always there. It just doesn't work the way you expect it to without this patch. Did you apply the patch?

#18

I'm pretty sure that checkbox didn't exist in beta2. I've been writing a tutorial for fellow newbies and I just documented the options on that page last week while using beta2.
I didn't install a patch but the beta3 release notes indicate that the patch is included in beta3. I installed beta3 today.
Thank you so much for helping with this.

#19

Oh, you're right.

From the release notes, it looks like the patch in comment #1 was merged. That adds the checkbox, and checking the box makes the country field go away when you're creating stuff. But as comment #2 points out, it has no effect on the display of the country field.

In my opinion comment #9 has the best fix so far.

#20

Okay, thanks. I'll continue to use CSS to set the display of the Country field to "none" until this is included functionality.
For the benefit of any other newbies that may read this, the following works for me from within my Bartik sub-theme:
.field-type-addressfield .country {display: none;}

#21

Version:7.x-1.0-beta3» 7.x-1.x-dev
Category:feature request» bug report
Status:needs review» reviewed & tested by the community

Patch in #9 works fine for me. What a relief! It's embarrassing to see the country show up when showing a client even though it was configured not to show. (This aspect is a bug since the original feature request was fulfilled.)

#22

#9 works great.
RTBC++

#23

Status:reviewed & tested by the community» needs work

tried patch form #2, it doesnt seem to do anything that i can see

tried patch from #9, but what is this for?? it is trying to patch the file: address-hide-country.inc, what is that? so i took code from that patch and manually added to address.inc, same result.. no visible affect

#24

This file does not exist in the latest beta.
The patch worked fine for me against the latest dev -- are you sure you're using 7.x-1.x-dev with datestamp 1323734458 (2011-12-13)?

#25

Status:needs work» reviewed & tested by the community

yup, my bad..i had beta2, latest -dev is pretty old so i grabbed beta3 (not sure how -dev can be older than a rel; i think a bug in the new GiT system)

patch in #9 now applies cleanly and feature seems to be working as expected.. :)

#26

Hi

This is a solution for hiding only country field from address block

Step 1. Register a plugins folder in our module

<?php
/**
* Implements hook_ctools_plugin_directory().
*/
function HOOK_ctools_plugin_directory($module, $plugin) {
  if (
$module == 'addressfield') {
    return
'plugins/'.$plugin;
  }
}

/**
* Implements hook_ctools_plugin_type().
*/
function HOOK_ctools_plugin_type() {
 
$plugins['format'] = array(
   
'load themes' => TRUE,
  );
  return
$plugins;
}
?>

Step 2. Create actual folder plugins and put folder format there
Path: sites/all/modules/custom/custom_module/plugin/format/
NOTE: custom_module is a name of your custom module

Step 3. Create file and put inside format folder
custom_module-address-row.inc

<?php
/**
* @file
* Hide then country field.
*/
$plugin = array(
 
'title' => t('Hide the country field'),
 
'format callback' => 'addressfield_format_address_hide_country_view',
 
'type' => 'address',
 
'weight' => -90,
);

/**
* Format callback.
*
* @see CALLBACK_addressfield_format_callback()
*/
function addressfield_format_address_hide_country_view(&$format, $address, $context = array()) {
$format['country']['#access'] = FALSE;
}
?>

Step 4. Flush your cache
Under Manage fields you will get an extra check-box field, enable it and its done.

Reference: http://blog.artemshymko.com/format-your-address-field-correctly

Thanks

#27

Version:7.x-1.x-dev» 7.x-1.0-beta3
Status:reviewed & tested by the community» needs review

If Hide the country when only one is available option is enabled in addressfield display settings (either directly or by checking Use the same configuration as the widget there and enabling the same option in field settings), and a single country only is marked in countries list of the field instance, the country still displays. I've implemented a patch to fix this issue.

AttachmentSize
addressfield_hide_country_if_one_is_available.patch 650 bytes

#28

@ #27, your patch is not valid. it does the same as #9, but in another, unrelated file, and is more expensive.
Above all, it removes an important feature from #9, which might have happened in your system:
#9 does NOT hide the country if the entered country is different from the (only) default country. (This can be the case if you first enter some addresses, and later set the only default country and the checkbox.)

#29

@#26, what is the purpose of your proposal? is it an alternative way for the same issue, which is resolved elegantly in #9?

#30

Status:needs review» reviewed & tested by the community

Setting patch in #9 back to RTBC (approved by #19, #21, #22, #23-25, and me), since #26, #27 do not improve #9.

#31

This patch is just a very small clean-up on the RTBC #9:

- Added linebreak in } else {
- Use the preferred isset() rather than array_key_exists().

AttachmentSize
addressfield-hide-country-render-1316788-31.patch 1.08 KB

#32

Status:reviewed & tested by the community» needs work

This patch works fine for hiding the output on a node, but when the field is being output inside a vertical tab, the tab still renders empty, which would suggest that there's still something being left behind.

If a tab has no elements to render it doesn't appear, which is not the case if an addressfield field is present, even when empty.

#33

Status:needs work» needs review

I don't think that issue is specifically related to countries. It's a common issue with several field types, addressfield may be adding a wrapper div or something.

#34

Version:7.x-1.0-beta3» 7.x-1.x-dev
Status:needs review» fixed

I agree with andrewbelcher's approach and appreciate all the effort that has gone into re-rolling it and reviewing it. I had to manually apply it due to changes I committed to this function earlier today, but it's working as expected (even for a legacy address using a different country value than the current field instance settings make available).

Agreed w/ respect to the vertical tab; that sounds like something happening upstream, perhaps due to our markup wrappers or some other unknown incompatibility.

Commit: http://drupalcode.org/project/addressfield.git/commitdiff/39ff714

#35

Ok, thanks Ryan, might open another issue in that respect then to see what we can do about the vertical tab incompatability.

Thanks for all the hard work on this.

#36

2013-May-08 dev version with the #31 patch (addressfield-hide-country-render-1316788-31.patch) produces this fail:

patch < addressfield-hide-country-render-1316788-31.patch
can't find file to patch at input line 5
Perhaps you should have used the -p or --strip option?
The text leading up to this was:
--------------------------
|diff --git a/plugins/format/address-hide-country.inc b/plugins/format/address-hide-country.inc
|index 65c0660..eb59960 100644
|--- a/plugins/format/address-hide-country.inc
|+++ b/plugins/format/address-hide-country.inc
--------------------------
File to patch: /...\ Modules\ to\ Install/BATCH\ 4/addressfield/plugins/format/address-hide-country.inc
/...\: No such file or directory
Skip this patch? [y] n
File to patch: plugins/format/address-hide-country.inc
patching file plugins/format/address-hide-country.inc
Hunk #1 FAILED at 18.
1 out of 1 hunk FAILED -- saving rejects to file plugins/format/address-hide-country.inc.rej

Is this fixed in the latest release (beta4 or dev) and I'm just not seeing where the decided solution was presented? Any help would be appreciated.

#37

Per my comment #34, I cleaned this up and committed it. It's part of the beta4 release.

#38

Sorry, comment innundation here, was looking for a fix for the "need a -None- for country", sorry. Thanks!

#39

Status:fixed» needs work

I might not understand the complications or specifics of this issue and I really appreciate the hard work that is gone into this, but I don’t think it is really fixed. When I install this patch, click the don’t display-checkbox the title of the field is still displayed if I leave all fields empty. In the outputted source code an empty (country) field is rendered. So it seems it is not really empty, otherwise no field would display (and no title along with it).

This might be related to the vertical tabs issue mentioned in #32.

The HTML that is rendered is:

<div class="field field-name-field-locatie-adres field-type-addressfield field-label-above">
  <div class="field-label">
    Address:
  </div>
  <div class="field-items">
    <div class="field-item even"></div>
  </div>
</div>

#40

Status:needs work» fixed

That's a separate issue, and it's more or less working as designed. The field has a value, so Drupal expects the rendering process to produce at least some output. Even though our display formatter settings allow you to hide a part of a country field value when rendered, Drupal has already made the decision that the field has a value, and we can't come back later in the render phase and say it isn't the case. You might be able to follow along the issue to make the "empty value" conditions configurable.

#41

I see, that is an insightful explanation.

#42

Just wanted to add a conclusion to this thread so that people don't have to read it all like I did to understand what they need to do.

If you only have one country and you would like to hide the country block from your form
here is what you need to do:

Upgrade to at least beta4

Go to Store >> Client profiles >> Profile types
There you might have two profiles such as Billing info and Shipping info.
Go to the Manage fields screen and then edit the address field.

Select your single country in the multiple select of available countries.
Finally check the box "Hide the country when only one is available".

nobody click here