-
Notifications
You must be signed in to change notification settings - Fork 13
/
open_data_schema_map.api.php
171 lines (155 loc) · 4.2 KB
/
open_data_schema_map.api.php
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
* @file
* Let there be hooks.
*/
/******** Add Endpoint in code *******/
/**
* Adds declared endpoint to list.
*
* This and hook_open_data_schema_map_load() are necessary so that modules can
* declare more than one endpoint.
*/
function hook_open_data_schema_map_endpoints_alter(&$records) {
$records[] = 'my_machine_name';
}
/**
* Loads endpoints with callback.
*
* @return object
* Record object with the following. All are required:
* - name
* - enabled
* - schema
* - type
* - bundle
* - arguments
* - description
* - machine_name
* - endpoint
* - callback
*/
function hook_open_data_schema_map_load($machine_name) {
if ($machine_name == 'my_machine_name') {
$record = new stdClass();
$record->name = 'My endpoint name';
$record->enabled = TRUE;
$record->schema = '';
$record->type = '';
$record->bundle = '';
$record->arguments = '';
$record->machine_name = 'my_machine_name';
$record->endpoint = 'api/action/3/my_endpoint';
$record->callback = 'my_module_endpoint_callback';
return $record;
}
}
/**
* Callback for my custom endpoint.
*
* @return array
* Results of my custom function or query.
*/
function my_module_endpoint_callback($queries, $args) {
// Here we can call a query on a single table or provide other callback to
// generate items. This endpoint has not arguments so ignoring those.
$items = my_sudo_code_query();
$results = array(
'description' => t('My endpoint'),
'results' => $items,
);
return $results;
}
/******** Add schema *******/
/**
* Declare new open data schema.
*/
function hook_open_data_schema() {
return array(
'short_name' => 'new_schema',
'title' => 'MY New Schema',
// This is the path to the schema. Schema MUST be in json 3 or json
// 4 format.
'schema_file' => $path,
'description' => t('This new schema rocks.'),
);
}
/**
* Allows adding new schema types.
*
* Currently onl json-4 and json-3 are accepted.
*/
function hook_open_data_schema_map_schema_types_alter(&$schemas) {
}
/******** Update results *******/
/**
* Allows overriding final results about to be rendered.
*/
function hook_open_data_schema_map_results_alter(&$result, $api_machine_name, $schema) {
if ($schema == 'new_schema') {
// Wrap results in 'output' array.
$result['output'] = $results;
}
}
/**
* Allows changing the output of a processed field.
*/
function hook_open_data_schema_map_process_field_alter(&$result, $api_field, $token) {
if ($api_field == 'foo') {
$result = 'bar';
}
}
/**
* Allows altering of arguments before they are queried.
*
* @param array $token
* Exploded token. This will be queried by $query->fieldConditioin if
* $token[0] = 'node' or $query->propertyCondition if $token[0] = 'field'.
* $token[1] will be the node property or field queried.
* @param array $arg
* Exploded argument.
*/
function hook_open_data_schema_map_args_alter(&$token, &$arg) {
if ($arg['token']['value'] == '[node:url:arg:last]') {
// Change property being queried. The nid column in the node table will now
// be queried.
$token[1] = 'nid';
}
}
/******** Use features to provide defaults *******/
/**
* Provide defaults using features module.
*
* @return array
* Exported mapping for ODSM endpoint.
*/
function hook_open_data_schema_apis_defaults() {
$open_data_schema_apis = array();
// Example endpoint:
$open_data_schema_apis['my_endpoint'] = array(
'name' => 'My Custom Endpoint',
// Is enabled by default?
'enabled' => 1,
'machine_name' => 'my_endpoint',
// Machine readbale schema.
'api_schema' => 'pod',
'description' => NULL,
'type' => 'node',
'bundle' => 'dataset',
// The schema fields and the associated tokens.
// See: https://github.com/GetDKAN/dkan/blob/7.x-1.x/modules/dkan/open_data_schema_map_dkan/open_data_schema_map_dkan.features.inc#L1273
// for example.
'mapping' => array(
),
'outputformat' => 'json',
'endpoint' => 'data.json',
'arguments' => array(
1 => array(
'field' => 'identifier',
'required' => 0,
),
),
'xml_root' => 'records',
'xml_defaulttag' => 'record',
);
}