-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtide_station_locator.module
102 lines (92 loc) · 2.88 KB
/
tide_station_locator.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
* @file
* Tide Station Locator module functionality.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\migrate\MigrateMessage;
use Drupal\migrate_tools\MigrateExecutable;
/**
* This function is for accessing the API.
*/
function tide_station_locator_get_api_data($delta = FALSE) {
$station_api_connector = \Drupal::service('tide_station_locator.station_api_connector');
// Connect API.
$result = $station_api_connector->connectApi();
// Get API response.
$full_data = $station_api_connector->getApiResponse($result['clientId'], $result['accessToken'], $result['rdm_auth'], $delta);
if ($full_data && !empty($full_data['records'])) {
// Get formatted data and save the file.
$station_api_connector->getFormattedStationResponse($full_data['records']);
}
}
/**
* Function to migrate stations.
*/
function tide_station_locator_migrate_stations($options) {
try {
// Connect to API and download the latest station JSON and migrate.
tide_station_locator_get_api_data($options['delta']);
if ($options['execute-dependencies'] == TRUE) {
$dependent_migrations =
[
'station_accessibility_terms',
'station_speciality_services_facility_terms',
'station_state_terms',
];
foreach ($dependent_migrations as $migration_ids) {
_tide_station_locator_migrate($migration_ids);
}
}
_tide_station_locator_migrate('station_nodes');
}
catch (Exception $e) {
// Something went wrong somewhere.
watchdog_exception('tide_station_locator', $e);
}
}
/**
* Helper function to run the migration.
*/
function _tide_station_locator_migrate($migration_id) {
$migration = \Drupal::service('plugin.manager.migration')
->createInstance($migration_id);
$migration->getIdMap()->prepareUpdate();
$executable = new MigrateExecutable($migration, new MigrateMessage());
$executable->import();
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function tide_station_locator_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (in_array($form_id, ['node_station_edit_form', 'node_station_form'])) {
$fields = [
'title',
'field_accessibility',
'field_case_conference_email',
'field_case_conference_phone',
'field_email',
'field_fax',
'field_geolocation',
'field_google_maps_url',
'field_opening_hours',
'field_opening_hours_notice',
'field_phone',
'field_postcode',
'field_node_primary_site',
'field_prosecution_unit_email',
'field_prosecution_unit_fax',
'field_prosecution_unit_notice',
'field_prosecution_unit_phone',
'field_node_site',
'field_specialty_services_or_faci',
'field_state',
'field_station_code',
'field_street_address',
'field_suburb',
];
foreach ($fields as $field) {
$form[$field]['#disabled'] = TRUE;
}
}
}