Is there a way of choosing which countries to display? I would like to have only a predefined set of countries to be displayed everywhere including Views filters. Currently it shows all the countries which is in my case too much.

Thanks!
Javier

Comments

blueblade’s picture

i would like to know how to do this too =)

j0rd’s picture

+1 needing to know how to do this. I can't figure it out either.

marqpdx’s picture

I would like to limit countries to only those which are part of the dataset the view represents. Say we have 100 nodes to search from, and they are hotels in 30 counties, and i want to expose the Location (Country/City). can we limit the country's in the exposed filter dropdown only to the 30 in the dataset, instead of all of them??

thanks,
m

rooby’s picture

In the function location_get_iso3166_list in location.inc is the list of countries.

You can comment out the ones you don't want. Just put // at the start of the lines with the countries you don't want in the list.

rooby’s picture

Title: How to limit the countries displayed in the dropdown » Give administrators the ability to limit country options
Version: 6.x-3.1-rc1 » 6.x-3.x-dev
Component: Miscellaneous » Code
Category: support » feature

Changing this to a feature request.

Administrators should be able to select, from the list of all countries, which countries will be options in the country field.

solona’s picture

Thank you, this helped.

toemaz’s picture

ambientdrup’s picture

These are great tweaks/fixes but they don't solve for this case:

What if we want to allow admins to post both international and US locations using the drop down (for country), but only allow our site visitors to search via an exposed location country drop down that only shows the international locations (but not the US). I have 2 exposed block Views - one for searching US state and one for searching International. So on the international one I want all the countries to show up except for United States.

Can we just remove one of the countries from the exposed View?

-backdrifting

toemaz’s picture

@backdrifting hook_form_alter will save your day

ambientdrup’s picture

Ok, cool. I'd stick the form_alter in the Views module correct? Or would I add it to location?

I only want to alter the output of the exposed drop down form in the view.

-backdrifting

ambientdrup’s picture

Will hook_form_alter work even though it's a drop down select list?

<select name="country" class="form-select location_auto_country location_auto_join_country" id="edit-country" >

-backdrifting

ambientdrup’s picture

Ok I've added the following to my template.php file in the theme folder but I can't get the print_r to work:

/**
 * Implements HOOK_theme().
 */
function themename_theme(&$existing, $type, $theme, $path) {
  return array(
    // The form ID.
    'views_exposed_form_find_sales_rep_international_block_1_form' => array(
      // Forms always take the form argument.
      'arguments' => array('form' => NULL),
    ),
  );
}

and this is the function to print_r:

/**
   * Theme override for views exposed country drop down list
   */
  function themename_views_exposed_form_find_sales_rep_international_block_1_form($form) {
    return print_r($form); 
  }

What am I doing wrong here? I'm pretty sure this is the name of the View block form - I got this from the form id in view source.

-backdrifting

toemaz’s picture

Here is an example with hook_form_alter for exposed filter


function hook_form_alter(&$form, $form_state, $form_id) {
  
  // remove all countries except for be and nl from location module
  if (isset($form['country'])) {
    foreach ($form['country']['#options'] as $key => $value) {
      if ($key != 'be' && $key != 'nl' && $key != 'All') {
        unset($form['country']['#options'][$key]);
      }
    }
  }

}

ambientdrup’s picture

Ok, I'll try this. So this should go in the views.module file? Or should I create a custom module file for it?

-backdrifting

ambientdrup’s picture

The problem is - is this going to override and make tweaks to the country location form used in the location module as part of the create content form? I just want to remove a country from the exposed view filter but not from the location module country select list.

-backdrifting

toemaz’s picture

Add it in a custom module 'yoursite'.module and no, it will only affect your exposed views. Just give it a try.

ambientdrup’s picture

Cool. Ok, I'll try it. Thanks so much.

-backdrifting

ambientdrup’s picture

Hmmm ... I've uploaded the module and tried this but it's not affecting the exposed filter. All countries are still showing.

-backdrifting

mandclu’s picture

Subscribing. It would be great to have an easy way to do this.

Anonymous’s picture

+1
tried the function in a custom module but countries are still showing. maybe I need to flush some caches (although I am in theme devel mode and no caching)

selecting the available countries on Location CCK would be a good improvement. I guess this hack will limit the countries in ALL Location select inputs? I just need it in a single node field.

IMHO Location CCK is powerful but still not flexible enough. In my wish-list: select list for provinces and auto-fill with zip code entered, out of the box.

kndr’s picture

Status: Active » Needs review
StatusFileSize
new3.49 KB
new62.81 KB
new6.75 KB

In my opinion, abillity to limit countries is very usefull and sometimes neccessary. I am attaching the patch with my own proposal.

aniebel’s picture

Tested the patch in #21. I was able to enter my chosen countries in the Admin interface however, I was unable to view ONLY those countries. It's still showing everything in both the Views Proximity Filter list and on the resulting View.

kndr’s picture

Status: Needs review » Needs work

Indeed, #21 wasn't designed to work with views.

awasson’s picture

Subscribing: Would like to limit countries to those available in the existing nodes. So if there are no nodes "located" in Japan, Japan will not be in the location: country filter but as soon as I create a node that is "located" in Japan, it (Japan) will become available in the filter.

Anonymous’s picture

subscribing - I would like to just show US and Canada in the country list for Content Profile.

Anonymous’s picture

I know this doesn't give admins the ability to easily change the list of available countries...but it is a way to achieve it easily. With very little headache I implemented this on one of my sites.

http://drupal.stackexchange.com/questions/4559/how-to-alter-country-drop...

awasson’s picture

Hey, thanks for sharing that link.

The article shows how you can create a simple module that populates the drop down menu that you specify with a tailor made list. It's actually pretty neat but it would be even more useful if it were extended to loop through the database and populate the drop-down with only countries that are relevant to the content in question.

I don't (think I) have the time right now to explore that at the moment but I think it could work and I'll investigate a little more as soon as I can free up some time.

Andrew

iRex’s picture

subscribing

NewZeal’s picture

Version: 6.x-3.x-dev » 6.x-3.1

I found that in hook_form_alter, the country selects are not visible and used the following hook instead (Make sure the weight of your module is greater than the weight of the location module):

function MYMODULE_locationapi(&$obj, $op, $a3 = NULL, $a4 = NULL, $a5 = NULL) {
  switch ($op) {
    case 'field_expand':
     switch ($a3) {
       case 'country':
          // Force default.
          if ($a4 == 4) {
            return array(
              '#type' => 'value',
              '#value' => $obj,
            );
          }
          else {
            $options = array('' => t('Please select'), 'ca' => 'Canada', 'us' => 'United States');
            return array(
              '#type'           => 'select',
              '#title'          => t('Country'),
              '#default_value'  => 'ca',
              '#options'        => $options,
              '#description'    => NULL,
              '#required'       => ($a4 == 2),
              // Used by province autocompletion js.
              '#attributes'     => array('class' => 'location_auto_country'),
            );
          }
          break;
	  }
  }
}

The same hook can be used to change a lot of other features of the module such as the field labels for the form. See the function location_locationapi().

legolasbo’s picture

Issue summary: View changes
Status: Needs work » Closed (outdated)

Closing old D6 issues as D6 is end of life