Skip to content
Snippets Groups Projects
uw_ct_service.module 3.63 KiB
<?php

/**
 * @file
 * Provides configuration and settings for services.
 */

use Drupal\Core\Form\FormStateInterface;

/**
 * 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;
  }
}