-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvarbase_api.module
More file actions
231 lines (197 loc) · 8.02 KB
/
varbase_api.module
File metadata and controls
231 lines (197 loc) · 8.02 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
/**
* @file
* Contains varbase_api.module.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\views\ViewEntityInterface;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Drupal\jsonapi_extras\Entity\JsonapiResourceConfig;
/**
* Implements hook_entity_insert().
*/
function varbase_api_entity_insert(EntityInterface $entity) {
$entity_json_operation = Drupal::config('varbase_api.settings')->get('entity_json');
$entity_type_bundle = $entity->getEntityType()->getBundleOf();
if ($entity_type_bundle && $entity_json_operation) {
Drupal::service('router.builder')->rebuild();
Drupal::service('router.route_provider')->reset();
}
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function varbase_api_view_presave(ViewEntityInterface $view) {
if (Drupal::isConfigSyncing()) {
return;
}
elseif ($view->id() === 'content' && $view->isNew()) {
$display = &$view->getDisplay('default');
// If the Operations field's 'Include destination' switch is enabled, it
// will force every operation to have the 'destination' query parameter,
// which breaks JSON API 2.0, causing the 'View JSON' operation to throw a
// BadRequestHttpException.
if (isset($display['display_options']['fields']['operations']['destination'])) {
$display['display_options']['fields']['operations']['destination'] = FALSE;
}
}
}
/**
* Implements hook_entity_operation().
*/
function varbase_api_entity_operation(EntityInterface $entity) {
$operations = [];
if (\Drupal::currentUser()->hasPermission('access view json entity operation')
&& \Drupal::config('varbase_api.settings')->get('entity_json')
&& varbase_api_is_jsonapi_resource_config_enabled($entity)) {
$view_json_operation_url = varbase_api_get_view_json_operation_url($entity);
if (isset($view_json_operation_url) && $view_json_operation_url !== '') {
$operations['view-json'] = [
'title' => t('View JSON'),
'url' => $view_json_operation_url,
'weight' => 90,
'query' => ['destination' => []],
];
}
}
if (\Drupal::currentUser()->hasPermission('access view api docs entity operation')
&& \Drupal::currentUser()->hasPermission('access openapi api docs')
&& \Drupal::config('varbase_api.settings')->get('bundle_docs')
&& varbase_api_is_jsonapi_resource_config_enabled($entity)) {
$view_api_docs_operation_url = varbase_api_get_view_api_docs_operation_url($entity);
if (isset($view_api_docs_operation_url) && $view_api_docs_operation_url !== '') {
$operations['api-documentation'] = [
'title' => t('View API Docs'),
'url' => $view_api_docs_operation_url,
'weight' => 95,
'query' => ['destination' => []],
];
}
}
return $operations;
}
/**
* Check if the JSON:API resource config was enabled for this entity.
*/
function varbase_api_is_jsonapi_resource_config_enabled(EntityInterface $entity) {
// Get the JSON:API resource type.
$resource_config_id = sprintf('%s--%s', $entity->getEntityTypeId(), $entity->bundle());
$resource_type_existing_entity = \Drupal::service('jsonapi.resource_type.repository')->getByTypeName($resource_config_id);
if (isset($resource_type_existing_entity)) {
$jsonapi_resource_config_disabled = \Drupal::configFactory()->get('jsonapi_extras.jsonapi_resource_config.' . $resource_config_id)->get('disabled');
if (!$jsonapi_resource_config_disabled) {
return TRUE;
}
}
return FALSE;
}
/**
* Get the View JSON:API operation URL for this entity.
*/
function varbase_api_get_view_json_operation_url(EntityInterface $entity) {
$view_json_operation_url = '';
$uuid = $entity->uuid();
$entity_type_id = $entity->getEntityTypeId();
$bundle = $entity->bundle();
/** @var \Drupal\jsonapi\ResourceType\ResourceType $resource_type */
$resource_type = \Drupal::service('jsonapi.resource_type.repository')
->get(
$entity_type_id,
($bundle) ? $bundle : ''
);
if ($uuid && $resource_type) {
// JSON API routes are built dynamically per entity bundle. If for whatever
// reason the appropriate route does not exist yet, fail silently.
// @see varbase_api_entity_insert().
try {
$route_name = 'jsonapi.' . $resource_type->getTypeName() . '.individual';
Drupal::service('router.route_provider')->getRouteByName($route_name);
$route_parameters = [
'entity' => $uuid,
];
$view_json_operation_url = Url::fromRoute($route_name, $route_parameters);
}
catch (RouteNotFoundException $e) {
// No worries. The route will probably be built soon, most likely during
// the next general cache rebuild.
}
}
return $view_json_operation_url;
}
/**
* Get the View API Docs operation URL for this entity.
*/
function varbase_api_get_view_api_docs_operation_url(EntityInterface $entity) {
$view_api_docs_operation_url = '';
$entity_type_id = $entity->getEntityTypeId();
$bundle = $entity->bundle();
$entity_type_label = \Drupal::entityTypeManager()->getDefinition($entity_type_id)->getLabel();
$bundle_type_id = $entity->getEntityType()->getBundleEntityType();
$fragment = '';
if (empty($bundle_type_id)) {
$fragment = str_replace(' ', '-', sprintf('tag/%s', $entity_type_label));
}
else {
$bundle_type_label = \Drupal::entityTypeManager()->getStorage($bundle_type_id)->load($bundle)->label();
$fragment = str_replace(' ', '-', sprintf('tag/%s-%s', $entity_type_label, $bundle_type_label));
}
$route_parameters = [
'openapi_ui' => 'redoc',
// 'openapi_ui' => 'swagger',
'openapi_generator' => 'jsonapi',
];
try {
$view_api_docs_operation_url = Url::fromRoute('openapi.documentation', $route_parameters, ['fragment' => $fragment]);
}
catch (RouteNotFoundException $e) {
// No worries. The route will probably be built soon, most likely during
// the next general cache rebuild.
}
return $view_api_docs_operation_url;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function varbase_api_form_oauth2_token_settings_alter(array &$form, FormStateInterface $form_state, $form_id) {
// The key generation form provided by Simple OAuth doesn't generate unique
// key names (or allow the user to override the names) and doesn't allow the
// user to specify the location of the OpenSSL config file. Specifically, the
// fact that the names are always the same could cause problems on systems
// where the home directory stores keys for more than one application. So hide
// the link to that form and users can continue to use the one provided by
// Varbase API.
$form['actions']['generate']['keys']['#access'] = FALSE;
}
/**
* Implements hook_entity_bundle_create().
*/
function varbase_api_entity_bundle_create($entity_type_id, $bundle) {
$auto_enabled_entity_types = (array) \Drupal::config('varbase_api.settings')->get('auto_enabled_entity_types');
if (isset($auto_enabled_entity_types)
&& !in_array($entity_type_id, $auto_enabled_entity_types)) {
// Get the JSON:API resource type.
$resource_config_id = sprintf('%s--%s', $entity_type_id, $bundle);
$existing_entity = \Drupal::service('jsonapi.resource_type.repository')->getByTypeName($resource_config_id);
if (isset($existing_entity)) {
JsonapiResourceConfig::create([
'id' => $resource_config_id,
'disabled' => FALSE,
'path' => $entity_type_id . '/' . $bundle,
'resourceType' => $resource_config_id,
'resourceFields' => [],
])->save();
$config_factory = \Drupal::configFactory()->getEditable('jsonapi_extras.jsonapi_resource_config.' . $resource_config_id);
if (isset($config_factory)) {
$config_factory->set('disabled', TRUE)->save();
\Drupal::service('jsonapi.resource_type.repository')->reset();
$logger_values = [
'@entity_type_id' => $entity_type_id,
'@bundle' => $bundle,
];
\Drupal::logger('varbase_api')->info('Disabled JSON:API Endpoint for Entity Type: @entity_type_id Bundle: @bundle', $logger_values);
}
}
}
}