graphql_compose-1.0.0-beta20/graphql_compose.module
graphql_compose.module
<?php
/**
* @file
* GraphQL Compose module file.
*/
declare(strict_types=1);
use Drupal\Component\Utility\Xss;
use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
/**
* 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_entity_predelete().
*
* Remove config on entity type deletion.
*/
function graphql_compose_entity_predelete(EntityInterface $entity): void {
if (!$entity instanceof ConfigEntityInterface) {
return;
}
// Light weight check for server IDs.
$server_ids = \Drupal::entityTypeManager()
->getStorage('graphql_server')
->getQuery()
->accessCheck(FALSE)
->condition('schema', 'graphql_compose')
->execute();
foreach ($server_ids as $server_id) {
$config = \Drupal::service('config.factory')->getEditable('graphql_compose.settings.' . $server_id);
$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();
}
/**
* Implements hook_modules_installed().
*
* Clear cache when GraphQL Compose module dependency is installed.
*/
function graphql_compose_modules_installed($modules, bool $is_syncing): void {
if ($is_syncing) {
return;
}
// Only get the extensions being installed.
$service = \Drupal::service('extension.list.module');
/** @var \Drupal\Core\Extension\Extension[] $extensions */
$extensions = array_combine(
$modules,
array_map(fn ($key) => $service->get($key), $modules)
);
$extensions = array_filter(
$extensions,
fn ($extension) => isset($extension->requires['graphql_compose'])
);
$extensions = array_combine(
array_keys($extensions),
array_keys($extensions)
);
if (empty($extensions)) {
return;
}
/** @var \Drupal\graphql\Entity\ServerInterface[] $servers */
$servers = \Drupal::entityTypeManager()
->getStorage('graphql_server')
->loadByProperties([
'schema' => 'graphql_compose',
]);
// Enable this module by default in any GraphQL Compose servers.
foreach ($servers as $server) {
$config = $server->get('schema_configuration');
$config['graphql_compose']['providers'] += $extensions;
$server->set('schema_configuration', $config);
$server->save();
}
// Flush the cache for GraphQL Compose.
_graphql_compose_cache_flush();
}
/**
* Implements hook_modules_uninstalled().
*
* Clear cache when GraphQL Compose module dependency is uninstalled.
*/
function graphql_compose_module_preuninstall($module, bool $is_syncing): void {
if ($is_syncing) {
return;
}
// Only get the extensions being installed.
$service = \Drupal::service('extension.list.module');
$extension = $service->get($module);
if (!isset($extension->requires['graphql_compose'])) {
return;
}
/** @var \Drupal\graphql\Entity\ServerInterface[] $servers */
$servers = \Drupal::entityTypeManager()
->getStorage('graphql_server')
->loadByProperties([
'schema' => 'graphql_compose',
]);
// Remove this module from any GraphQL Compose servers.
foreach ($servers as $server) {
$config = $server->get('schema_configuration');
$config['graphql_compose']['providers'] = array_filter(
$config['graphql_compose']['providers'] ?? [],
fn ($provider) => $provider !== $module,
ARRAY_FILTER_USE_KEY
);
$server->set('schema_configuration', $config);
$server->save();
}
}
/**
* Implements hook_entity_operation().
*
* Add JS UUID copy to clipboard utility to entity operation.
*/
function graphql_compose_entity_operation(EntityInterface $entity): array {
$operations = [];
$enabled_types = \Drupal::service('graphql_compose.entity_type_manager')->getDefinitions();
$enabled = in_array($entity->getEntityTypeId(), array_keys($enabled_types));
if ($entity instanceof ContentEntityInterface && $enabled) {
$operations['copy_uuid'] = [
'title' => t('Copy UUID'),
'weight' => 100,
'url' => Url::fromRoute('<current>', [], [
'attributes' => [
'class' => 'graphql-compose--uuid-link',
'data-uuid' => $entity->uuid(),
],
]),
];
}
return $operations;
}
/**
* Implements hook_preprocess_links__dropbutton__operations().
*/
function graphql_compose_preprocess_links__dropbutton__operations(array &$variables): void {
if (array_key_exists('copy_uuid', $variables['links'] ?? [])) {
$variables['#attached']['library'][] = 'graphql_compose/uuid.admin';
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function graphql_compose_form_graphql_server_create_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
_graphql_compose_schema_options($form, $form_state);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function graphql_compose_form_graphql_server_edit_form_alter(array &$form, FormStateInterface $form_state, string $form_id): void {
_graphql_compose_schema_options($form, $form_state);
}
/**
* Callback from hook_form_FORM_ID_alter().
*
* Remove GraphQL extensions from other schemas.
*/
function _graphql_compose_schema_options(&$form, FormStateInterface $form_state): void {
$schema = $form['schema']['#default_value'] ?? NULL;
$options =& $form['schema_configuration'][$schema]['extensions']['#options'] ?? [];
if ($schema && $schema !== 'graphql_compose' && $options) {
$compose_extensions = array_filter(
\Drupal::service('plugin.manager.graphql.schema_extension')->getDefinitions(),
fn ($ext) => $ext['schema'] === 'graphql_compose'
);
$options = array_diff_key($options, $compose_extensions);
}
$form['actions']['submit']['#submit'] = array_merge(
['_graphql_compose_cache_flush'],
$form['actions']['submit']['#submit'],
);
}
/**
* Utility function to nuke cache for GraphQL.
*/
function _graphql_compose_cache_flush(): void {
\Drupal::cache('graphql.apq')->deleteAll();
\Drupal::cache('graphql.ast')->deleteAll();
\Drupal::cache('graphql.definitions')->deleteAll();
\Drupal::cache('graphql.results')->deleteAll();
\Drupal::cache('graphql_compose.definitions')->deleteAll();
}
