Hi everyone.

I have a content type with location required. I have a view that shows me all nodes of this type. I want the user to filter by country, so I have exposed the filter and everything is working fine.

Now I need to filter the country list to show only the countries that have nodes assigned.

I found this solution, apparently for D6. I'm on D7, so I need help exporting as I'm not an expert...

At the moment I'm gettin a blank page...

function custom_countries_form_alter(&$form, $form_state, $form_id) {

  // define variables
  $view_name = 'top_rated_nodes_courses_info';
  $filer_name = 'country';

  // modify country drop down
  if ($form_id == 'views_exposed_form' && is_object($form_state['view']) && $form_state['view']->name==$view_name && is_array($form[$filer_name])) {
        
    // define SQL to fetch nodes that have countries
    $sql = "
      select distinct
        l.country
      from
        {node} n
      join
        {location_instance} li on li.nid = n.nid and li.vid = n.vid
      join
        {location} l on l.lid = li.lid
      where
       n.status = 1 and n.type = 'person'
    ";
  
    // fetch results
    $resource = db_query($sql);
    $countries = array();
    while($row = db_fetch_object($resource)) {
      $countries[] = $row->country;
    }
  
    // filter country list
    foreach($form[$filer_name]['#options'] as $key => $value) {
    
      // allow "All"
      if ($key == 'All') {
        continue;
      }
    
      elseif (!in_array($key, $countries)) {
        unset($form[$filer_name]['#options'][$key]);
      }
    
    }
  
  }
}

I'm using this on a new module by the way.

Thanks !

Update:
Ok, as recommended by drupal, i changed the query to use db_select instead of db_query...

still blanck page.

<?php
/**
* Implements hook_form_alter()
*/
function custom_countries_form_alter(&$form, $form_state, $form_id) {

  // define variables
  $view_name = 'top_rated_nodes_courses_info';
  $filer_name = 'country';

  // modify country drop down
  if ($form_id == 'views_exposed_form' && is_object($form_state['view']) && $form_state['view']->name==$view_name && is_array($form[$filer_name])) {
          
    $resource = db_select('{node}', 'n');
    $resource->distinct();
    $resource->addField('l', 'country' );
	$resource->Join('{location_instance}', 'li', 'n.nid = li.nid', 'n.vid = li.vid');
	$resource->Join('{location}', 'l', 'li.lid = l.lid');
	$resource->addExpression( "WHERE(n.status='1', n.type='courses')" );

    $countries = array();
    while($row = db_fetch_object($resource)) {
      $countries[] = $row->country;
    }
  
    // filter country list
    foreach($form[$filer_name]['#options'] as $key => $value) {
    
      // allow "All"
      if ($key == 'All') {
        continue;
      }
    
      elseif (!in_array($key, $countries)) {
        unset($form[$filer_name]['#options'][$key]);
      }
    
    }
  
  }
}

Still looking for help...

Comments

pipep’s picture

I think I converted everything to D7 functions but still no luck: the page is rendered perfectly, but the list isn't filtered

<?php
/**
* Implements hook_form_alter()
*/
function custom_countries_form_alter(&$form, $form_state, $form_id) {

  // define variables
  $view_name = 'top_rated_nodes_courses_info';
  $filer_name = 'country';

  // modify country drop down
  if ($form_id == 'views-exposed-form-top-rated-nodes-courses-info-block' && is_object($form_state['view']) && $form_state['view']->name==$view_name && is_array($form[$filer_name])) {
          
    $resource = db_select('node', 'n');
    $resource->distinct();
    $resource->addField('l', 'country' );
	$resource->Join('location_instance', 'li', 'n.nid = li.nid', 'n.vid = li.vid');
	$resource->Join('location', 'l', 'li.lid = l.lid');
	$resource->addExpression( "WHERE(n.status='1', n.type='courses')" );

    $countries = array();

	foreach ($resource as $row) {	
      $countries[] = $row->country;
    }
  
    // filter country list
    foreach ($form[$filer_name]['#options'] as $key => $value) {
   
   
      // allow "All"
      if ($key == 'All') {
        continue;
      }
    
      elseif (in_array($key, $countries)) {
		 
		 $form[$filer_name]['#options'][$key] = FALSE;
		  
      }
    
    }
  
  }
}


hutch’s picture

Try something like this:

$query = db_select('node', 'n');
$query->distinct();
$query->addField('l', 'country' );
$query->join('location_instance', 'li', 'n.nid = li.nid');
$query->join('location', 'l', 'li.lid = l.lid');
$query->condition('n.status', 1)->condition('n.type', 'courses');
$resource = $query->execute();
$countries = array();
foreach ($resource as $row) {
  $countries[] = $row->country;
}
pipep’s picture

Thanks a lot hutch but still no luck... I've been 8 hours on this and can't find the problem. I've cleaned a bit more.

Any ideas are welcome

<?php
/**
* Implements hook_form_alter()
*/
function custom_countries_form_alter(&$form, $form_state, $form_id) {

  // modify country drop down
  if ($form_id == 'views-exposed-form-top-rated-nodes-courses-info-block') {
         
	$query = db_select('node', 'n');
	$query->distinct();
	$query->addField('l', 'country');
	$query->join('location_instance', 'li', 'n.nid = li.nid');
	$query->join('location', 'l', 'li.lid = l.lid');
	$query->condition('n.status', 1)->condition('n.type', 'courses');
	$resource = $query->execute();
	$countries = array();
	
	foreach ($resource as $row) {
	  $countries[] = $row->country;
	  }
 
    // filter country list
    foreach ($form['country']['#options'] as $key => $value) {
    
      // allow "All"
      if ($key == 'All') {
        continue;
      }
   
      elseif (in_array($key, $countries)) {

		$form['country']['options'][$key] = FALSE;
 
      }
   
    }
 
  }
}
hutch’s picture

Try unset($form['country']['#options'][$key]) rather than setting it to FALSE
Also note the '#' and I think you need to use elseif (! in_array($key, $countries)) {

pipep’s picture

While reading this discussion, I've already tried the three possibilities, and constantly trying when I change something else

unset($form['country']['#options'][$key]);
hide($form['country']['#options'][$key]);
$form['country']['#options'][$key] = FALSE;

You're right about the ! and the #, I probably deleted them somewhere in between.

I think the problem is on line 25,

    foreach ($form['country']['#options'] as $key => $value) {

so i'm looking for possibilities at the moment (i really need this to work)

Here's what i'm working on:

<?php
/**
* Implements hook_form_alter()
*/
function custom_countries_form_alter(&$form, $form_state, $form_id) {

  // modify country drop down
  if ($form_id == 'views-exposed-form-top-rated-nodes-courses-info-block') {
         
	$query = db_select('node', 'n');
	$query->distinct();
	$query->addField('l', 'country');
	$query->join('location_instance', 'li', 'n.nid = li.nid');
	$query->join('location', 'l', 'li.lid = l.lid');
	$query->condition('n.status', 1)->condition('n.type', 'courses');
	$resource = $query->execute();
	$countries = array();
	
	foreach ($resource as $row) {
	  $countries[] = $row->country;
	  }
 
    // filter country list

    foreach ($form['country']['#options'] as $key => $value) {
    
      // allow "All"
      if ($key == 'All') {
		continue;
      }
  
	  elseif (! in_array($key, $countries)) {
		unset($form['country']['#options'][$key]);
 
      }
   
    }
 
  }
}

Thanks again hutch.

pipep’s picture

I can't keep on this at the moment, but i'll retake it on feb. cos I think this is a must for location...

Update:

Ok, I'm back on this.

Any ideas are welcome !

pipep’s picture

Status: Active » Fixed

Holy s*** ! It worked !!!

the problem was on the form_id, as being too specific. changed to views_exposed_form solved the issue !!

Now I hope there's no problem with others forms on the site !

Here's the code for custom_countries.module (that's what I named it at least... )

/**
* Implements hook_form_alter()
*/
function custom_countries_form_alter(&$form, $form_state, $form_id) {

  // modify country drop down
  if ($form_id == 'views_exposed_form') {
        
$query = db_select('node', 'n');
$query->distinct();
$query->addField('l', 'country');
$query->join('location_instance', 'li', 'n.nid = li.nid');
$query->join('location', 'l', 'li.lid = l.lid');
$query->condition('n.status', 1)->condition('n.type', 'courses');
$resource = $query->execute();
$countries = array();

foreach ($resource as $row) {
  $countries[] = $row->country;
  }

    // filter country list

    foreach ($form['country']['#options'] as $key => $value) {
   
      // allow "All"
      if ($key == 'All') {
continue;
      }
 
  elseif (! in_array($key, $countries)) {
	unset($form['country']['#options'][$key]);

      }
  
    }

  }
}

For the record I'm on D.7.12 + location 7.x-3.x-dev + views 7.x-3.1

UPDATE:
Ok, the code above messed up all other views with exposed filters.

the solution is to specify the name of the view, so I added an extra if statement

/**
* Implements hook_form_alter()
*/
function custom_countries_form_alter(&$form, $form_state, $form_id) {

  // modify country drop down
  if ($form_id == 'views_exposed_form') {

	$view = $form_state['view'];
		if ( $view->name == 'name_of_the_view' && $view->current_display == 'display_value_of_the_view' ) {
       
		$query = db_select('node', 'n');
		$query->distinct();
		$query->addField('l', 'country');
		$query->join('location_instance', 'li', 'n.nid = li.nid');
		$query->join('location', 'l', 'li.lid = l.lid');
		$query->condition('n.status', 1)->condition('n.type', 'courses');
		$resource = $query->execute();
		$countries = array();
		
		foreach ($resource as $row) {
		  $countries[] = $row->country;
		  }
			// filter country list
		
			foreach ($form['country']['#options'] as $key => $value) {
		  
			  // allow "All"
			  if ($key == 'All') {
		continue;
			  }
		
		  elseif (! in_array($key, $countries)) {
			unset($form['country']['#options'][$key]);
		
			  }
 
    }

  }
}

}
tomaprice’s picture

Hi all,

Looking for that for a while and I'm a bit novice.

How could I use that to associate province and city? By the way, Province filter is exposed as autocomplete field and city as textfield and I'd like to turn them into 2 drop down list.

Thanks for your help!

pipep’s picture

I want to do kind of the same thing, but with a cck as second filter. I'll be looking into that in a few weeks though. If I succeed I'll post it here, so maybe it can help figuring ur specific case out.

Status: Fixed » Closed (fixed)

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

Anonymous’s picture

Issue summary: View changes

Update 1

kevin671’s picture

Hi,

I just update the location module. The code #7 does not work . Do you have a solution?

Cordially