Something went wrong on our end
-
Eric Bremner authoredEric Bremner authored
uw_ct_service.module 5.59 KiB
<?php
/**
* @file
* Provides configuration and settings for services.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
/**
* Implements hook_geofield_map_latlon_element_alter().
*/
function uw_ct_service_geofield_map_latlon_element_alter(array &$map_settings, array &$complete_form, array &$form_state_values) {
// Library for editing uw_ct_service.
if ($map_settings['id'] === 'edit-field-uw-service-location-coord-0-value') {
$complete_form['#attached']['library'][] = 'uw_ct_service/uw_ct_service_edit';
}
}
/**
* Load data about UW locations from a JSON file.
*
* @return array[]
* The location data.
*/
function _uw_ct_service_load_locations(): array {
// Cached results of this function.
static $locations;
if ($locations) {
return $locations;
}
// Load data file.
$locations_file = file_get_contents(__DIR__ . '/uw_locations.json');
$locations_file = json_decode($locations_file, TRUE);
// Field mapping for renaming some keys.
$fields = [
'street' => 'address',
'city' => NULL,
'province' => 'province_code',
'province_name' => NULL,
'country' => 'country_code',
'country_name' => NULL,
'postal_code' => NULL,
'latitude' => NULL,
'longitude' => NULL,
];
$locations = [];
foreach ($locations_file as $file_location) {
// Parse name into code and name.
list($location_code, $location_name) = explode('-', $file_location['name'], 2);
$location_code = trim($location_code);
// Create array for this location.
$location = [
'location_code' => $location_code,
'location_name' => trim($location_name),
];
$location['location_name_and_code'] = $location['location_code'] . ' - ' . $location['location_name'];
// Copy values from file, renaming some keys.
foreach ($fields as $field_in => $field_out) {
$location[$field_out ?: $field_in] = $file_location[$field_in];
}
// Use capital letters for country codes.
$location['country_code'] = strtoupper($location['country_code']);
// Append to main array.
$locations[$location_code] = $location;
}
return $locations;
}
/**
* Return locations as a $code => $title array.
*
* @return string[]
* An array of locations with their codes as the keys and names as the values.
*/
function _uw_ct_service_get_location_options(): array {
// Cached results of this function.
static $options;
if ($options) {
return $options;
}
$options = [];
foreach (_uw_ct_service_load_locations() as $location_code => $location) {
$options[$location_code] = $location_code . ' - ' . $location['location_name'];
}
return $options;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function uw_ct_service_form_node_uw_ct_service_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
// Add locations pre-fill menu.
$form['group_service_location']['location_presets_select'] = [
'#title' => t('Location lookup'),
'#description' => t('Choose a location or enter location information below.'),
'#type' => 'select',
'#empty_option' => '- Choose location -',
'#options' => _uw_ct_service_get_location_options(),
];
$form['#attached']['library'][] = 'uw_ct_service/location_autofill';
$form['#attached']['drupalSettings']['uwCtService'] = _uw_ct_service_load_locations();
}
/**
* Implements hook_form_alter().
*/
function uw_ct_service_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
switch ($form_id) {
case 'node_uw_ct_service_edit_form':
case 'node_uw_ct_service_quick_node_clone_form':
uw_ct_service_form_node_uw_ct_service_form_alter($form, $form_state, $form_id);
break;
}
}
/**
* Implements hook_preprocess_views_view().
*/
function uw_ct_service_preprocess_views_view(&$variables)
{
// Get the view variable.
$view = $variables['view'];
// Get the display of the view.
$id = $view->getDisplay()->display['id'];
// The array of view ids for services.
$services_ids = [
'services_page',
'popular_services_page',
'services_by_audience_page',
'services_in_category_page',
'all_services_page',
];
// Putting the service search form into
// variables so that views template can use it.
if (in_array($id, $services_ids)) {
// Put the form into the variables.
$variables['form_search'] = \Drupal::formBuilder()->getForm('Drupal\uw_ct_service\Form\ServiceSearchForm');
// Get the current path and put into array based on
// the slashes.
$current_path = \Drupal::service('path.current')->getPath();
$current_path_parts = explode('/', $current_path);
// If we have something past /services add the tabs.
if (isset($current_path_parts[2])) {
// Tab for Category.
$content_list[] = [
'url' => '/taxonomy/term/' . end($current_path_parts),
'text' => 'Category',
'active' => strpos($current_path, '/taxonomy/term/') !== false ? 1 : 0,
];
// Tab for A-Z.
$content_list[] = [
'url' => '/services/all',
'text' => 'A-Z',
'active' => $current_path == '/services/all' ? 1 : 0,
];
// Tab for Audience.
$content_list[] = [
'url' => '/services/audience',
'text' => 'Audience',
'active' => strpos($current_path, '/services/audience/') !== false ? 1 : 0,
];
// Tab for Popular.
$content_list[] = [
'url' => '/services/popular',
'text' => 'Popular',
'active' => strpos($current_path, '/services/audience/') !== false ? 1 : 0,
];
// Set the tabs in the variables.
$variables['content_list'] = $content_list;
}
}
}