forked from Vardot/varbase_api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
varbase_api.module
134 lines (116 loc) · 4.18 KB
/
varbase_api.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
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
<?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;
/**
* Implements hook_entity_insert().
*/
function varbase_api_entity_insert(EntityInterface $entity) {
$allowed = Drupal::config('varbase_api.settings')->get('entity_json');
if ($entity->getEntityType()->getBundleOf() && $allowed) {
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;
}
}
}
/**
* Determines the URL of the JSON API representation of an entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity.
*
* @return \Drupal\Core\Url
* The URL to the JSON API representation of the entity.
*/
function varbase_api_entity_json(EntityInterface $entity) {
$allowed = \Drupal::config('varbase_api.settings')->get('entity_json');
$uuid = $entity->uuid();
/** @var \Drupal\jsonapi\ResourceType\ResourceType $resource_type */
$resource_type = \Drupal::service('jsonapi.resource_type.repository')
->get(
$entity->getEntityTypeId(),
$entity->bundle()
);
if ($allowed && $uuid && $resource_type) {
$route_name = 'jsonapi.' . $resource_type->getTypeName() . '.individual';
// 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 {
Drupal::service('router.route_provider')->getRouteByName($route_name);
return Url::fromRoute($route_name, ['entity' => $uuid]);
}
catch (RouteNotFoundException $e) {
// No worries. The route will probably be built soon, most likely during
// the next general cache rebuild.
}
}
}
/**
* Implements hook_entity_operation().
*/
function varbase_api_entity_operation(EntityInterface $entity) {
$operations = [];
$url = varbase_api_entity_json($entity);
if ($url) {
$operations['view-json'] = [
'title' => t('View JSON'),
'url' => $url,
'weight' => 50,
];
}
$bundle_of = $entity->getEntityType()->getBundleOf();
if ($bundle_of && \Drupal::config('varbase_api.settings')->get('bundle_docs')) {
$fragment = str_replace(' ', '-', sprintf(
'tag/%s-%s',
\Drupal::entityTypeManager()->getDefinition($bundle_of)->getLabel(),
$entity->label()
));
$route_parameters = [
'openapi_ui' => 'redoc',
'openapi_generator' => 'jsonapi',
];
$operations['api-documentation'] = [
'title' => t('View API documentation'),
'url' => Url::fromRoute('openapi.documentation', $route_parameters, ['fragment' => $fragment]),
'weight' => 51,
];
}
return $operations;
}
/**
* 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;
}