social_geolocation-8.x-1.2/modules/social_geolocation_maps/social_geolocation_maps.install
modules/social_geolocation_maps/social_geolocation_maps.install
<?php
/**
* @file
* Install, update and uninstall functions for social_geolocation_maps module.
*/
use Drupal\Core\Config\FileStorage;
use Drupal\Core\Config\InstallStorage;
use Symfony\Component\Yaml\Yaml;
/**
* Add 'Social Geolocation Members' map block.
*/
function social_geolocation_maps_update_8001(&$sandbox): void {
$configs = [
'view' => [
'config_id' => 'social_geolocation_members',
'config_file' => 'views.view.social_geolocation_members',
'location' => InstallStorage::CONFIG_INSTALL_DIRECTORY,
],
'block' => [
'config_id' => 'views_block__social_geolocation_members_members_map_block',
'config_file' => 'block.block.views_block__social_geolocation_members_members_map_block',
'location' => InstallStorage::CONFIG_OPTIONAL_DIRECTORY,
],
];
$base_path = \Drupal::service('extension.path.resolver')->getPath('module', 'social_geolocation_maps');
foreach ($configs as $type => $config) {
$storage = \Drupal::entityTypeManager()->getStorage($type);
// Only create if the social_geolocation_members view doesn't exist.
if (!$storage->load($config['config_id'])) {
$new_storage = new FileStorage($base_path . '/' . $config['location']);
$storage->create($new_storage->read($config['config_file']))
->save();
}
}
// Display 'Social Geolocation Members' map block.
\Drupal::configFactory()->getEditable('social_geolocation_maps.settings')
->set('members_map', TRUE)
->save();
}
/**
* Removing pagination.
*
* From Social Geolocation - Leaflet - CommonMap with marker icons view.
*/
function social_geolocation_maps_update_8002(&$sandbox): void {
$config_file = \Drupal::service('extension.path.resolver')->getPath('module', 'social_geolocation_maps') . '/config/static/views.view.social_geolocation_leaflet_commonmap_with_marker_icons_8002.yml';
if (is_file($config_file)) {
$views_configuration = Yaml::parse(file_get_contents($config_file));
if (is_array($views_configuration)) {
$config = \Drupal::configFactory()
->getEditable('views.view.social_geolocation_leaflet_commonmap_with_marker_icons');
$config->setData($views_configuration)->save(TRUE);
}
}
}
/**
* Change upcoming events filtration on events map.
*
* Replace filter "field_event_date" with "field_event_date_end".
*/
function social_geolocation_maps_update_9001(&$sandbox): void {
$config = \Drupal::configFactory()
->getEditable('views.view.social_geolocation_leaflet_commonmap_with_marker_icons');
$data = $config->getRawData();
$displays_to_change = [
'block_community_events_map',
'block_upcomming_events_map'
];
foreach ($displays_to_change as $display_to_change) {
if (isset($data['display'][$display_to_change]['display_options']['filters']['field_event_date_value'])) {
$date_field_config = $data['display'][$display_to_change]['display_options']['filters']['field_event_date_value'];
// Change field_event_date to field_event_date_end.
// Old value: 'field_event_date_value'.
$date_field_config['id'] = 'field_event_date_end_value';
// Old value: 'node__field_event_date'.
$date_field_config['table'] = 'node__field_event_date_end';
// Old value: 'field_event_date_value'.
$date_field_config['field'] = 'field_event_date_end_value';
// Change exposed filter settings for block_community_events_map
if ($display_to_change === 'block_community_events_map') {
// Old value: 'field_event_date_value_op'.
$date_field_config['expose']['operator_id'] = 'field_event_date_end_value_op';
// Old value: 'Start (field_event_date)'.
$date_field_config['expose']['label'] = 'End (field_event_date_end)';
// Old value: 'field_event_date_value_op'.
$date_field_config['expose']['operator'] = 'field_event_date_end_value_op';
// Old value: 'field_event_date_value'.
$date_field_config['expose']['identifier'] = 'field_event_date_end_value';
// Old value: 'Start (field_event_date)'.
$date_field_config['group_info']['label'] = '';
// Old value: 'Upcoming events'.
$date_field_config['group_info']['group_items']['1']['title'] = 'Ongoing and upcoming events';
// Old value: 'Events that have started or are finished'.
$date_field_config['group_info']['group_items']['2']['title'] = 'Past events';
}
// Change field_event_date_value key and replace it with field_event_date_end
$filters = $data['display'][$display_to_change]['display_options']['filters'];
foreach ($filters as $key => $filter) {
if ($key === 'field_event_date_value') {
$updated_filters['field_event_date_end_value'] = $date_field_config;
} else {
$updated_filters[$key] = $filter;
}
}
$data['display'][$display_to_change]['display_options']['filters'] = $updated_filters;
}
}
$config->setData($data)->save(TRUE);
}
/**
* Update event maps views.
*
* Revert exposed identifier on upcoming events filtration on events map,
* set exposed form in block as true, to hide the exposed filter in view,
* because filtration is handled is sidebar filter block and set
* "Ongoing and upcoming events" as default group filter option in
* field_event_date_end_value filter.
*/
function social_geolocation_maps_update_9002(&$sandbox): void {
$config = \Drupal::configFactory()
->getEditable('views.view.social_geolocation_leaflet_commonmap_with_marker_icons');
$data = $config->getRawData();
// Revert exposed identifier on upcoming events filtration on events map
// that was wrongly changed in social_geolocation_maps_update_9001.
if (isset($data['display']['block_community_events_map']['display_options']['filters']['field_event_date_end_value']['expose']['identifier'])) {
$data['display']['block_community_events_map']['display_options']['filters']['field_event_date_end_value']['expose']['identifier'] = 'field_event_date_value';
}
// Set exposed form in block as true, to hide the exposed filter in view,
// because filtration is handled is sidebar filter block.
if (isset($data['display']['block_community_events_map']['display_options'])) {
$data['display']['block_community_events_map']['display_options']['exposed_block'] = TRUE;
}
// Set "Ongoing and upcoming events" as default group filter option in
// field_event_date_end_value filter.
if (isset($data['display']['block_community_events_map']['display_options']['filters']['field_event_date_end_value']['group_info'])) {
$data['display']['block_community_events_map']['display_options']['filters']['field_event_date_end_value']['group_info']['default_group'] = '1';
}
$config->setData($data)->save(TRUE);
}
/**
* Update event maps views.
*
* Change the field_event_date_end filter back to field_event_date.
* Because we have a problem with displaying events on the map -
* if the field field_event_date_end has today's date
* and the all day field is activated, the event is not displayed on the map,
* although it is still ongoing. Since the custom filter event_date
* has been modified and it fixes this case,
* it makes sense to bring this filter back to filter events on the map.
*/
function social_geolocation_maps_update_9003(&$sandbox): string {
/** @var \Drupal\update_helper\Updater $updateHelper */
$updateHelper = \Drupal::service('update_helper.updater');
// Execute configuration update definitions with logging of success.
$updateHelper->executeUpdate('social_geolocation_maps', __FUNCTION__);
// Output logged messages to related channel of update execution.
return $updateHelper->logger()->output();
}
/**
* Add 'Social Geolocation Groups' map block.
*/
function social_geolocation_maps_update_9004(): void {
// Prepare the path.
$path = \Drupal::service('extension.list.module')->getPath('social_geolocation_maps') . '/config/static/';
$configs = [
'views.view.social_geolocation_groups',
'block.block.views_block__social_geolocation_groups_groups_map_block'
];
// Retrieves the configuration factory.
$config_factory = \Drupal::configFactory();
// Create each configuration.
foreach ($configs as $config_name) {
$config_file = $path . $config_name . '_9003.yml';
if (is_file($config_file)) {
$settings = Yaml::parse(file_get_contents($config_file));
if (is_array($settings)) {
$config = $config_factory->getEditable($config_name);
$config->setData($settings)->save(TRUE);
}
}
}
}
