CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Drupal Modules (Moderator: Donald Lobo) »
  • Country and State/Province in Views exposed filters
Pages: [1]

Author Topic: Country and State/Province in Views exposed filters  (Read 19933 times)

jyee

  • I’m new here
  • *
  • Posts: 19
  • Karma: 3
Country and State/Province in Views exposed filters
September 23, 2010, 09:30:19 am
Thanks to the 3.2 udpate, country and state are available as drop down menus if you expose them as drupal views filters. 

The problem is that they don't have any of the Civi handling to limit the states by the country selected.  That means if you enable all countries, you'll have about 3700 options in the state drop down.  The attached patch modifies the /civicrm/drupal/modules/views/civicrm/civicrm_handler_filter_state.inc file so that if a country filter and state/province filter exist, the state/province will be conditional based on the country selected.

For those who don't use patches, here's the added code:
Code: [Select]
        /*
         * code to add country->state/province conditional options
         * first determine if there's a country filter on this view
         * if there is, load the country->state options into a JSON
         * object we can attach via javascript.  Remove the state
         * options set above and attach the javascript handler
         */
       
        // get the country filter
        foreach ($this->view->filter as $name=>$handler) {
            if ($handler->definition['handler'] == 'civicrm_handler_filter_country') {
                $country = $handler->field;

                $result = db_query('SELECT country_id, id, name FROM {civicrm_state_province} WHERE country_id IN (%s)', implode(', ', array_keys($handler->value_options)));
                $countries = array();
                while ($row = db_fetch_array($result)) {
                    $countries[$row['country_id']] .= '<option value="' . $row['id'] . '">' . $row['name'] . '</option>';
                }
                $c_object = json_encode($countries);
            }
        }//end foreach

       // country filter exists, remove options, add js
       if ($country) {
           $this->value_options = array();

           $js = 'var stateCountryOptions = ' . $c_object . ';
               jQuery("select[name=' . $country . ']").change( function() {
                   var selectedCountry = jQuery(this).val();
                   // get the string of options
                   var stateList = \'<option value="All">&lt;Any&gt;</option>\' + ((stateCountryOptions[selectedCountry])? stateCountryOptions[selectedCountry] : \'\');
                   jQuery("select[name=' . $this->field . ']").empty();
                   jQuery("select[name=' . $this->field . ']").html(stateList);
               });';
           drupal_add_js($js, 'inline', 'footer');
       } // end if country filter

insert on line 59 right after the if block, but before the end of the get_value_options() function

Kurund Jalmi

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4169
  • Karma: 128
    • CiviCRM
  • CiviCRM version: 4.x, future
  • CMS version: Drupal 7, Joomla 3.x
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Country and State/Province in Views exposed filters
September 24, 2010, 03:04:03 pm
It would be great if you can file an issue in jira and attach the patch.

Kurund
Found this reply helpful? Support CiviCRM

roswell2011

  • I’m new here
  • *
  • Posts: 28
  • Karma: 0
  • CiviCRM version: 4.0.4
  • CMS version: Drupal 7
  • MySQL version: 5
  • PHP version: php 5.3
Re: Country and State/Province in Views exposed filters
September 11, 2012, 09:49:00 am
Did anything come out of it?

zkrebs

  • I post occasionally
  • **
  • Posts: 69
  • Karma: 1
Re: Country and State/Province in Views exposed filters
April 29, 2013, 10:26:19 am
Any update?

petednz

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4899
  • Karma: 193
    • Fuzion
  • CiviCRM version: 3.x - 4.x
  • CMS version: Drupal 6 and 7
Re: Country and State/Province in Views exposed filters
April 29, 2013, 03:27:52 pm
Not sure if it will be approved of, but i went ahead and made a ticket and attached the above - i suspec the above will need some love before it is 4.3/4.4 viable

http://issues.civicrm.org/jira/browse/CRM-12462
Sign up to StackExchange and get free expert advice: https://civicrm.org/blogs/colemanw/get-exclusive-access-free-expert-help

pete davis : www.fuzion.co.nz : connect + campaign + communicate

ishadakota

  • I’m new here
  • *
  • Posts: 5
  • Karma: 0
Re: Country and State/Province in Views exposed filters
July 23, 2013, 06:57:29 am
Any thoughts as to how this can be applied to the Pseudo Constant filter handler introduced in 4.1?

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Support »
  • Using CiviCRM »
  • Using Drupal Modules (Moderator: Donald Lobo) »
  • Country and State/Province in Views exposed filters

This forum was archived on 2017-11-26.