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 three types of downloadable files; first type should be displayed only for visitors from Philippines; the second type should be displayed only for visitors from Singapore and the third type should be displayed for visitors that are not from Philippines and Singapore (rest of the world) 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; since we need to geolocate the file field only, the Field Collection module will be used to wrap the file field and location field together and we will create three separate fields which represents the three types of downloadable files, so each of these three Field Collection fields will contain file field and location field; and Location module is used to provide the location field. The three field collection fields will be:

  • field_download_ph - where the location country field value was fixed to PH

  • field_download_sg - where the location country field value was fixed to SG

  • field_download_global - where the location country field value was fixed to NONE

The following are the logic that we will use to implement the conditions above with the help of EVA module and views:

  • The field_download_ph (views eva) will be visible if user Smart IP detected location is PH and its No Result Behavior is set to display field_download_sg (views block).

  • The field_download_sg (views block) will be visible if user Smart IP detected location is SG and its No Result Behavior is set to display field_download_global (views block).

  • The field_download_global (views block) is always visible.

Basically, if field_download_ph is empty, field_download_sg will be its fallback but if field_download_sg is empty, the field_download_global will the last fallback (like a catch all). In effect, only one downloadable file should be shown on each "Product" content node varying to PH, SG and Rest of the world.

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" and choose "Embedded" as "Form element to edit the data". Click "Save" button.

    Add field_collection Download PH

    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" and choose "Embedded" as "Form element to edit the data". Click "Save" button.

    Add field_collection Download SG

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

  4. 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 Global" and choose "Embedded" as "Form element to edit the data". Click "Save" button.

    Add "Download Global" field collection

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

  5. Now we will add file field and location field to each "Download PH", "Download SG" and "Download Global" 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_global".

  6. 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.

    Add global "Product Manual" file field

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

  7. 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.

    Add global location field

    At "Location" Field Settings > Locative Information > Collection Settings select "Allow" for the "Country" item then choose "Select" as its default, select "Do not collect" for the rest of location items and click "Save field settings" button.

    Global location field settings

    Under "Default value" of "Country", choose "NOT LISTED" and click "Save settings" button.

    Global location field default value

  8. Now lets do the "Download PH" 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_ph". This time we will use the existing file field and location field we have created for "Download Global" Field Collection field. On "Field to share" dropdown menu, select "File: field_product_manual (Product Manual)" option and click "Save" button.

    field_collection Download PH add file field

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

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

    field_collection Download PH add location field

    Under "Default value" of "Country", choose "Philippines" and click "Save settings" button.

    field_collection Download PH location field settings

  10. And 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". 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.

  11. 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

    Under "Default value" of "Country", choose "Singapore" and click "Save settings" button.

    Singapore location field default value

  12. We will create a view and use the Smart IP country views filter to display the appropriate downloadable file available for visitors from Philippines, Singapore and rest of the world. 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

  13. Remove everything in "Sort Criteria".

    Views master display

  14. Collapse the "Advanced" and under "Contextual filter", add "Content: Nid":

    Add views default contextual filter

    Click "Add and configure contextual filters" button. Under "When the filter value is not available", select "Display contents of 'No results found'". Click "Apply" button.

    Add views default contextual filter's default value

  15. Add "EVA field" display and under the "Fields", remove the "Content: Title" field (make sure the "This entity_view (override)" is selected under "For" dropdown box at the upper left) then add "Content: Download PH" in Fields. Click "Apply (this display)" button.

    Add Download PH field

    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

  16. 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)" under "For" dropdown box at the upper left.

    Views Download PH add relationship

    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.

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

    Views Download PH add location relationship

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

    Views Download PH location relationship settings

  18. Under "Filter Criteria" add "Location: Country". Make sure to select "This entity_view (override)" under "For" dropdown box at the upper left.

    Views Download PH add location filter

    Select "Location from field_country" under "Relationship" field. Under the "Operator" select "Is", select "Smart IP: visitor's country code" item from "Country" dropdown box and click "Apply (this display)" button.

    Views Download PH add location filter settings

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

    Views Download PH entity content settings

  20. Change the "Display name" to "Download PH EVA Field". Click "Apply" button.

    Change the 'Display name'

  21. Save your view.

    Views Download PH display settings

  22. Now lets create "Attachment" display and under the "Fields", remove the "Content: Title" field (make sure the "This entity_view (override)" is selected under "For" dropdown box at the upper left) then add "Content: Download SG" in "Fields". Click "Apply (this display)" button.

    Add Download SG field

    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

    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.

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

    Views Download SG add location relationship

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

    Singapore country views relationship

  25. Under "Filter Criteria" add "Location: Country". Make sure to select "This entity_view (override)" under "For" dropdown box at the upper left.

    Views Download SG add location filter

    Select "Location from field_country" under "Relationship" field. 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

  26. Change the "Display name" to "Download SG Attachment". Click "Apply" button.

    "Views Download SG entity content settings

  27. Save your view.

    Views Download SG display settings

  28. Lets now create "Attachment" display and under the "Fields", remove the "Content: Title" field (make sure the "This entity_view (override)" is selected under "For" dropdown box at the upper left) then add "Content: Download Global" in "Fields". Click "Apply (this display)" button.

    Add Download Global field collection views field

    Select the "Fields only" from "Formatter" dropdown menu.

    Download Global field collection views field formatter settings

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

    Download Global field collection views field formatter set no result

  29. Change the "Display name" to "Download Global Attachment". Click "Apply" button.

    Change the global views 'Display name'

  30. Save your view.

    Save global views

  31. Now we will implement the fallback process. Go to "Download PH EVA Field" views display at http://www.yoursite.com/?q=admin/structure/views/view/product_manual_field/edit/entity_view_1 and under "No results behavior", add "Global: View area". Make sure to select "This entity_view (override)" from "For" dropdown box at upper left. Click "Apply (this display)" button.

    Add views 'No result behavior'

    Under "View to insert", select "View: product_manual_field - Display: attachment_1". Tick the "Inherit contextual filters". Click "Apply (this display)" button.

    Add views 'No result behavior'

    This will make the "Download SG Attachment" views display as fallback of "Download PH EVA Field" views display if it has no result or empty.

  32. Go to "Download SG Attachment" views display at http://www.yoursite.com/?q=admin/structure/views/view/product_manual_field/edit/attachment_1 and under "No results behavior", add "Global: View area". Make sure to select "This entity_view (override)" from "For" dropdown box at upper left. Click "Apply (this display)" button.

    Add views 'No result behavior'

    Under "View to insert", select "View: product_manual_field - Display: attachment_2". Tick the "Inherit contextual filters". Click "Apply (this display)" button.

    Add views 'No result behavior'

    This will make the "Download Global Attachment" views display as fallback of "Download SG Attachment" views display if it has no result or empty.

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

    Basic page hide original field_collection fields

  34. 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

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

    Test result

  36. For visitors from Singapore, the "Download SG" field should only display while the "Download PH" and "Download Global" should be hidden.

    Sample content output for Singapore

  37. And visitors from other countries (eg. USA), the "Download Global" field should only display while the "Download PH" and "Download SG" should be hidden.

    Sample content output for global

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';
$handler->display->display_options['fields']['title']['label'] = '';
$handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
$handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;
/* Contextual filter: Content: Nid */
$handler->display->display_options['arguments']['nid']['id'] = 'nid';
$handler->display->display_options['arguments']['nid']['table'] = 'node';
$handler->display->display_options['arguments']['nid']['field'] = 'nid';
$handler->display->display_options['arguments']['nid']['default_action'] = 'empty';
$handler->display->display_options['arguments']['nid']['default_argument_type'] = 'fixed';
$handler->display->display_options['arguments']['nid']['summary']['number_of_records'] = '0';
$handler->display->display_options['arguments']['nid']['summary']['format'] = 'default_summary';
$handler->display->display_options['arguments']['nid']['summary_options']['items_per_page'] = '25';
/* Filter criterion: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
  'page' => 'page',
);

/* Display: Download PH EVA Field */
$handler = $view->new_display('entity_view', 'Download PH EVA Field', 'entity_view_1');
$handler->display->display_options['defaults']['empty'] = FALSE;
/* No results behavior: Global: View area */
$handler->display->display_options['empty']['view']['id'] = 'view';
$handler->display->display_options['empty']['view']['table'] = 'views';
$handler->display->display_options['empty']['view']['field'] = 'view';
$handler->display->display_options['empty']['view']['empty'] = TRUE;
$handler->display->display_options['empty']['view']['view_to_insert'] = 'product_manual_field:attachment_1';
$handler->display->display_options['empty']['view']['inherit_arguments'] = TRUE;
$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: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
  'page' => 'page',
);
/* 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 Attachment */
$handler = $view->new_display('attachment', 'Download SG Attachment', 'attachment_1');
$handler->display->display_options['pager']['type'] = 'some';
$handler->display->display_options['defaults']['empty'] = FALSE;
/* No results behavior: Global: View area */
$handler->display->display_options['empty']['view']['id'] = 'view';
$handler->display->display_options['empty']['view']['table'] = 'views';
$handler->display->display_options['empty']['view']['field'] = 'view';
$handler->display->display_options['empty']['view']['empty'] = TRUE;
$handler->display->display_options['empty']['view']['view_to_insert'] = 'product_manual_field:attachment_2';
$handler->display->display_options['empty']['view']['inherit_arguments'] = TRUE;
$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: Content: Published */
$handler->display->display_options['filters']['status']['id'] = 'status';
$handler->display->display_options['filters']['status']['table'] = 'node';
$handler->display->display_options['filters']['status']['field'] = 'status';
$handler->display->display_options['filters']['status']['value'] = 1;
$handler->display->display_options['filters']['status']['group'] = 1;
$handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;
/* Filter criterion: Content: Type */
$handler->display->display_options['filters']['type']['id'] = 'type';
$handler->display->display_options['filters']['type']['table'] = 'node';
$handler->display->display_options['filters']['type']['field'] = 'type';
$handler->display->display_options['filters']['type']['value'] = array(
  'page' => 'page',
);
/* 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',
);

/* Display: Download Global Attachment */
$handler = $view->new_display('attachment', 'Download Global Attachment', 'attachment_2');
$handler->display->display_options['pager']['type'] = 'some';
$handler->display->display_options['defaults']['fields'] = FALSE;
/* Field: Content: Download Global */
$handler->display->display_options['fields']['field_download_global']['id'] = 'field_download_global';
$handler->display->display_options['fields']['field_download_global']['table'] = 'field_data_field_download_global';
$handler->display->display_options['fields']['field_download_global']['field'] = 'field_download_global';
$handler->display->display_options['fields']['field_download_global']['hide_empty'] = TRUE;
$handler->display->display_options['fields']['field_download_global']['type'] = 'field_collection_fields';
$handler->display->display_options['fields']['field_download_global']['settings'] = array(
  'view_mode' => 'full',
);
  

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.