Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Locate map pins by indexed WKT #336

Merged
merged 8 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see any use of this Search: Rendered item field anywhere in this View's display. If we drop this field, the embed_map display loads about a thousand map pins without any memory exhaustion issues in our site. So I would request that this search_api_rendered_item field be dropped.

I am still running into memory exhaustion issues around 1500 map pins after dropping the search_api_rendered_item field. But I don't yet see any easy way out of that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ekes fancy adding this change?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm almost tempted to go the other way. If we're not including a freetext search based on rendered item in the default view maybe we should be?

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);
Copy link
Contributor

@Adnan-cds Adnan-cds Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello Ekes,
I am trying to understand the purpose behind this new localgov_location_wkt Search index field. Is it there to avoid the unwanted left join issue that Andy has pointed out?
Okay, don't worry. I can see it is of string type. Which suggests that it is for displaying the map popups only.

$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