Question

I need to be able to only display certain content/nodes based on where a user is located in the world. For example, if the user is from the US, they should see different results than someone from Europe.

Ideally I would be able to tag each node with the region(s) that it should be displayed in and then when the content is displayed, it is filtered by these tags.

I'm also going to be using various content 'scrollers' to display content and I would like these to have different content based on location as well.

How would I best achieve this?

Was it helpful?

Solution

Use Views - and cool modules like:

  1. Use modules like smart_ip http://drupal.org/project/smart_ip & Ip Geolocation http://drupal.org/project/ip_geoloc
  2. Read the info on those module pages they will help you get on the right track

Basically you want to first get the location of the user, this will be stored in the session. once you have that you can reference it any time.

OTHER TIPS

I know this is an old thread, but it's one that doesn't necessarily have an easy solution in Drupal. Here's how I did it. This solution doesn't match the original question exactly, but this explains a method that could be extended to cover that scenario by extending the PHP code to compare the user's location to the taxonomy term(s) assigned to each node.

First, install IP2Country. This allows you to match an IP address to someone's country by doing a geolocation lookup.

Second, use PHP code like this to determine if the visitor's IP address matches a certain country. This example returns true if the visitor is in the US or Canada, and false if they are not.

<?php
    $detectedcountry = module_invoke('ip2country','get_country',$_SERVER['REMOTE_ADDR']);
    $countries = array(
        'US','CA'
    );
    if(in_array($detectedcountry,$countries)){
        return true;
    }
    else return false;
?>

You can use this code in various places, but you will need to enable the PHP filter module for it to work.

In my solution, I used this code in two ways:

First, I created a Panels Variant for a Node Template (could be a Taxonomy Term template as well) to return a HTTP response rather than a Panel Pane. I used this code as a selection rule, so it would return a 404 if the user was not in a country that was allowed to see that specific node type. I then created Panels variant that returned the node page for anyone who qualified based on their location.

Second, I created a Panels variant for a node type (Product, in this case) and used the same PHP code to hide certain elements on the page for those users who weren't allowed to see them because of their location. In this case, I hid the Price and Add to Cart panes on a product display page. Visitors outside the allowed countries only saw the product information, and not the ecommerce elements on the page.

As a third option, you could also use it to show/hide Drupal blocks using the same code.

Finally, I created a custom theme function so I could show/hide individual menu items depending on a person's location. In this case, I wanted to hide the Shop menu item unless you were in the permitted countries. I wrote about how to do that in more detail here: https://fiveminutelessons.com/learn-drupal-sitebuilding/show-or-hide-menu-item-drupal-7-based-users-location

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top