Closed (fixed)
Project:
IP Geolocation Views & Maps
Version:
7.x-1.12
Component:
User interface
Priority:
Normal
Category:
Support request
Assigned:
Reporter:
Created:
28 Jun 2012 at 13:19 UTC
Updated:
27 Jul 2012 at 06:21 UTC
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
Comment #1
rdeboerHmm 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
Comment #2
rdeboerComment #3
Sinan Erdem commentedThank 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.
Comment #4
rdeboerHi 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:
<?php return $_SESSION['ip_geoloc']['location']['city']; ?>Now go back to edit your 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.
Comment #5
Sinan Erdem commentedCan 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.
Comment #6
rdeboerRe #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
Comment #7
Sinan Erdem commentedActually 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...
Comment #8
Sinan Erdem commentedAnd 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.
Comment #9
rdeboerWill be there in the next 10 minutes....
Comment #10
Sinan Erdem commentedI went on and applied the latest commit from Git myself. I get this error when I log in:
Comment #11
Sinan Erdem commentedI 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?
Comment #12
rdeboer@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.
Comment #13
Sinan Erdem commentedHello 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.
Comment #14
rdeboer