It would be great to have the select drop-down exposed filter for provinces instead of autocomplete textfield. In some cases it can be found to be more convenient to select the province using the mouse instead of typing some letters in the autocomplete field. Is there any way this could be possible to be done with the latest location releases?

Comments

evave’s picture

Title: Province autocomplete to dropdown select in views » Province autocomplete vs dropdown select in views
mtndan’s picture

I'm also very interested in this issue - any help would be appreciated!

raspberryman’s picture

I don't know how the module could support select dropdowns for states/provinces. With all the state provinces in the world... it could get crazy.

Anyways, I did need this functionality for a client, so here is my solution:

function select_province($element) {
  $provinces = location_get_provinces('us');
  if (!empty($provinces)) {
    while (list($code, $name) = each($provinces)) {
      $matches[$name] = $name;
    }
  }
  $element['#multiple'] = FALSE;
  $element['#type'] = 'select';
  $element['#options'] = $matches;
  unset($element['#size']);
  return $element;
}

function mysite_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'views_exposed_form':
       if ($form['#parameters'][1]['view']->name == 'myviewname') {
          $form['province']['#pre_render'][] = 'select_province';
       }
// ..etc..
dubios’s picture

Thank you very much, you're my drupal guru, that was incredibly useful.

sankar_jeya’s picture

Thanks for giving this idea will u plz tel how to work on drupal6 since i have tried tis code not working i have used this in view expose filter for location

raspberryman’s picture

The code is definitely a hack and may need some customization to get it working. The code above was used for Drupal 6. mysite_form_alter should be renamed to use your site's custom module name, myviewname should be the name of your view, location_get_provinces('us') should be set to the country you want to use, and you may need to use devel.module's dpm function to verify that $form['#parameters'][1] is your views object.

sankar_jeya’s picture

thanks ..

neokrish’s picture

I need a list box with multiselect facility for province. How do I hack this code to achieve this?

        case 'province':
          drupal_add_js(drupal_get_path('module', 'location') .'/location_autocomplete.js');
          $country = $a5['country'] ? $a5['country'] : variable_get('location_default_country', 'us');
          return array(
            '#type' => 'textfield',
            '#title' => t('State/Province'),
            '#autocomplete_path' => 'location/autocomplete/'. $country,
            '#default_value' => $obj,
            '#size' => 64,
            '#maxlength' => 64,
            '#description' => NULL,
            // Used by province autocompletion js.
            '#attributes' => array('class' => 'location_auto_province'),
            '#required' => ($a4 == 2),
          );

I tried your code by changing the above to something like this,

        case 'province':
          drupal_add_js(drupal_get_path('module', 'location') .'/location_autocomplete.js');
          $country = $a5['country'] ? $a5['country'] : variable_get('location_default_country', 'us');
          $provinces = location_get_provinces('us');
          if (!empty($provinces)) {
            while (list($code, $name) = each($provinces)) {
               $matches[$name] = $name;
            }
          }
          return array(
            '#type' => 'select',
            '#title' => t('State/Province'),
            '#multiple' = TRUE;  //since I want this to multiple select field
            '#options' = $matches;
            '#description' = NULL;
         );

I know I am making many blunders here, but any pointers on how to proceed will be greatly appreciated. I need this code for one of the projects that I am currently working on.

Thanks for any help,
neokrish

yesct’s picture

Issue tags: +location auto-complete

tagging

Xabi’s picture

Subscribing.

Drupal-Tech’s picture

subscribing

smithn.nc’s picture

Subscribing. It would make a lot of sense to have this as a standard option for Location's Views interactions. Most site visitors expect State/Province selection to be done via drop-down.

mcaden’s picture

I used this variation of raspberryman's code to make the "province" field on every view a dropdown:

(in mymodule_form_alter)


function select_province($element) {
  $provinces = location_get_provinces('us');
  $matches['Any'] = '<Any>';
  if (!empty($provinces)) {
    while (list($code, $name) = each($provinces)) {
      $matches[$name] = $name;
    }
  }
  $element['#multiple'] = FALSE;
  $element['#type'] = 'select';
  $element['#options'] = $matches;
  unset($element['#size']);
  return $element;
}

if( $form_id == 'views_exposed_form')
{
    if( isset( $form['province']) )
    {
        $form['province']['#pre_render'][] = 'select_province';
    }
}

EDIT: Added 'Any' to the filter list, otherwise it will always filter by a state. This makes filtering by state optional.

alextronic’s picture

Hey Raspberry, first of all Thanks for your code, it seems that it works for some people; I'm trying to have a State/Province dropdown in one of my Views (via Location module). I'm on D6, and have the latest versions of Location and Views 2.

But I'd really be very thankful if you could ellaborate a little more on how to use that hack since I still can't figure out how to implement it... I'm not good at custom modules but if you could provide me a very simple step-by-step on how and where to use that piece of code I'm sure I could use it.

Thanks in advance so much.

Alex.

mcaden’s picture

The general consensus is to make your own module per-site that has all your customization in it: http://drupal.org/node/206753

Then paste the function to add:

function select_province($element) {
  $provinces = location_get_provinces('us');
  $matches['Any'] = '<Any>';
  if (!empty($provinces)) {
    while (list($code, $name) = each($provinces)) {
      $matches[$name] = $name;
    }
  }
  $element['#multiple'] = FALSE;
  $element['#type'] = 'select';
  $element['#options'] = $matches;
  unset($element['#size']);
  return $element;
}

Then create a function called mymodule_form_alter, replacing mymodule in the function name with the name of the module you just created, and within that function paste:

function mymodule_form_alter(&$form, $form_state, $form_id) 
{
  switch ($form_id)
  {
    if( $form_id == 'views_exposed_form')
    {
      if( isset( $form['province']) )
      {
          $form['province']['#pre_render'][] = 'select_province';
      }
  }
}
alextronic’s picture

See next comment.

alextronic’s picture

OK I made it! ...But a new problem has arised. Bear with me for a minute.

This is my provincedropdown module:

<?php
// $Id: provincedropdown.module,v 1.1.2.4.2.5.2.14 2008/12/22 10:34:32 njt1982 Exp $
/**
 * @file
 * The Location US States Dropdown module Provides a dropdown for US states in Location forms
 */
function select_province($element) {
  $provinces = location_get_provinces('us');
  if (!empty($provinces)) {
    while (list($code, $name) = each($provinces)) {
      $matches[$name] = $name;
    }
  }
  $element['#multiple'] = FALSE;
  $element['#type'] = 'select';
  $element['#options'] = $matches;
  unset($element['#size']);
  return $element;
}
?>

...and this is what I inserted into views.module:

function provincedropdown_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'views_exposed_form':
       if ($form['#parameters'][1]['view']->name == 'name_of_the_view') {
          $form['province']['#pre_render'][] = 'select_province';
       }
}}

And now, on the exposed filter for my view, I (finally) can see a dropdown select list for the US states. But, when I select a State and click on the "Apply" button to filter the View, it goes to the URL:
http://www.example.com/?title=&province=Delaware&field_field1_value_many_to_one=All&field_field2_value_many_to_one=All (home page, of course)

So, it would be necessary to fix 2 things:

1) First item in select list: - Any -
2) On applying exposed filters, display new View correctly.

Anyone?

Thanks so much in advance.

Alex.

mcaden’s picture

Not sure why your query is messing up. I don't have that problem with the code I pasted and I don't see anything particularly wrong with your code

provincedropdown_form_alter should go in provincedropdown.module - it's a hook. By putting this function into your module hooking into views from your module. Otherwise you'd just edit the views code which would be a hack and make it a royal pain to upgrade drupal later. Additionally, if look look at my version of select_province, you'll see that I added "Any" to the options.

alextronic’s picture

I also don't know why, really.

I had to change the way of embedding the View into my custom template using this code:

<?php
$view = views_get_view('name_of_the_view','default');
$view->override_path = $_POST['q'];
$viewsoutput = $view->preview();
print $viewsoutput;
 ?>

Anyway, thank you very much.
A.

juicytoo’s picture

subscribing

edemus’s picture

i just wanted to share with others, because it took me a while to figure this
In function:

function mysite_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'views_exposed_form':
       if ($form['#parameters'][1]['view']->name == 'myviewname') {
          $form['province']['#pre_render'][] = 'select_province';
       }
// ..etc..

on line

$form['province']['#pre_render'][] = 'select_province';

$form['province'] states for filter identifier in Views. province is default which, in most cases its ok, but not for me. I had two views in bundled in Quicktab tabs, and both had filter identifier set to "province" which happen to work with only one view, the other didnt work, which causes unwanted results.
The thing is to use different filter identifiers, but only if you plan displaying more than one view on the same page simultaneously.
For the record :

function mysite_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) {
    case 'views_exposed_form':
       if ($form['#parameters'][1]['view']->name == 'myviewname_1') {
          $form['filter_identifier_1']['#pre_render'][] = 'select_province';
       }
       if ($form['#parameters'][1]['view']->name == 'myviewname_2') {
          $form['filter_identifier_2']['#pre_render'][] = 'select_province';
       }
// ..etc..

Hope this helps someone

edit: Forgot to thank to author. You really saved my day.
Emil

compujohnny’s picture

This Code did not work for me in case the user just clicked apply without selecting anything, province "Any" returns empty

function select_province($element) {
  $provinces = location_get_provinces('us');
  $matches['Any'] = '<Any>';
  if (!empty($provinces)) {
    while (list($code, $name) = each($provinces)) {
      $matches[$name] = $name;
    }
  }
  $element['#multiple'] = FALSE;
  $element['#type'] = 'select';
  $element['#options'] = $matches;
  unset($element['#size']);
  return $element;
}

I fixed it by changing

$matches['Any'] = '<Any>';

to

$matches['All'] = '<Any>';

Here is the fixed code

function select_province($element) {
  $provinces = location_get_provinces('us');
  $matches['All'] = '<Any>';
  if (!empty($provinces)) {
    while (list($code, $name) = each($provinces)) {
      $matches[$name] = $name;
    }
  }
  $element['#multiple'] = FALSE;
  $element['#type'] = 'select';
  $element['#options'] = $matches;
  unset($element['#size']);
  return $element;
}
czeky’s picture

Hi, this is very helpful post, can someone explain, how to identify and use fields? I'd like to override exposed view.. I need to limit exposed operator to "is less than or equal" or "is greater or equal"

thanx

Anonymous’s picture

Should this feature request be updated to Version 6.x?

-subscribing-

kruser’s picture

Subscribing - Looking for a D6 fix.

clashar’s picture

dropdown is better for sure
subscribe

clashar’s picture

Version: 5.x-3.x-dev » 6.x-3.x-dev
edmund.kwok’s picture

Status: Active » Needs review
StatusFileSize
new3.26 KB

Ah, it just so happens that a client's site would benefit from this feature so I went around coding this. Attached patch adds an extra option for the Province filter - either Dropdown or Autocomplete. Similar to how Taxonomy: Term filter works.

Please test :)

clashar’s picture

edmund.kwok,

I applied patch to "location_handler_filter_location_province.inc".

Dropdown and autocomplete options now appears on filter creation form.

But if dropdown is chosen, then the filter is not displayed on the result page, nor even on preview. Even if I change it autocomplete, filter is not displayed.
So I should delete created dropdown filter, save view and then create new autocomplete filter.

bryancasler’s picture

subscribe

Anonymous’s picture

I applied the patch but can't figure out where the "Dropdown or Autocomplete" configuration option is.

Tried admin/content/types/.../fields and admin/settings/location but nothing there. Can you specify where is this option?

Thank you.

pog21’s picture

I can't find it either. Does anybody know where it is?

BrockBoland’s picture

Status: Needs review » Reviewed & tested by the community

Patch in #28 looks good and works for me.

For those looking for the option it adds: this is only in Views. If you already have the Province filter enabled in your View, click the gear icon to the right of it to toggle between the Dropdown and Autocomplete options.

clashar, it sounds like you need to edit the filter and click the Expose button to show the dropdown. Only exposed filters are shown in the preview and on the actual view.

pog21’s picture

Thanks BrockBoland. Patch in #28 now working.

But there's one more thing. Is there a way to update the province list when a country is selected? At the moment the province field does not change when I select a country. Instead I have to choose the country, click submit, then choose a province.

stoptime’s picture

Patch seems to be working - thank you very much!

alb’s picture

regards the patch;
the province is filled with the correct values only after click the send button (so only after the page is reloaded );
this isn't good,
is possible to fill the select's province in the moment that is selected a country?

see that province's values are read in the database and this is slow;

there is already a solution for read the value not in a db;
I think better solution is read by a js file;

is possible to do this?

pog21’s picture

Status: Reviewed & tested by the community » Active

Changed to active. Not sure if this is correct usage, so let me know if I should start a new issue instead.

mototribe’s picture

subscribe

agileware’s picture

Title: Province autocomplete vs dropdown select in views » Add option to have the views province filter as a select field
Status: Active » Needs work

Needs work as there is a valid patch in #28 that needs a little extra.

Seems like it would just need a fix for the issue mentioned in #34 & #36.

agileware’s picture

Here is the patch from #28 with a minor change (the default value of the select field).

Still doesn't fix the issue mentioned in #34 & #36 though.

agileware’s picture

Still having a problem on initial load where you get the error:
"An illegal choice has been detected. Please contact the site administrator." because the value is empty.

bryancasler’s picture

Thanks so much for doing this Agileware. Do you have any plans to patch against the D7 version?

see15_aug’s picture

Thanks it's working for me.

For other countries use iso abbreviation for country name at
$provinces = location_get_provinces('countryname');

thepanz’s picture

Assigned: thepanz » Unassigned

I'm coding a fix for #34 : AJAX fetching of provinces on Country change... based on Agileware patch.
I'll post it in a few days...

thepanz’s picture

Assigned: Unassigned » thepanz
ankur’s picture

Assigned: Unassigned » thepanz

@thePanz

You wouldn't be open to also posting a 7.x-3.x version of the patch, would you? :-)

I'd review and commit it if you were.

thepanz’s picture

Hi @ankur ..are you the Ankur I "stole" TrackBack module from? ;)

My edits to 6.x-3.x version aren't big, I think porting to Views 3 should not be so hard.. even if I never worked with Views3 yet!
I'll post my patch in few days

thepanz’s picture

Status: Needs work » Needs review
StatusFileSize
new9.19 KB

As promised: the attached patch allows to have a SELECT field in a View filter.
The Patch includes my other fix for #1443240: Province SELECT auto-update not working without clean-urls.

Hope Ankur will commit it :)

Unfortunately I don't have time to port it onD7 or Views3 :(

Johnny vd Laar’s picture

For drupal 7 I created an extra views filter that is a child of the allready existing province filter in a separate module.

So make sure that in your module you have this in your info file:
files[] = includes/location_handler_filter_location_province_select.inc

Make sure that you have this in your hook_views_data

  $data['location']['province_select'] = array(
    'title' => t('Province (dropdown)'),
    'help' => t('The province of the selected location.'),
    'filter' => array(
      'field' => 'province',
      'handler' => 'location_handler_filter_location_province_select',
    ),
  );

This gives you a new views filter

pog21’s picture

Thanks for the patch in #48. But having a bit of trouble applying with terminal (mac). Haven't applied many patches this way before, but seems like a lot of changes to do manually. I get this output:

$ patch < 0001-Issue-330066-by-ThePanz-Add-option-to-have-the-views.patch 
patching file location_handler_filter_location_country.inc
Hunk #1 succeeded at 34 (offset -44 lines).
patching file location_handler_filter_location_province.inc
Hunk #1 FAILED at 11.
Hunk #2 succeeded at 18 with fuzz 2 (offset -2 lines).
Hunk #3 FAILED at 46.
2 out of 3 hunks FAILED -- saving rejects to file location_handler_filter_location_province.inc.rej
can't find file to patch at input line 131
Perhaps you should have used the -p or --strip option?
The text leading up to this was:
--------------------------
|diff --git a/location.module b/location.module
|index 6bf1240..ad95864 100644
|--- a/location.module
|+++ b/location.module
--------------------------
File to patch:

Is there something I should be doing differently since it changes 4 files?

Cheers,
Oliver

ionz149’s picture

@pog21 I was having the same problem and I was able to apply the patch successfully by using -p1

ie: patch -p1 < filename.patch

it was on this page: http://drupal.org/node/707484

pog21’s picture

@ionz149 Thanks! That was it.
Oh, and thanks to thePanz, too. :)

see15_aug’s picture

Title: Add option to have the views province filter as a select field » a
Version: 6.x-3.x-dev » 7.x-5.x-dev

a

see15_aug’s picture

Status: Needs work » Needs review

Title: a » On click star add points
Project: Location » Fivestar
Version: 7.x-5.x-dev » 7.x-2.0-alpha2
Component: Location_views » Code
Assigned: thepanz » see15_aug
Status: Needs review » Needs work

The last submitted patch, 0001-Issue-330066-by-ThePanz-Add-option-to-have-the-views.patch, failed testing.

BrockBoland’s picture

Title: On click star add points » Add option to have the views province filter as a select field
Project: Fivestar » Location
Version: 7.x-2.0-alpha2 » 6.x-3.x-dev
Assigned: see15_aug » Unassigned
Status: Needs review » Needs work
BrockBoland’s picture

Component: Code » Location_views
32i’s picture

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

@thePanz I took latest location 7.x-3.x-dev versino and manually added changes suggested by you and managed to have the province as dropdown. However, I did not found some of JS changes - actually haven't looked at it too much. I'll test if it's working ok in next few days and I guess I'll provide a patch for 7.x

Thanks for your work, anyway.

mrweiner’s picture

@32i: A patch for 7.x would be greatly appreciated. :)

32i’s picture

Status: Needs work » Needs review
StatusFileSize
new3.38 KB

Please find attached, Province/state filter as dropdown for 7.x.
I used location 7.x-3.x-dev (released April 21), however patch should apply to any 7.x version.

mrweiner’s picture

@32i: Patch works perfectly. Thank you so much!

simon_s’s picture

@32i: Patch #60 works also fine applied on 7.x 3.x-dev from 2012-May-05. Thanks!!!

energee’s picture

StatusFileSize
new3.54 KB

Rerolled against latest production release (July 25th 2012)

w00zle’s picture

StatusFileSize
new3.47 KB

I had two issues with the patch from post 63:

1. The file path was hard coded to /Drupal/sites/all/modules/contrib/location/handlers/location_handler_filter_location_province.inc, so cloning the repo and then applying the patch failed the first time I tried.

2. The "Allow Multiple Selections" value for the exposed filter was not being passed, so you could only filter by one value at a time.

I believe I have fixed both of these issues in this patch. Please let me know if anything looks wrong or doesn't work.

ZoodaVex’s picture

Version: 7.x-3.x-dev » 7.x-3.0-alpha1
Category: feature » support

Thank you for the patch, it works fine for me on 7.x-3.0-a1.
I noticed, however, that Better Exposed Filters does not recognize the new province dropdown as a dropdown that may be "bettered", but treats it as an autocomplete. Is there a way to make BEF recognize province dropdown?

mattbk’s picture

Category: support » feature

Did this patch ever get incorporated into the module?

podarok’s picture

Version: 7.x-3.0-alpha1 » 7.x-3.x-dev
Status: Needs review » Active

#1931088: [META] Fixing tests tests were broken, so triggering to active

podarok’s picture

Status: Active » Needs review

bot

podarok’s picture

Status: Needs review » Needs work

For get this feature commited I have to know what patch is the TRUE
please, point me to it

yaelsho’s picture

Hello,
Did someone succeed to resolve the issue mentioned in #36?
Thanks, Yael

yaelsho’s picture

Anyone?

lukio’s picture

Hi podarok!

I apply the patch #64 against 7.x-3.0-rc3 and the patch pass and works. Hope you can commit the patch.

comradebeck’s picture

Status: Needs work » Needs review
podarok’s picture

Status: Needs review » Fixed

#64 commited pushed to 7.x-3.x-dev
Thanks!!

comradebeck’s picture

When you show the exposed filter form on a block, the ajax to call corresponding provinces from a selected country is not working.

Any thoughts why????

Status: Fixed » Closed (fixed)
Issue tags: -location auto-complete

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

pog21’s picture

I have exposed filters in views, one of the views in a panel the other just a view page. In both cases, the form needs be updated after choosing a country before the province field updates.

Reopening this issue, but I did get a bit confused about the goal of this issue halfway down, so if the current issue is only about adding drop down functionality and not making it automatically update when a country is chosen, let me know and I'll create a new issue. If the automatic update part has been addressed/fixed, I'd also like to know as it's not working for me! :)

Using 7.x-3.x-dev.

pog21’s picture

Issue summary: View changes
Status: Closed (fixed) » Active
podarok’s picture

Status: Active » Closed (fixed)

Better create follow-up child issue
This one is from 2008... Long time ago...