Display downloadable files based on the country location of the visitor of a Drupal site's content

This article will show how to display downloadable files based on the country location of the visitor of a Drupal 7 site's content. We will take a sample application where the website's contents will have two types of downloadable files; first type should be displayed only for visitors from Philippines and the second type should be displayed only for visitors from Singapore with the use of Smart IP, Field Collection and Location contributed modules. Smart IP module is used to identify Drupal site visitor's geographical location based on his IP address; the Field Collection module will be used to create two separate fields which represents the two types of downloadable files, each of these two Field Collection fields will contain file field and location field; and Location module is used to provide the location field.

Steps:

  1. Install Smart IP, Smart IP Views Bridge, Field Collection, Entity API, Location, Location CCK, Ctools, EVA, Views and Views UI modules. For debug purposes, enable Device Geolocation module.

  2. Setup a content type or use an existing content type (in this example we will use the existing "Basic page" content type) and add a new field with "Field collection" field type at http://www.yoursite.com/?q=admin/structure/types/manage/page/fields. Populate the field label with "Download PH". Click "Save" button.

    Add field_collection Download SG

    Use the default values for succeeding setup settings of this field.

  3. Add another new field with "Field collection" field type at http://www.yoursite.com/?q=admin/structure/types/manage/page/fields. Populate the field label with "Download SG". Click "Save" button.

    Add field_collection Download PH

    Use the default values for succeeding setup settings of this field.

  4. Now we will add file field and location field to each "Download PH" and "Download SG" Field Collection fields. Go to "Field collections" settings at http://www.yoursite.com/?q=admin/structure/field-collections.

    field_collection settings

    Click "manage fields" of "field_download_ph".

  5. Add a new field with "File" field type. Populate the field label with "Product Manual". Select "File" from Type of data to store and click "Save" button.

    field_collection Download PH add file field

    Use the default values for succeeding setup settings of this field.

  6. And add a new field with "Location" field type. Populate the field label with "Country". Select "Location" from Type of data to store and click "Save" button.

    field_collection Download PH add location field

    At "Location" Field Settings > Locative Information > Collection Settings select "Allow" for the "Country" items and click "Save field settings" button.

    field_collection Download PH location field settings

  7. Lets do the "Download SG" Field Collection field. Go to "Field collections" settings at http://www.yoursite.com/?q=admin/structure/field-collections and click "manage fields" of "field_download_sg". This time we will use the existing file field and location field we have created for "Download PH" Field Collection field. On "Field to share" dropdown menu, select "File: field_product_manual (Product Manual)" option and click "Save" button.

    field_collection Download SG file field

    Use the default values for succeeding setup settings of this field.

  8. Now for the location field, select "Location: field_country (Country)" on "Field to share" dropdown menu and click "Save" button.

    field_collection Download SG location field

    Use the default values for succeeding setup settings of this field.

  9. We will create a view and use the Smart IP country views filter to display the appropriate downloadable file available for visitors from Philippines and Singapore. We will use EVA module will to expose this view as entity field to our "Basic page" content type. Go to http://www.yoursite.com/?q=admin/structure/views/add and populate "View name" with "Product Manual field". Select "Basic page" for "type", uncheck the "Create a page" and click "Continue& edit" button.

    Create view entity field

  10. Remove everything in "Filter Criteria" and "Sort Criteria".

    Views master display

  11. Add "EVA field" display and remove from the "Fields" the "Content: Title" field then add "Content: Download PH" in Fields. Click "Apply (this display)" button.

    Add Download PH field

  12. Select "This entity_view (override) from "For" dropdown menu at the top left and select the "Fields only" from "Formatter" dropdown menu.

    Views Download PH field formatter settings

    Scroll down and collapse the "No Results Behavior" then tick the "Hide if empty". Click "Apply (this display)" button.

    Views Download PH field No Results Behavior settings

  13. Collapse the "Advanced" and add "Content: Download PH (field_download_ph)" in "Relationship". Click "Apply (this display)" button. Make sure to select "This entity_view (override)" from "For" dropdown box at upper left.

    Views Download PH add relationship

  14. Tick the "Require this relationship" checkbox and click "Apply (this display)" button.

    Views Download PH relationship settings

    This should make the "Field Collection item" available in "Relationship" options.

  15. Add "Field collection item: Country (field_country)" in "Relationship". Click "Apply (this display)" button.

    Views Download PH add location relationship

  16. Tick the "Require this relationship" checkbox and click "Apply (this display)" button.

    Views Download PH location relationship settings

  17. Under "Filter Criteria" add "Location: Country".

    Views Download PH add location filter

  18. Under the "Operator" select "Is", select "Smart IP: visitor's country code" item from "Country" dropdown box and click "Apply (this display)" button. Make sure to select "This entity_view (override)" from "For" dropdown box at upper left.

    Views Download PH add location filter settings

  19. In "Entity Content Settings", change the "Entity type" to "Node".

    Views Download PH entity content settings

  20. Save your view.

    Views Download PH display settings

  21. Now create another "EVA field" display and remove from the "Fields" the "Content: Title" field then add "Content: Download SG" in Fields. Click "Apply (this display)" button.

    Add Download SG field

  22. Select "This entity_view (override) from "For" dropdown menu at the top left and select the "Fields only" from "Formatter" dropdown menu.

    Views Download SG field formatter settings

    Scroll down and collapse the "No Result Behavior" then tick the "Hide if empty". Click "Apply (this display)" button.

    Views Download SG field No Results Behavior settings

  23. Collapse the "Advanced" and add "Content: Download SG (field_download_sg)" in "Relationship". Click "Apply (this display)" button. Make sure to select "This entity_view (override)" from "For" dropdown box at upper left.

    Views Download SG add relationship

  24. Tick the "Require this relationship" checkbox and click "Apply (this display)" button.

    Views Download SG location relationship settings

    This should make the "Field Collection item" available in "Relationship" options.

  25. Add "Field collection item: Country (field_country)" in "Relationship". Click "Apply (this display)" button.

    Views Download SG add location relationship

  26. Tick the "Require this relationship" checkbox and click "Apply (this display)" button.

    Views Download SG location relationship settings

  27. Under "Filter Criteria" add "Location: Country".

    Views Download SG add location filter

  28. Under the "Operator" select "Is", select "Smart IP: visitor's country code" item from "Country" dropdown box and click "Apply (this display)" button. Make sure to select "This entity_view (override)" from "For" dropdown box at upper left.

    Views Download SG add location filter settings

  29. In "Entity Content Settings", change the "Entity type" to "Node".

    "Views Download SG entity content settings

  30. Save your view.

    Views Download SG display settings

  31. Go to manage display of "Basic page" at http://www.yoursite.com/?q=admin/structure/types/manage/page/display and move the "Download PH" and "Download SG" fields to hidden.

    Basic page hide original field_collection fields

  32. To test, create a "Basic page". Populate the "Title"; Download PH's file and country; and Download SG's file and country. Save your sample content.

    Create sample content

  33. The "Download PH" field should only display to visitors from Philippines and the "Download SG" should be hidden.

    Test result

The following codes are the Views export of this example application:

  
$view = new view();
$view->name = 'product_manual_field';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'Product Manual field';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'perm';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['style_plugin'] = 'default';
$handler->display->display_options['row_plugin'] = 'fields';
/* Field: Content: Title */
$handler->display->display_options['fields']['title']['id'] = 'title';
$handler->display->display_options['fields']['title']['table'] = 'node';
$handler->display->display_options['fields']['title']['field'] = 'title';

/* Display: Download PH Field */
$handler = $view->new_display('entity_view', 'Download PH Field', 'entity_view_1');
$handler->display->display_options['defaults']['relationships'] = FALSE;
/* Relationship: Content: Download PH (field_download_ph) */
$handler->display->display_options['relationships']['field_download_ph_value']['id'] = 'field_download_ph_value';
$handler->display->display_options['relationships']['field_download_ph_value']['table'] = 'field_data_field_download_ph';
$handler->display->display_options['relationships']['field_download_ph_value']['field'] = 'field_download_ph_value';
$handler->display->display_options['relationships']['field_download_ph_value']['required'] = TRUE;
$handler->display->display_options['relationships']['field_download_ph_value']['delta'] = '-1';
/* Relationship: Field collection item: Country (field_country) */
$handler->display->display_options['relationships']['field_country_lid']['id'] = 'field_country_lid';
$handler->display->display_options['relationships']['field_country_lid']['table'] = 'field_data_field_country';
$handler->display->display_options['relationships']['field_country_lid']['field'] = 'field_country_lid';
$handler->display->display_options['relationships']['field_country_lid']['relationship'] = 'field_download_ph_value';
$handler->display->display_options['relationships']['field_country_lid']['required'] = TRUE;
$handler->display->display_options['defaults']['fields'] = FALSE;
/* Field: Content: Download PH */
$handler->display->display_options['fields']['field_download_ph']['id'] = 'field_download_ph';
$handler->display->display_options['fields']['field_download_ph']['table'] = 'field_data_field_download_ph';
$handler->display->display_options['fields']['field_download_ph']['field'] = 'field_download_ph';
$handler->display->display_options['fields']['field_download_ph']['hide_empty'] = TRUE;
$handler->display->display_options['fields']['field_download_ph']['type'] = 'field_collection_fields';
$handler->display->display_options['fields']['field_download_ph']['settings'] = array(
  'view_mode' => 'full',
);
$handler->display->display_options['defaults']['filter_groups'] = FALSE;
$handler->display->display_options['defaults']['filters'] = FALSE;
/* Filter criterion: Location: Country */
$handler->display->display_options['filters']['country']['id'] = 'country';
$handler->display->display_options['filters']['country']['table'] = 'location';
$handler->display->display_options['filters']['country']['field'] = 'country';
$handler->display->display_options['filters']['country']['relationship'] = 'field_country_lid';
$handler->display->display_options['filters']['country']['value'] = array(
  'smart_ip.location.country_code' => 'smart_ip.location.country_code',
);
$handler->display->display_options['entity_type'] = 'node';

/* Display: Download SG Field */
$handler = $view->new_display('entity_view', 'Download SG Field', 'entity_view_2');
$handler->display->display_options['defaults']['relationships'] = FALSE;
/* Relationship: Content: Download SG (field_download_sg) */
$handler->display->display_options['relationships']['field_download_sg_value']['id'] = 'field_download_sg_value';
$handler->display->display_options['relationships']['field_download_sg_value']['table'] = 'field_data_field_download_sg';
$handler->display->display_options['relationships']['field_download_sg_value']['field'] = 'field_download_sg_value';
$handler->display->display_options['relationships']['field_download_sg_value']['required'] = TRUE;
$handler->display->display_options['relationships']['field_download_sg_value']['delta'] = '-1';
/* Relationship: Field collection item: Country (field_country) */
$handler->display->display_options['relationships']['field_country_lid']['id'] = 'field_country_lid';
$handler->display->display_options['relationships']['field_country_lid']['table'] = 'field_data_field_country';
$handler->display->display_options['relationships']['field_country_lid']['field'] = 'field_country_lid';
$handler->display->display_options['relationships']['field_country_lid']['relationship'] = 'field_download_sg_value';
$handler->display->display_options['relationships']['field_country_lid']['required'] = TRUE;
$handler->display->display_options['defaults']['fields'] = FALSE;
/* Field: Content: Download SG */
$handler->display->display_options['fields']['field_download_sg']['id'] = 'field_download_sg';
$handler->display->display_options['fields']['field_download_sg']['table'] = 'field_data_field_download_sg';
$handler->display->display_options['fields']['field_download_sg']['field'] = 'field_download_sg';
$handler->display->display_options['fields']['field_download_sg']['hide_empty'] = TRUE;
$handler->display->display_options['fields']['field_download_sg']['type'] = 'field_collection_fields';
$handler->display->display_options['fields']['field_download_sg']['settings'] = array(
  'view_mode' => 'full',
);
$handler->display->display_options['defaults']['filter_groups'] = FALSE;
$handler->display->display_options['defaults']['filters'] = FALSE;
/* Filter criterion: Location: Country */
$handler->display->display_options['filters']['country']['id'] = 'country';
$handler->display->display_options['filters']['country']['table'] = 'location';
$handler->display->display_options['filters']['country']['field'] = 'country';
$handler->display->display_options['filters']['country']['relationship'] = 'field_country_lid';
$handler->display->display_options['filters']['country']['value'] = array(
  'smart_ip.location.country_code' => 'smart_ip.location.country_code',
);
$handler->display->display_options['entity_type'] = 'node';
  

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.