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
Comment #1
pipep commentedI think I converted everything to D7 functions but still no luck: the page is rendered perfectly, but the list isn't filtered
Comment #2
hutch commentedTry something like this:
Comment #3
pipep commentedThanks 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
Comment #4
hutch commentedTry
unset($form['country']['#options'][$key])rather than setting it to FALSEAlso note the '#' and I think you need to use
elseif (! in_array($key, $countries)) {Comment #5
pipep commentedWhile 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,
so i'm looking for possibilities at the moment (i really need this to work)
Here's what i'm working on:
Thanks again hutch.
Comment #6
pipep commentedI 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 !
Comment #7
pipep commentedHoly 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... )
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
Comment #8
tomaprice commentedHi 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!
Comment #9
pipep commentedI 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.
Comment #10.0
(not verified) commentedUpdate 1
Comment #11
kevin671 commentedHi,
I just update the location module. The code #7 does not work . Do you have a solution?
Cordially