Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Damien Tournoud’s picture

Status: Needs review » Fixed
FileSize
1021 bytes

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

ezheidtmann’s picture

Status: Fixed » Needs review
FileSize
1.71 KB

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?

ezheidtmann’s picture

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.

orlitzky’s picture

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.

markabur’s picture

Tested #2 and it works.

andrewbelcher’s picture

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.

cesareaugusto’s picture

Is this patch backported to the official release?

klonos’s picture

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

daveparrish’s picture

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

Loube’s picture

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"

orlitzky’s picture

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

Loube’s picture

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.

orlitzky’s picture

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.

Loube’s picture

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.

orlitzky’s picture

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

dlshannon’s picture

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?

orlitzky’s picture

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?

dlshannon’s picture

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.

orlitzky’s picture

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.

dlshannon’s picture

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

Sborsody’s picture

Version: 7.x-1.0-beta3 » 7.x-1.x-dev
Category: feature » bug
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.)

AaronBauman’s picture

#9 works great.
RTBC++

liquidcms’s picture

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

AaronBauman’s picture

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

liquidcms’s picture

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

makbul_khan8’s picture

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

Hi

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

Step 1. Register a plugins folder in our module

/**
* 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

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

Alex Bukach’s picture

Version: 7.x-1.x-dev » 7.x-1.0-beta3
Status: Reviewed & tested by the community » Needs review
FileSize
650 bytes

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.

johnv’s picture

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

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

johnv’s picture

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

johnv’s picture

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.

dalin’s picture

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

kiwimind’s picture

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.

dalin’s picture

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.

rszrama’s picture

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

kiwimind’s picture

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.

deanflory’s picture

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.

rszrama’s picture

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

deanflory’s picture

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

rolfmeijer’s picture

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>
rszrama’s picture

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.

rolfmeijer’s picture

I see, that is an insightful explanation.

Nicolas Bouteille’s picture

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

Status: Fixed » Closed (fixed)

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

anou’s picture

Issue summary: View changes

Just another tip so none will loose time like I did. From the address-hide-country.inc file

// Hide the country element only if the whole field is required, otherwise
// there will always be an additional None option.

I just needed to make the field required and the country field disappeared. ;-)