-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgraphql_compose.module
More file actions
281 lines (229 loc) · 8.4 KB
/
graphql_compose.module
File metadata and controls
281 lines (229 loc) · 8.4 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?php
/**
* @file
* GraphQL Compose module file.
*/
declare(strict_types=1);
use Drupal\Component\Utility\Xss;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\EntityFormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\field\Entity\FieldConfig;
/**
* Implements hook_help().
*/
function graphql_compose_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.graphql_compose':
$path = \Drupal::service('extension.list.module')->getPath('graphql_compose');
$output = file_get_contents($path . '/README.md');
return '<pre>' . Xss::filterAdmin($output) . '</pre>';
}
}
/**
* Implements hook_form_alter().
*
* Enable config on entity type form.
*/
function graphql_compose_form_alter(&$form, FormStateInterface $form_state, $form_id): void {
$entityTypeManager = \Drupal::entityTypeManager();
$gqlEntityTypeManager = \Drupal::service('graphql_compose.entity_type_manager');
$form_object = $form_state->getFormObject();
if (!$form_object instanceof EntityFormInterface) {
return;
}
$entity = $form_object->getEntity();
if (!$entity instanceof ConfigEntityInterface) {
return;
}
if (stristr($form_id, '_delete_') || stristr($form_id, '_confirm_')) {
return;
}
// Shortlist the enabled entity types.
$enabled_entities = [FieldConfig::class];
$entity_type_plugins = $gqlEntityTypeManager->getDefinitions();
foreach (array_keys($entity_type_plugins) as $entity_type_plugin_id) {
if ($type = $entityTypeManager->getDefinition($entity_type_plugin_id, FALSE)) {
$enabled_entities[] = $type->getClass();
}
}
// Set this form entity type.
$entity_type = $base_type = $entity->getEntityType();
if ($bundle_of = $entity->getEntityType()->getBundleOf()) {
$base_type = $entityTypeManager->getDefinition($bundle_of);
}
if (!in_array($base_type->getClass(), $enabled_entities)) {
return;
}
$form_state->set('entity_type', $entity_type);
$form_state->set('base_type', $base_type);
_graphql_compose_entity_type_settings_alter($form, $form_state);
}
/**
* Alter form with GraphQL settings.
*/
function _graphql_compose_entity_type_settings_alter(&$form, FormStateInterface $form_state): void {
$config = \Drupal::config('graphql_compose.settings');
$form_object = $form_state->getFormObject();
$entity = ($form_object instanceof EntityFormInterface)
? $form_object->getEntity()
: NULL;
$form_state->set('entity', $entity);
$entity_type = $form_state->get('entity_type');
$base_type = $form_state->get('base_type');
$target = $entity ? $entity->getConfigTarget() : $entity_type->id();
$settings = $config->get($base_type->id() . '.' . $target) ?: [];
\Drupal::moduleHandler()->invokeAll('graphql_compose_entity_type_form_alter', [
&$form,
$form_state,
$settings,
]);
// https://www.drupal.org/project/drupal/issues/2252165
// Its not so much an issue, just a work around.
if (isset($form['actions']['submit']['#submit'])) {
$form['actions']['submit']['#submit'][] = '_graphql_compose_entity_type_settings_submit';
}
if (isset($form['actions']['save_continue']['#submit'])) {
$form['actions']['save_continue']['#submit'][] = '_graphql_compose_entity_type_settings_submit';
}
$form['#submit'][] = '_graphql_compose_entity_type_settings_submit';
}
/**
* Callback on settings submit.
*/
function _graphql_compose_entity_type_settings_submit(&$form, FormStateInterface $form_state): void {
$settings = [];
\Drupal::moduleHandler()->invokeAll('graphql_compose_entity_type_form_submit_alter', [
$form,
$form_state,
&$settings,
]);
/** @var \Drupal\Core\Entity\EntityInterface|null $entity */
$entity = $form_state->get('entity');
/** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */
$entity_type = $form_state->get('entity_type');
// Reload the entity by uuid.
// Helps deal with isNew items.
if ($entity->uuid()) {
$entity = \Drupal::service('entity.repository')
->loadEntityByUuid($entity_type->id(), $entity->uuid());
}
$target = $entity ? $entity->getConfigTarget() : $entity_type->id();
if (!$target) {
return;
}
$config = \Drupal::service('config.factory')->getEditable('graphql_compose.settings');
$base_type = $form_state->get('base_type');
$cid = $base_type->id() . '.' . $target;
// Cleanup or set.
if (empty($settings['enabled'])) {
$config->clear($cid);
}
else {
$config->set($cid, $settings);
}
$config->save();
_graphql_compose_cache_flush();
}
/**
* Implements hook_graphql_compose_entity_type_form_alter().
*
* Modify form to add GraphQL Compose settings.
*/
function graphql_compose_graphql_compose_entity_type_form_alter(array &$form, FormStateInterface $form_state, array $settings): void {
$form['graphql_compose'] = [
'#type' => 'details',
'#title' => t('GraphQL'),
'#tree' => TRUE,
'#weight' => 90,
'#group' => isset($form['additional_settings']) ? 'additional_settings' : NULL,
];
$form['graphql_compose']['enabled'] = [
'#type' => 'checkbox',
'#title' => t('Enable GraphQL'),
'#default_value' => $settings['enabled'] ?? FALSE,
];
$base_type = $form_state->get('base_type');
if ($base_type->getClass() === FieldConfig::class) {
$form['graphql_compose']['name_sdl'] = [
'#type' => 'textfield',
'#title' => t('Schema field name'),
'#default_value' => $settings['name_sdl'] ?? NULL,
'#description' => t('Leave blank to use automatically generated name.'),
'#element_validate' => ['_graphql_compose_validate_name_sdl'],
];
}
if ($base_type instanceof ContentEntityTypeInterface) {
$form['graphql_compose']['query_load_enabled'] = [
'#type' => 'checkbox',
'#title' => t('Enable single query'),
'#default_value' => $settings['query_load_enabled'] ?? FALSE,
'#description' => t('Enable a query to load this type by UUID.'),
];
}
}
/**
* Callback for name sdl validation.
*
* @see hook_graphql_compose_entity_type_form_alter()
*/
function _graphql_compose_validate_name_sdl(array &$element, FormStateInterface &$form_state, array $form): void {
$field_value = $element['#value'];
if (!empty($field_value) && !preg_match('/^[a-z0-9]+$/i', $field_value)) {
$form_state->setError($element, t('Name can only include letters and numbers.'));
}
}
/**
* Implements hook_graphql_compose_entity_type_form_submit_alter().
*/
function graphql_compose_graphql_compose_entity_type_form_submit_alter(array $form, FormStateInterface $form_state, array &$settings): void {
$values = $form_state->getValue('graphql_compose', []);
$settings['enabled'] = (bool) $values['enabled'] ?: FALSE;
$base_type = $form_state->get('base_type');
if ($base_type instanceof ContentEntityTypeInterface) {
$settings['query_load_enabled'] = (bool) $values['query_load_enabled'] ?: FALSE;
}
// Only add field name to config, if there is a value.
if ($base_type->getClass() === FieldConfig::class) {
if ($name = $values['name_sdl'] ?: NULL) {
$settings['name_sdl'] = $name;
}
}
}
/**
* Implements hook_entity_predelete().
*
* Remove config on entity type deletion.
*/
function graphql_compose_entity_predelete(EntityInterface $entity): void {
if (!$entity instanceof ConfigEntityInterface) {
return;
}
$config = \Drupal::service('config.factory')->getEditable('graphql_compose.settings');
$entityTypeManager = \Drupal::entityTypeManager();
$entity_type = $base_type = $entity->getEntityType();
if ($bundle_of = $entity->getEntityType()->getBundleOf()) {
$base_type = $entityTypeManager->getDefinition($bundle_of);
}
$target = $entity ? $entity->getConfigTarget() : $entity_type->id();
$cid = $base_type->id() . '.' . $target;
if ($target && $config->get($cid)) {
$config->clear($cid);
// Any fields associated with this.
$config->clear('field_config.' . $cid);
$config->save();
_graphql_compose_cache_flush();
}
}
/**
* Utility function to nuke cache for GraphQL.
*/
function _graphql_compose_cache_flush(): void {
\Drupal::service('cache.graphql.ast')->invalidateAll();
\Drupal::service('cache.graphql.definitions')->invalidateAll();
\Drupal::service('cache.graphql.results')->invalidateAll();
\Drupal::service('cache.graphql_compose.definitions')->invalidateAll();
}