Influence the Drupal site's suggestion list of search autocomplete module with Smart IP views country filter

This is an example application of Smart IP where it influence the suggestion shown in the list of search autocomplete based on the country location of the visitor in a Drupal 7 site with the use of Smart IP, Search Autocomplete and Location contributed modules. Basically, the search autocomplete will display list of suggestions but the list of nodes will be filtered based on country location of the visitor and nodes that have an empty value in Location country field will not be filtered. Smart IP module is used to identify Drupal site visitor's geographical location based on his IP address, Search Autocomplete module is used to take of the autocomplete feature on Drupal search box and Location module is used to provide the field and expose views location filters.

Steps:

  1. Install Smart IP, Smart IP Views Bridge, Search Autocomplete, Location, Location CCK, Ctools, 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 "Location" field type at http://www.yoursite.com/?q=admin/structure/types/manage/page/fields. Populate the field label with "Branch". Select "Location" from Type of data to store and click "Save" button.

    Add location field

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

    Location field collection settings

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

  4. Create some "Basic page" content. Populate the "Title", "City" fields and select a country.

    Create Basic page

    Note: Make sure that at one of your created node's country location field value matches your country geolocation that Smart IP has detected based on your IP. To check, enable the Device Geolocation's block at http://www.yoursite.com/?q=admin/structure/block.

  5. Create some "Basic page" contents where the Location field (City/Country) are empty.

    Create some "Basic page" Location field (City/Country) are empty

  6. Edit the nodes-autocomplete Views at http://www.yoursite.com/?q=admin/structure/views/view/nodes_autocomplete/edit.

  7. Add a filter criteria using "Location: Country" filter. Make sure to select "This page (override)" from "For" dropdown box at upper left.

    Add views Location: Country filter

    Under the "Operator" select "Is", select "Smart IP: visitor's country code" item from "Country" dropdown box and click "Apply" button.

    Configure views Location: Country filter

  8. Add another filter criteria using "Location: Country" filter for checking if empty. Make sure to select "This page (override)" from "For" dropdown box at upper left.

    Add views Location: Country filter

    Under the "Operator" select "Is empty (NULL)" and click "Apply" button.

    Location country filter select "Is empty (NULL)"

  9. Lets rearrange the filter criteria. Select the "And/Or, Rearrange" option. Click the "Create new filter group", select "OR" operator for the new filter group created and select "AND" for the main operator of the two filter groups.

    Rearrange the filter criteria

  10. Save your views.

    Modified nodes_autocomplete views

  11. Go to http://www.yoursite.com/?q=admin/config/development/performance and clear Drupal cache.

  12. Test the search autocomplete. Typing a few first letters of the node title that you created will list suggestions but filtered based on your country location.

    Search autocomplete test

    And it should list suggestions nodes that have an empty value in Location country field.

    Search autocomplete test for nodes with empty location field

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

  
$view = new view();
$view->name = 'nodes_autocomplete';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'node';
$view->human_name = 'nodes-autocomplete';
$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['title'] = 'Node title suggestions for autocompletion';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['use_more_text'] = 'plus';
$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['query']['options']['query_comment'] = FALSE;
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['exposed_form']['options']['submit_button'] = 'Search';
$handler->display->display_options['pager']['type'] = 'some';
$handler->display->display_options['pager']['options']['items_per_page'] = '15';
$handler->display->display_options['pager']['options']['offset'] = '0';
$handler->display->display_options['style_plugin'] = 'search_autocomplete';
$handler->display->display_options['style_options']['group_by'] = 'type';
$handler->display->display_options['style_options']['input_label'] = 'title';
$handler->display->display_options['style_options']['input_link'] = 'view_node';
$handler->display->display_options['style_options']['output_fields'] = array(
  'title' => 'title',
  'name' => 'name',
  'comment_count' => 'comment_count',
);
/* Relationship: Content: Author */
$handler->display->display_options['relationships']['uid']['id'] = 'uid';
$handler->display->display_options['relationships']['uid']['table'] = 'node';
$handler->display->display_options['relationships']['uid']['field'] = 'uid';
/* Field: Content: Link */
$handler->display->display_options['fields']['view_node']['id'] = 'view_node';
$handler->display->display_options['fields']['view_node']['table'] = 'views_entity_node';
$handler->display->display_options['fields']['view_node']['field'] = 'view_node';
$handler->display->display_options['fields']['view_node']['label'] = '';
$handler->display->display_options['fields']['view_node']['element_label_colon'] = FALSE;
/* 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']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['title']['link_to_node'] = FALSE;
/* Field: User: Name */
$handler->display->display_options['fields']['name']['id'] = 'name';
$handler->display->display_options['fields']['name']['table'] = 'users';
$handler->display->display_options['fields']['name']['field'] = 'name';
$handler->display->display_options['fields']['name']['relationship'] = 'uid';
$handler->display->display_options['fields']['name']['label'] = 'author';
$handler->display->display_options['fields']['name']['link_to_user'] = FALSE;
/* Field: Content: Comment count */
$handler->display->display_options['fields']['comment_count']['id'] = 'comment_count';
$handler->display->display_options['fields']['comment_count']['table'] = 'node_comment_statistics';
$handler->display->display_options['fields']['comment_count']['field'] = 'comment_count';
$handler->display->display_options['fields']['comment_count']['label'] = '';
$handler->display->display_options['fields']['comment_count']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['comment_count']['separator'] = ' ';
$handler->display->display_options['fields']['comment_count']['suffix'] = ' comment(s)';
/* Field: Content: Type */
$handler->display->display_options['fields']['type']['id'] = 'type';
$handler->display->display_options['fields']['type']['table'] = 'node';
$handler->display->display_options['fields']['type']['field'] = 'type';
$handler->display->display_options['fields']['type']['label'] = '';
$handler->display->display_options['fields']['type']['element_label_colon'] = FALSE;
/* Sort criterion: Content: Title */
$handler->display->display_options['sorts']['title']['id'] = 'title';
$handler->display->display_options['sorts']['title']['table'] = 'node';
$handler->display->display_options['sorts']['title']['field'] = 'title';
/* 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 revision: Title */
$handler->display->display_options['filters']['title']['id'] = 'title';
$handler->display->display_options['filters']['title']['table'] = 'node_revision';
$handler->display->display_options['filters']['title']['field'] = 'title';
$handler->display->display_options['filters']['title']['operator'] = 'contains';
$handler->display->display_options['filters']['title']['exposed'] = TRUE;
$handler->display->display_options['filters']['title']['expose']['operator_id'] = 'title_op';
$handler->display->display_options['filters']['title']['expose']['label'] = 'title';
$handler->display->display_options['filters']['title']['expose']['operator'] = 'title_op';
$handler->display->display_options['filters']['title']['expose']['identifier'] = 'filter';
$handler->display->display_options['filters']['title']['expose']['remember_roles'] = array(
  2 => '2',
  1 => 0,
  4 => 0,
  3 => 0,
);

/* Display: Page */
$handler = $view->new_display('page', 'Page', 'page');
$handler->display->display_options['defaults']['fields'] = FALSE;
/* Field: Content: Link */
$handler->display->display_options['fields']['view_node']['id'] = 'view_node';
$handler->display->display_options['fields']['view_node']['table'] = 'views_entity_node';
$handler->display->display_options['fields']['view_node']['field'] = 'view_node';
$handler->display->display_options['fields']['view_node']['label'] = '';
$handler->display->display_options['fields']['view_node']['element_label_colon'] = FALSE;
/* 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']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['title']['link_to_node'] = FALSE;
/* Field: User: Name */
$handler->display->display_options['fields']['name']['id'] = 'name';
$handler->display->display_options['fields']['name']['table'] = 'users';
$handler->display->display_options['fields']['name']['field'] = 'name';
$handler->display->display_options['fields']['name']['relationship'] = 'uid';
$handler->display->display_options['fields']['name']['label'] = 'author';
$handler->display->display_options['fields']['name']['link_to_user'] = FALSE;
/* Field: Content: Comment count */
$handler->display->display_options['fields']['comment_count']['id'] = 'comment_count';
$handler->display->display_options['fields']['comment_count']['table'] = 'node_comment_statistics';
$handler->display->display_options['fields']['comment_count']['field'] = 'comment_count';
$handler->display->display_options['fields']['comment_count']['label'] = '';
$handler->display->display_options['fields']['comment_count']['element_label_colon'] = FALSE;
$handler->display->display_options['fields']['comment_count']['separator'] = ' ';
$handler->display->display_options['fields']['comment_count']['suffix'] = ' comment(s)';
/* Field: Content: Type */
$handler->display->display_options['fields']['type']['id'] = 'type';
$handler->display->display_options['fields']['type']['table'] = 'node';
$handler->display->display_options['fields']['type']['field'] = 'type';
$handler->display->display_options['fields']['type']['label'] = '';
$handler->display->display_options['fields']['type']['element_label_colon'] = FALSE;
$handler->display->display_options['defaults']['filter_groups'] = FALSE;
$handler->display->display_options['filter_groups']['groups'] = array(
  1 => 'AND',
  2 => 'OR',
);
$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 revision: Title */
$handler->display->display_options['filters']['title']['id'] = 'title';
$handler->display->display_options['filters']['title']['table'] = 'node_revision';
$handler->display->display_options['filters']['title']['field'] = 'title';
$handler->display->display_options['filters']['title']['operator'] = 'contains';
$handler->display->display_options['filters']['title']['group'] = 1;
$handler->display->display_options['filters']['title']['exposed'] = TRUE;
$handler->display->display_options['filters']['title']['expose']['operator_id'] = 'title_op';
$handler->display->display_options['filters']['title']['expose']['label'] = 'title';
$handler->display->display_options['filters']['title']['expose']['operator'] = 'title_op';
$handler->display->display_options['filters']['title']['expose']['identifier'] = 'filter';
$handler->display->display_options['filters']['title']['expose']['remember_roles'] = array(
  2 => '2',
  1 => 0,
  4 => 0,
  3 => 0,
);
/* 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']['operator'] = 'empty';
$handler->display->display_options['filters']['country']['group'] = 2;
/* Filter criterion: Location: Country */
$handler->display->display_options['filters']['country_1']['id'] = 'country_1';
$handler->display->display_options['filters']['country_1']['table'] = 'location';
$handler->display->display_options['filters']['country_1']['field'] = 'country';
$handler->display->display_options['filters']['country_1']['value'] = array(
  'smart_ip.location.country_code' => 'smart_ip.location.country_code',
);
$handler->display->display_options['filters']['country_1']['group'] = 2;
$handler->display->display_options['path'] = 'nodes-autocomplete';
  

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.