Skip to content
This repository has been archived by the owner on Jul 1, 2022. It is now read-only.

OS-15: Ability to filter and sort locations by amenities offered #1005

Merged
merged 18 commits into from Apr 24, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions modules/custom/location_finder/location_finder.module
Expand Up @@ -50,6 +50,15 @@ function location_finder_entity_view_alter(&$build, EntityInterface $entity, Ent
'tags' => $tags,
];
$build['#attributes']['data-locations'] = json_encode($values);

$amenities = [];
if ($entity->hasField('field_location_amenities')) {
$entityAmenties = $entity->get('field_location_amenities')->getValue();
foreach ($entityAmenties as $amenity) {
$amenities[] = $amenity['target_id'];
}
}
$build['#attributes']['data-amenities'] = json_encode($amenities);
}

/**
Expand Down
62 changes: 59 additions & 3 deletions modules/custom/openy_map/js/map.js
Expand Up @@ -70,9 +70,11 @@
this.draw_map_controls();
this.hookup_map_controls_events();
this.update_tag_filters();
this.update_amenities_filters();
this.draw_map_locations();
this.draw_list_locations();


var mapLocation = document.location.href.match(/&?[amp;]?map_location=([\w|\+]*)&?[amp;]?/),
component = this;

Expand Down Expand Up @@ -131,6 +133,7 @@
}

this.update_tag_filters();
this.update_amenities_filters();
this.redraw_map_locations();
this.draw_list_locations();
},
Expand All @@ -143,6 +146,8 @@
this.locate_me_el.on('click', $.proxy(this.locate_me_onclick, this));
this.component_el.find('nav.types input[type=checkbox]').on('change', $.proxy(this.bar_filter_change, this));
this.search_field_el.on("autocompleteselect", $.proxy(this.apply_autocomplete_search, this));

$('#views-exposed-form-location-by-amenities-block-1').find('input[type=checkbox]').on('change', $.proxy(this.filter_change, this));
},

// Attempts a map search against Google's
Expand Down Expand Up @@ -244,6 +249,7 @@
this.draw_list_locations();
},


// Executed if was provided empty ZIP code.
reset_search_results: function () {
if (this.search_center === null) {
Expand Down Expand Up @@ -317,6 +323,7 @@
}
},


// Applies the current checkbox state of the tag filter controls
// to the internal filters data structure.
// Called at init time, and after every checkbox state change.
Expand All @@ -328,15 +335,29 @@
var el = $(this);
self.tag_filters.push(el.val());
};

this.map_controls_el.find('.tag_filters input[type=checkbox]:checked').each(f);
},

update_amenities_filters: function () {
this.amenities_filters = [];
var self = this;

var f = function (index) {
var el = $(this);
self.amenities_filters.push(el.val());
};

$('#views-exposed-form-location-by-amenities-block-1')
.find('input[type=checkbox]:checked')
.each(f);
},

// Applies tag and distance filters to a list of locations,
// returns the filtered list.
apply_filters: function (locations) {
locations = this.apply_tag_filters(locations);
locations = this.apply_distance_filters(locations);
locations = this.apply_amenities_filters(locations);
this.set_url_parameters();
return locations;
},
Expand Down Expand Up @@ -411,6 +432,28 @@
return filtered_locations;
},


// Applies tag filters to a list of locations,
// returns the filtered list.
apply_amenities_filters: function (locations) {
if (this.amenities_filters.length === 0) {
return locations;
}
var filtered_locations = [];
for (var i = 0; i < locations.length; i++) {
var loc = locations[i];
for (var j = 0; j < this.amenities_filters.length; j++) {
var amenities_filter = this.amenities_filters[j];
if ($.inArray(amenities_filter, loc.amenities) >= 0) {
filtered_locations.push(loc);
continue; // If any tag matches, skip checking other tags
}
}
}
return filtered_locations;
},


// Populates an array of active tags from an URL parameter "type".
init_active_tags: function () {
if (this.initial_active_tags) {
Expand Down Expand Up @@ -457,7 +500,9 @@
var url = document.location.pathname,
params = this.get_parameters(),
filterTagsRaw = this.tag_filters,
filteramenitiesRaw = this.amenities_filters,
filterTags = '',
filteramenities = '',
mapLocation = $('.search_field').val() || (params.hasOwnProperty('map_location') && params.map_location) || '';
if (mapLocation) {
mapLocation = '?map_location=' + this.encode_to_url_format(mapLocation);
Expand All @@ -470,7 +515,15 @@
}, this, filterTags);
filterTags = filterTags.substring(0, filterTags.length - 1);
}
window.history.replaceState(null, null, url + mapLocation + filterTags);
if (filteramenitiesRaw) {
filteramenities = '&';
filteramenities += 'amenities=';
filteramenitiesRaw.forEach(function (tag) {
filteramenities += this.encode_to_url_format(tag) + ',';
}, this, filteramenities);
filteramenities = filteramenities.substring(0, filteramenities.length - 1);
}
window.history.replaceState(null, null, url + mapLocation + filterTags + filteramenities);
},

// Renders an extra set of filter boxes below the map.
Expand Down Expand Up @@ -725,11 +778,14 @@
if ($.trim($self.find('.location-item--title')[0].innerText).toLowerCase() == $.trim(data[i].name).toLocaleLowerCase()) {
data[i].element = {};
data[i].element = $self.parent();
data[i].amenities = [];
data[i].amenities = ($self.data('amenities'));
}
}
}
});


$('.openy-map-canvas', context).once().each(function () {
var $canvas = $(this);
var timer = setInterval(function () {
Expand All @@ -752,4 +808,4 @@
}
};

}) (jQuery, window, Drupal, drupalSettings);
})(jQuery, window, Drupal, drupalSettings);
@@ -0,0 +1,44 @@
langcode: en
status: true
dependencies:
enforced:
module:
- openy_demo_nlanding
id: openy_demo_lp_paragraph_loc_by_amnts
migration_tags: { }
migration_group: openy_demo_nlanding
label: 'Create Location By Amenities paragraph(s) for demo landing page nodes'
source:
plugin: embedded_data
data_rows:
-
id: locations_by_amenties
parent_id: locations
title: 'Amenities'
ids:
id:
type: string
process:
langcode:
plugin: default_value
source: language
default_value: und
status:
plugin: default_value
default_value: 1
uid:
plugin: default_value
default_value: 1
parent_id:
plugin: migration
migration: openy_demo_node_landing
no_stub: true
source: parent_id
parent_type:
plugin: default_value
default_value: node
# TODO: get parent_field_name from data?
field_prgf_title: title
destination:
plugin: 'entity:paragraph'
default_bundle: location_filter_by_amenities
Expand Up @@ -15,6 +15,7 @@ source:
id: homepage
title: 'OpenY'
alias: ''
layout: one_column
header_content_ids:
-
header_content_id: homepage_banner
Expand All @@ -29,6 +30,7 @@ source:
id: about
title: 'About the YMCA'
alias: '/about'
layout: one_column
header_content_ids:
-
header_content_id: about_gallery
Expand All @@ -39,6 +41,7 @@ source:
id: give
title: 'Give'
alias: '/give'
layout: one_column
header_content_ids:
-
header_content_id: give_microsites_menu
Expand All @@ -51,6 +54,7 @@ source:
id: blog
title: 'Blog'
alias: '/blog'
layout: one_column
header_content_ids:
-
header_content_id: blog_small_banner
Expand All @@ -63,6 +67,7 @@ source:
id: accelerator
title: 'Accelerator'
alias: '/accelerator'
layout: one_column
header_content_ids:
-
header_content_id: accelerator_gallery
Expand All @@ -77,6 +82,7 @@ source:
id: locations
title: 'Locations'
alias: '/locations'
layout: two_column
header_content_ids:
-
header_content_id: locations_small_banner
Expand All @@ -87,10 +93,14 @@ source:
content_ids:
-
content_id: locations_location_finder
sidebar:
-
sidebar_id: locations_by_amenties
-
id: lp_camp_page_1
title: 'Camp Colman page'
alias: '/camps/camp-colman/about'
layout: one_column
header_content_ids:
-
header_content_id: lp_camp_menu_1
Expand All @@ -101,6 +111,7 @@ source:
id: lp_camp_page_2
title: 'Camp Orkila page'
alias: '/camps/camp-orkila/about'
layout: one_column
header_content_ids:
-
header_content_id: lp_camp_menu_2
Expand All @@ -111,6 +122,7 @@ source:
id: lp_camp_page_3
title: 'Camp Terry page'
alias: '/camps/camp-terry/about'
layout: one_column
header_content_ids:
-
header_content_id: lp_camp_menu_3
Expand All @@ -121,6 +133,7 @@ source:
id: join
title: 'Join'
alias: '/join'
layout: one_column
header_content_ids:
-
header_content_id: join_gallery
Expand All @@ -131,6 +144,7 @@ source:
id: schedules
title: 'Schedules'
alias: '/schedules'
layout: one_column
header_content_ids:
-
header_content_id: schedules_small_banner
Expand All @@ -145,6 +159,7 @@ source:
id: group_schedules
title: 'Group Schedules'
alias: '/schedules/group-schedules'
layout: one_column
header_content_ids:
-
header_content_id: group_schedules
Expand All @@ -155,6 +170,7 @@ source:
id: group_schedules_embedded
title: 'Group Schedules (Embedded)'
alias: '/schedules/group-schedules-embedded'
layout: one_column
header_content_ids:
-
header_content_id: group_schedules_embedded
Expand All @@ -165,6 +181,7 @@ source:
id: amenities
title: 'Amenities at each Y'
alias: '/amenities'
layout: one_column
content_ids:
-
content_id: all_amenities
Expand All @@ -191,7 +208,7 @@ process:
source: alias
field_lp_layout:
plugin: default_value
default_value: one_column
source: layout
field_header_content:
plugin: iterator
source: header_content_ids
Expand Down Expand Up @@ -264,6 +281,20 @@ process:
- openy_demo_lp_paragraph_group_schedules
- openy_demo_lp_paragraph_all_amenities
source: content_id
field_sidebar_content:
plugin: iterator
source: sidebar
process:
target_id:
plugin: migration
migration:
- openy_demo_lp_paragraph_loc_by_amnts
source: sidebar_id
target_revision_id:
plugin: migration
migration:
- openy_demo_lp_paragraph_loc_by_amnts
source: sidebar_id
destination:
plugin: 'entity:node'
default_bundle: landing_page
Expand Down Expand Up @@ -291,5 +322,6 @@ migration_dependencies:
- openy_demo_lp_paragraph_microsites_menu
- openy_demo_lp_paragraph_all_amenities
- openy_demo_lp_paragraph_lto
- openy_demo_lp_paragraph_loc_by_amnts
optional:
- openy_demo_node_blog
Expand Up @@ -11,7 +11,8 @@ use Drupal\Core\Form\FormStateInterface;
* Implements hook_form_FORM_ID_alter().
*/
function openy_prgf_amenities_form_views_exposed_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if ($form['#id'] == 'views-exposed-form-branches-default') {
if (($form['#id'] == 'views-exposed-form-branches-default') ||
($form['#id'] == 'views-exposed-form-location-by-amenities-block-1')) {
$query = Drupal::entityQuery('taxonomy_term')
->condition('vid', 'amenities')
->sort('name');
Expand Down