localgov_directories-3.3.1/modules/localgov_directories_location/localgov_directories_location.install
modules/localgov_directories_location/localgov_directories_location.install
<?php
/**
* @file
* LocalGovDrupal directories location install file.
*/
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;
/**
* Implements hook_install().
*
* - Adds location field to search index.
* - Adds extra displays to the Directory channel view.
* - Adds the Proximity search configuration field to the Directory channel
* form. Also hides it from entity view.
*/
function localgov_directories_location_install($is_syncing) {
if ($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');
$view_with_map_embed = Yaml::decode(file_get_contents($module_path . '/config/override/views.view.localgov_directory_channel.yml'));
$view = View::load('localgov_directory_channel');
if ($view) {
$display = $view->get('display');
$display['embed_map'] = $view_with_map_embed['display']['embed_map'];
$view->set('display', $display);
$view->save();
}
}
/**
* Add new map embed display, but do not enable it as default.
*
* It is advised that installations remove the old attachment and enable the
* new map embed field on the directory channel view mode.
*/
function localgov_directories_location_update_8001() {
// Retrieve view display mode config and add it to the existing configuration.
$module_path = \Drupal::service('extension.list.module')->getPath('localgov_directories_location');
$view_with_map_embed = Yaml::decode(file_get_contents($module_path . '/config/override/views.view.localgov_directory_channel.yml'));
$view = View::load('localgov_directory_channel');
if ($view) {
$display = $view->get('display');
$display['embed_map'] = $view_with_map_embed['display']['embed_map'];
$view->set('display', $display);
$view->save();
}
// But don't enable it by default. This sets it as disabled before
// extra fields has even added it.
$directory_display_modes = \Drupal::service('entity_display.repository')
->getViewModeOptionsByBundle('node', 'localgov_directory');
foreach (array_keys($directory_display_modes) as $display_id) {
$directory_display = \Drupal::entityTypeManager()
->getStorage('entity_view_display')
->load('node.localgov_directory.' . $display_id);
$directory_display->removeComponent('localgov_directory_map')->save();
}
}
/**
* Sets up proximity search.
*/
function localgov_directories_location_update_8003() {
$proximity_search_setup = \Drupal::classResolver(ProximitySearchSetup::class);
if (!$proximity_search_setup->hasLocationSearch()) {
return t('Search index is not ready for location search.');
}
Drupal::service('module_installer')->install([
'search_api_location_views',
'search_api_location_geocoder',
]);
$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');
}
