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:
-
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.
-
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.
Use the default values for succeeding setup settings of this field.
-
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.
Use the default values for succeeding setup settings of this field.
-
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.
Use the default values for succeeding setup settings of this field.
-
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.
Click "manage fields" of "field_download_global".
-
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.
Use the default values for succeeding setup settings of this field.
-
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.
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.
Under "Default value" of "Country", choose "NOT LISTED" and click "Save settings" button.
-
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.
Use the default values for succeeding setup settings of this field.
-
For the location field, select "Location: field_country (Country)" on "Field to share" dropdown menu and click "Save" button.
Under "Default value" of "Country", choose "Philippines" and click "Save settings" button.
-
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.
Use the default values for succeeding setup settings of this field.
-
Now for the location field, select "Location: field_country (Country)" on "Field to share" dropdown menu and click "Save" button.
Under "Default value" of "Country", choose "Singapore" and click "Save settings" button.
-
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.
-
Remove everything in "Sort Criteria".
-
Collapse the "Advanced" and under "Contextual filter", add "Content: Nid":
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 "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.
Select the "Fields only" from "Formatter" dropdown menu.
Scroll down and collapse the "No Results Behavior" then tick the "Hide if empty". Click "Apply (this display)" button.
-
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.
Tick the "Require this relationship" checkbox and click "Apply (this display)" button.
This should make the "Field Collection item" available in "Relationship" options.
-
Add "Field collection item: Country (field_country)" in "Relationship". Click "Apply (this display)" button.
Tick the "Require this relationship" checkbox and click "Apply (this display)" button.
-
Under "Filter Criteria" add "Location: Country". Make sure to select "This entity_view (override)" under "For" dropdown box at the upper left.
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.
-
In "Entity Content Settings", change the "Entity type" to "Node". Click "Apply" button.
-
Change the "Display name" to "Download PH EVA Field". Click "Apply" button.
-
Save your view.
-
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.
Select the "Fields only" from "Formatter" dropdown menu.
Scroll down and collapse the "No Result Behavior" then tick the "Hide if empty". Click "Apply (this display)" button.
-
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.
Tick the "Require this relationship" checkbox and click "Apply (this display)" button.
This should make the "Field Collection item" available in "Relationship" options.
-
Add "Field collection item: Country (field_country)" in "Relationship". Click "Apply (this display)" button.
Tick the "Require this relationship" checkbox and click "Apply (this display)" button.
-
Under "Filter Criteria" add "Location: Country". Make sure to select "This entity_view (override)" under "For" dropdown box at the upper left.
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.
-
Change the "Display name" to "Download SG Attachment". Click "Apply" button.
-
Save your view.
-
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.
Select the "Fields only" from "Formatter" dropdown menu.
Scroll down and collapse the "No Result Behavior" then tick the "Hide if empty". Click "Apply (this display)" button.
-
Change the "Display name" to "Download Global Attachment". Click "Apply" button.
-
Save your view.
-
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.
Under "View to insert", select "View: product_manual_field - Display: attachment_1". Tick the "Inherit contextual filters". Click "Apply (this display)" button.
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.
-
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.
Under "View to insert", select "View: product_manual_field - Display: attachment_2". Tick the "Inherit contextual filters". Click "Apply (this display)" button.
This will make the "Download Global Attachment" views display as fallback of "Download SG Attachment" views display if it has no result or empty.
-
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.
-
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.
-
The "Download PH" field should only display to visitors from Philippines, the "Download SG" and "Download Global" should be hidden.
-
For visitors from Singapore, the "Download SG" field should only display while the "Download PH" and "Download Global" should be hidden.
-
And visitors from other countries (eg. USA), the "Download Global" field should only display while the "Download PH" and "Download SG" should be hidden.
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',
);