Hi,

Is there a way to populate an exposed filter field (text field) with the name of the user's city? or maybe lat/lon pair? So, the user will see the results filtered as their current city. Later, they can change the exposed filter.

Comments

rdeboer’s picture

Hmm interesting.... no not yet.
But the latest version IP Geolocation let's you center the map on the user's location, as described on the updated project page.
Wouldn't that solve your issue?
Have a look at the new Settings panel that comes with the View format "Map (via IP Geolocation)"
Rik

rdeboer’s picture

Version: 7.x-1.9 » 7.x-1.12
Assigned: Unassigned » rdeboer
Sinan Erdem’s picture

Thank you.
But the view i am using has the display option as table, not a map. i want the locations to be listed in the table for only user' city by default. Later they can change the exposed filters value to see results of another city.

rdeboer’s picture

Hi Sinan,
I believe I have something for you that may work. I'm assuming that you already have a text field, let's call it field_city, on a content type and that you have created a View for it that displays City as a field (Content: City). You'll also have an (exposed) filter for City. First step: remove this filter from your View.

Download en enable http://drupal.org/project/global_filter, dev snapshot 7.x-1.x-dev (29-Jun-2012). No need to study the README. We're going to use it in a slightly different way from what it is intended for, but it should work. Follow the instructions below:

  • At /admin/structure/block find in the "Disabled" section a block named "Global filter 1 (not configured)" . Press "configure".
  • Enter <none> in the "Block title" box.
  • In the dropdown "Choose the field to be used as a global filter" find your city fied: "Field: City (field_city)".
  • Widget: "Inherit from field"
  • Under "Alternatively specify a default through PHP code" enter this snippet:
    <?php return $_SESSION['ip_geoloc']['location']['city']; ?>
  • For the "Region setting" select "Content"
  • Finally press "Show block only on the listed pages" and enter the path to your View.
  • Click "Save block"

Now go back to edit your View.

  • In the "Advanced" fieldset on the right "Add" a Contextual Filter for "Content: City (field_city)".
  • On the next panel press "Provide default value", then for the Type select "Global filter (field or search terms)"
  • Click "Apply".
  • Save the View.

That should be it. Log out (to create a new session). Go to the URL associated with the View and it should be filtered by the user's city.
You should also see a text box there to filter the View by any city you type.

This should work for both logged-in and anonymous users. A further option is that if the same field_city is also added to the user profile at admin/config/people/accounts/fields, (e.g. "My favourite city"), then the value entered by the account holder on the "Edit account" page will become the initial value of the exposed filter. If they haven't entered anything, then their city as retrieved by Smart IP or GeoIP will be used instead, just like it is for anonymous users.

Good luck! Let us know how you went, and I'll update the IP Geolocation project page so that others can enjoy this little trick too.

PS: for this to work you need have either Smart IP or GeoIP enabled. Be aware that when testing it won't work for http://localhost or http://127.0.0.1 as these are not valid IP addresses that Smart IP and GeoIP know how to look up the city for.

Sinan Erdem’s picture

Can you just code it inside your module instead of making me deal with tons of things?!

Just kidding :) Thanks a lot for the effort. I will just try it and inform you about the results.

One small problem is that I have multiple exposed filters with the city filter. When I do this method, the city filter and the others will be separated into two blocks. But no big deal.

Thanks again.

rdeboer’s picture

Re #5:
If you have multiple City filters you have all the more reason to use Views Global Filter. This allows you to have the City set globally by your visitor somewhere on your site in a block (which you may repeat on a number of pages). Then after that every View that employs that City as a filter will get that global value automatically, until the user changes it to something else.
Rik

Sinan Erdem’s picture

Actually I have multiple filters but not for city, they are for different fields of a view. One of them is city, one of them is node title, etc...

Sinan Erdem’s picture

And I can only test the method when the latest dev release of Views Global Filter appears on the drupal.org. Currently it is from May 29.

rdeboer’s picture

Will be there in the next 10 minutes....

Sinan Erdem’s picture

I went on and applied the latest commit from Git myself. I get this error when I log in:

Notice: Undefined index: city in eval() (line 1 of /home/administrator/website/modules/php/php.module(80) : eval()'d code).
Sinan Erdem’s picture

I think using Views Global Filter is a very nice module, and I will have it in my mind to use in some other cases. But for my case above, it is a little bit complicated.

I am thinking of populating the exposed filter text field maybe in a custom module with form_alter or something. I think this code is enough for the default value:

return $_SESSION['ip_geoloc']['location']['city'];

Do you have an idea how to do it?

rdeboer’s picture

@etcetera9, #10

That's probably because you tested it out on "localhost". As mentioned in #4 it won't work for "localhost", it has to be a real IP address (and NOT 127.0.0.1).

Re #11: well maybe it is a little bit of overkill, however it does work and you don't have to program anything! In fact, I just realised that although you have put this request against the IP Geolocation module, you don't even need IP Geolocation at all! You can do it with just Smart IP and Views Global Filter. All you have to do is change the code snippet in #4 to
<?php return $_SESSION['smart_ip']['location']['city']; ?>
Again, this will return a blank or '-' for city when "localhost" is used.
Alternatively, use GeoIP API with this code snippet in #4:
<?php $location = geoip_city(); return empty($location) ? 'YOURDEFAULTCITY' : $location->city;?>
This will return 'YOURDEFAULTCITY' for "localhost".
The disadvantage of both of these methods compared to IP Geolocation is that IP Geolocation uses the visitor's real location as retrieved via HTML5 (with either Smart IP or GeoIP as a backup.

Further re #11: yes you'll need some code along those lines... You'll have to exploit a form hook or one of the Views module hooks. I'm not sure which one off the top of my head. But you'll need more code too, as you only want the substitution to take place upon first visit, right? And you can't use an empty filter box as the trigger, unless you introduce a new value to "reset" the filter (or maybe use the Better Filters module?). After that the user should be free to enter whichever city they like.... The advantage of using the solution in #4 is that Views Global Filter is both context filter aware and session-aware: it knows when somebody had just logged in and when they log off (i.e. the start of anonymous session) and only then fetches the location. At all other times it takes what the user has typed in the filter box.
I've tried without Views Global Filter, using Views with an Exposed Filter for Content:City in combination with the above PHP snippet for the Contextual Filter (for Content:City also), but it does not work.

Sinan Erdem’s picture

Hello Rik,

Thank you for the guidance. I am just getting to know better about these modules all together. I think I will work more on these, and it will result in what I try to achieve.

I will share the method here if I can correctly set up all the functionality.

Thanks again.

rdeboer’s picture

Status: Active » Fixed

Status: Fixed » Closed (fixed)

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