Skip to content

Commit

Permalink
Locate map pins by indexed WKT (#336)
Browse files Browse the repository at this point in the history
* Locate map pins by indexed WKT.

This removes the requirement for an entity load of the geo entity.
It enables the possibility to show multiple locations per directory
item as #333.

* Coding standard fix.

* Remove schema-less unused property from view.

* Add patch for leaflet schema #336 (comment)

---------

Co-authored-by: Finn Lewis <[email protected]>
Co-authored-by: Andy Broomfield <[email protected]>
Co-authored-by: Finn <[email protected]>
  • Loading branch information
4 people authored Sep 17, 2024
1 parent e922596 commit 7088083
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 98 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
"patches": {
"drupal/facets": {
"Don't render facet block if backend isn't available: https://www.drupal.org/project/facets/issues/3311856": "https://git.drupalcode.org/issue/facets-3311856/-/commit/765d5ef4228906c7f201e116763f3018a7867c96.patch"
},
"drupal/leaflet": {
"Fix leaflet schema for missing properties on leaflet_popup: https://www.drupal.org/project/leaflet/issues/3473537": "https://git.drupalcode.org/issue/leaflet-3473537/-/commit/1f8d30a70ff7ac07cf7e8537aad65559f83905f2.patch"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ dependencies:
- field.storage.node.body
- search_api.index.localgov_directories_index_default
module:
- geo_entity
- geofield
- leaflet_views
- search_api
Expand Down Expand Up @@ -178,7 +177,6 @@ display:
default_argument_type: fixed
default_argument_options:
argument: ''
default_argument_skip_url: false
summary_options: { }
summary:
sort_order: asc
Expand Down Expand Up @@ -247,10 +245,8 @@ display:
options:
view_modes:
'entity:node':
localgov_directories_page: teaser
localgov_directories_venue: teaser
localgov_directory_page: search_result
localgov_directory_promo_page: teaser
query:
type: search_api_query
options:
Expand Down Expand Up @@ -349,15 +345,14 @@ display:
use_highlighting: false
multi_type: separator
multi_separator: ', '
location:
id: location
table: search_api_entity_geo_entity
field: location
relationship: localgov_location
search_api_rendered_item:
id: search_api_rendered_item
table: search_api_index_localgov_directories_index_default
field: search_api_rendered_item
relationship: none
group_type: group
admin_label: ''
entity_type: geo_entity
plugin_id: search_api_field
plugin_id: search_api_rendered_item
label: ''
exclude: true
alter:
Expand Down Expand Up @@ -399,36 +394,19 @@ display:
hide_empty: false
empty_zero: false
hide_alter_empty: true
click_sort_column: value
type: geofield_default
settings:
output_format: json
output_escape: true
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
field_rendering: true
fallback_handler: search_api
fallback_options:
link_to_item: false
use_highlighting: false
multi_type: separator
multi_separator: ', '
search_api_rendered_item:
id: search_api_rendered_item
view_modes:
'entity:node':
localgov_directories_page: teaser
localgov_directories_venue: teaser
localgov_directory_promo_page: default
localgov_location_wkt:
id: localgov_location_wkt
table: search_api_index_localgov_directories_index_default
field: search_api_rendered_item
field: localgov_location_wkt
relationship: none
group_type: group
admin_label: ''
plugin_id: search_api_rendered_item
plugin_id: search_api_field
label: ''
exclude: true
alter:
Expand Down Expand Up @@ -470,11 +448,28 @@ display:
hide_empty: false
empty_zero: false
hide_alter_empty: true
view_modes:
'entity:node':
localgov_directories_page: teaser
localgov_directories_venue: teaser
localgov_directory_promo_page: default
click_sort_column: value
type: geofield_default
settings:
output_format: wkt
output_escape: true
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
field_rendering: false
fallback_handler: search_api
fallback_options:
link_to_item: false
use_highlighting: false
multi_type: separator
multi_separator: ', '
pager:
type: none
options:
Expand Down Expand Up @@ -597,7 +592,7 @@ display:
options:
grouping: { }
data_source:
location: location
localgov_location_wkt: localgov_location_wkt
entity_source: __base_table
entity_source_submit: 'Update Entity Source'
name_field: title
Expand All @@ -607,12 +602,14 @@ display:
leaflet_popup:
value: '#rendered_view_fields'
options: '{"maxWidth":"300","minWidth":"50","autoPan":true}'
view_mode: full
leaflet_map: 'OSM Mapnik'
height: '400'
height_unit: px
hide_empty_map: true
disable_wheel: false
gesture_handling: false
fitbounds_options: '{"padding":[0,0]}'
reset_map:
control: false
options: '{"position":"topleft","title":"Reset View"}'
Expand Down Expand Up @@ -670,18 +667,6 @@ display:
position: topright
input_size: 25
providers:
file:
weight: '0'
checked: 0
gpx_file:
weight: '0'
checked: 0
geojson_file:
weight: '0'
checked: 0
kml_file:
weight: '0'
checked: 0
geo_entity_osm:
weight: '0'
checked: 0
Expand Down Expand Up @@ -712,18 +697,7 @@ display:
fields: false
filters: false
filter_groups: false
relationships:
localgov_location:
id: localgov_location
table: search_api_datasource_localgov_directories_index_default_entity_node
field: localgov_location
relationship: none
group_type: group
admin_label: 'Location: Geo'
entity_type: node
plugin_id: search_api
required: false
skip_access: false
relationships: { }
display_description: ''
display_extenders: { }
cache_metadata:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use Drupal\Component\Serialization\Yaml;
use Drupal\localgov_directories\Constants as Directory;
use Drupal\localgov_directories_location\ProximitySearchSetup;
use Drupal\search_api\Entity\Index as SearchIndex;
use Drupal\search_api\Item\Field as SearchIndexField;
use Drupal\views\Entity\View;

/**
Expand All @@ -24,6 +26,15 @@ function localgov_directories_location_install($is_syncing) {
return;
}

$index = SearchIndex::load(Directory::DEFAULT_INDEX);
$location_field = new SearchIndexField($index, Directory::LOCATION_FIELD_WKT);
$location_field->setLabel('Location » Geo » location WKT');
$location_field->setDatasourceId('entity:node');
$location_field->setType('string');
$location_field->setPropertyPath('localgov_location:entity:location');
$index->addField($location_field);
$index->save();

// Retrieves view display mode config and adds location-related sections to
// the existing configuration.
$module_path = \Drupal::service('extension.list.module')->getPath('localgov_directories_location');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use Drupal\localgov_directories_location\ProximitySearchSetup;
use Drupal\node\NodeInterface;
use Drupal\search_api\Entity\Index as SearchIndex;
use Drupal\search_api\IndexInterface as SearchIndexInterface;
use Drupal\search_api\Item\Field as SearchIndexField;

/**
* Implements hook_entity_extra_field_info().
Expand Down Expand Up @@ -47,29 +48,43 @@ function localgov_directories_location_node_view(array &$build, NodeInterface $n
* type.
*/
function localgov_directories_location_field_config_insert(FieldConfigInterface $field) {

$proximity_search_setup = \Drupal::classResolver(ProximitySearchSetup::class);
if (!$proximity_search_setup->hasLocationSearch()) {
return;
}

$index = SearchIndex::load(Directory::DEFAULT_INDEX);
$new_field_name = $field->getName();
$is_node_entity_type = $field->getTargetEntityTypeId() === 'node';

if ($new_field_name === Directory::LOCATION_FIELD && $is_node_entity_type && $index && $index->status() && !$index->getField(Directory::LOCATION_FIELD)) {
if ($proximity_search_setup->setup($field, $index)) {
\Drupal::classResolver(ConfigurationHelper::class)->createFacet(Directory::FACET_CONFIG_ENTITY_ID_FOR_PROXIMITY_SEARCH, Directory::FACET_CONFIG_FILE_FOR_PROXIMITY_SEARCH);
}
if ($index && $index->status() && $is_node_entity_type &&
$new_field_name == Directory::LOCATION_FIELD &&
!$index->getField(Directory::LOCATION_FIELD_WKT)
) {
$location_field = new SearchIndexField($index, Directory::LOCATION_FIELD_WKT);
$location_field->setLabel('Location » Geo » location WKT');
$location_field->setDatasourceId('entity:node');
$location_field->setType('string');
$location_field->setPropertyPath('localgov_location:entity:location');
$index->addField($location_field);
$index->save();
}
elseif (in_array($new_field_name, [

if (in_array($new_field_name, [
Directory::CHANNEL_SELECTION_FIELD,
Directory::TITLE_SORT_FIELD,
]) && $is_node_entity_type && $index) {
// The channel selection and title sort fields may have gone missing from
// the search index when we added localgov_location.
$proximity_search_setup->repairSearchIndex($index);
}

if ($proximity_search_setup->hasLocationSearch() &&
$is_node_entity_type && $index && $index->status() &&
$new_field_name === Directory::LOCATION_FIELD &&
!$index->getField(Directory::LOCATION_FIELD)
) {
if ($proximity_search_setup->setup($field, $index)) {
\Drupal::classResolver(ConfigurationHelper::class)->createFacet(Directory::FACET_CONFIG_ENTITY_ID_FOR_PROXIMITY_SEARCH, Directory::FACET_CONFIG_FILE_FOR_PROXIMITY_SEARCH);
}
}
}

/**
Expand All @@ -80,31 +95,29 @@ function localgov_directories_location_field_config_insert(FieldConfigInterface
* this module was installed but has now become available.
*/
function localgov_directories_location_search_api_index_update(SearchIndexInterface $search_index) {

$has_proximity_search_setup_ended = &drupal_static(__FUNCTION__);
if (isset($has_proximity_search_setup_ended)) {
return;
}
$has_proximity_search_setup_ended = TRUE;

if ($search_index->id() !== Directory::DEFAULT_INDEX) {
$index_update_running = &drupal_static(__FUNCTION__);
if ($index_update_running) {
return;
}
$index_update_running = TRUE;

if ($search_index->getField(Directory::LOCATION_FIELD)) {
return;
}

$proximity_search_setup = \Drupal::classResolver(ProximitySearchSetup::class);
if (!$proximity_search_setup->hasLocationSearch()) {
return;
// Check location wkt string and location lat/lon fields. If missing
// run same logic as when their are first inserted to see if they
// can or should be added to the index.
if ($search_index->id() == Directory::DEFAULT_INDEX &&
(
!$search_index->getField(Directory::LOCATION_FIELD_WKT) ||
!$search_index->getField(Directory::LOCATION_FIELD)
)
) {
$location_field_configs = \Drupal::service('entity_type.manager')
->getStorage('field_config')
->loadByProperties([
'field_name' => Directory::LOCATION_FIELD,
'entity_type' => 'node',
]);
array_walk($location_field_configs, 'localgov_directories_location_field_config_insert');
}

$location_field_configs = \Drupal::service('entity_type.manager')
->getStorage('field_config')
->loadByProperties([
'field_name' => Directory::LOCATION_FIELD,
'entity_type' => 'node',
]);
array_walk($location_field_configs, 'localgov_directories_location_field_config_insert');
$index_update_running = FALSE;
}
2 changes: 2 additions & 0 deletions src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Constants {

const LOCATION_FIELD = 'localgov_location';

const LOCATION_FIELD_WKT = 'localgov_location_wkt';

const LOCATION_MODULE = 'localgov_directories_location';

const PROXIMITY_SEARCH_CFG_FIELD = 'localgov_proximity_search_cfg';
Expand Down

0 comments on commit 7088083

Please sign in to comment.