What I'm trying to do is to automatically select the default term in the "add content" page based on the user's location in the site.

Example: User is currently viewing classifieds listed under "Los Angeles", which is in "California", which is in the "Locations" vocabulary.
I want to automatically set the default value of the "Locations" vocabulary shown in the the content form for new classifieds to the term that identifies the area they are viewing.

Is this possible?

NOTE: I'm not referring to GeoIP or locations-based modules. I'm just looking for a way to automatically select the term they are already viewing, and use it for the node being created.

  • Is the "Add Classified" a button, or a link, or a menu item? I think the technique would likely be different depending on how they are getting to the "Add Content" form. – Chaulky Apr 14 '11 at 22:18
  • I suppose it could be any? It does need to be dynamic though, since there will be hundreds of locations. Right now it's a menu link, but it probably makes more sense to have it as a link on the listings page they are viewing. – Garrett Apr 14 '11 at 22:41

I think the answer will be to make your button/menu item dynamic using arguments from the URL. It sounds like when a user clicks the "Add Classified" they are already viewing the classifieds tagged with a particular location taxonomy term. In which case, I'd imagine that term name (or id) appears in the current url, something like http://yoursite.com/classifieds/LosAngeles. If so, I believe you can pull that term out of the url and use it in your link, something like http://yoursite.com/node/add/classified/LosAngeles. Then, you'd need to alter your 'Add Content' form to pull in the term from the url (the LosAngeles part). This should work in theory, however I'm not sure exactly how to do that in practice. I'll see if I can figure it out before you end up answering your own question, but that should at least help point you in the right direction for now.


You could use the smart_ip module to determine the users location and then hook the form to modify the selected taxonomy term with hook_form_alter() / form api reference.Smart ip can use either W3C Geolocation API which isn't 100% browser compatible and accurate or maxminds geo databases. Maxmind offers a free Geolite database but if you want precision you should use the commercial geoip database.

  • Sorry, that's not entirely what I meant. I mean that I want to use the "current" term display. As in, since they are viewing the "Los Angeles" page, if they hit "Add Classified" I want it to default to "Los Angeles" (or whatever page they where viewing). I don't really care where they are in the world, per se. – Garrett Apr 14 '11 at 21:43
  • You could set the users active viewing location in their session or even cookie. Cookie may be the better option because they might not choose to add a classified until their next visit to the site. Then you can use hook_form_alter to auto populate the location vocabulary with either their session or cookie value. – Jepedo Apr 15 '11 at 0:04

You can probably put a link saying Add classified with the intented term appended to the URL as an argument, like yourdomain/?q=node/add/content_type/. Now you can edit the field settings, so that you can write a PHP snippet for the default value field that fetches this term from the URL. Hope this would work for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.