lightning_api.module in Lightning API 8
File
lightning_api.module
View source
<?php
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
function lightning_api_modules_installed(array $modules) {
if (\Drupal::isConfigSyncing()) {
return;
}
if (in_array('lightning_dev', $modules, TRUE)) {
\Drupal::configFactory()
->getEditable('lightning_api.settings')
->set('entity_json', TRUE)
->set('bundle_docs', TRUE)
->save();
}
}
function lightning_api_entity_insert(EntityInterface $entity) {
$allowed = \Drupal::config('lightning_api.settings')
->get('entity_json');
if ($entity
->getEntityType()
->getBundleOf() && $allowed) {
\Drupal::service('router.builder')
->setRebuildNeeded();
}
}
function lightning_api_entity_json(EntityInterface $entity) {
$allowed = \Drupal::config('lightning_api.settings')
->get('entity_json');
$uuid = $entity
->uuid();
$resource_type = \Drupal::service('jsonapi.resource_type.repository')
->get($entity
->getEntityTypeId(), $entity
->bundle());
if ($allowed && $uuid && $resource_type) {
$route = 'jsonapi.' . $resource_type
->getTypeName() . '.individual';
try {
\Drupal::service('router.route_provider')
->getRouteByName($route);
return Url::fromRoute($route, [
$resource_type
->getEntityTypeId() => $uuid,
]);
} catch (RouteNotFoundException $e) {
}
}
}
function lightning_api_entity_operation(EntityInterface $entity) {
$operations = [];
$url = lightning_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('lightning_api.settings')
->get('bundle_docs')) {
$fragment = str_replace(' ', '-', sprintf('tag/%s-%s', \Drupal::entityTypeManager()
->getDefinition($bundle_of)
->getLabel(), $entity
->label()));
$operations['api-documentation'] = [
'title' => t('View API documentation'),
'url' => Url::fromRoute('openapi_redoc.jsonapi', [], [
'fragment' => $fragment,
]),
'weight' => 51,
];
}
return $operations;
}
function lightning_api_form_oauth2_token_settings_alter(array &$form, FormStateInterface $form_state, $form_id) {
$form['actions']['generate']['keys']['#access'] = FALSE;
}