You are here

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;

/**
 * Implements hook_modules_installed().
 */
function lightning_api_modules_installed(array $modules) {

  // Don't change config during a sync.
  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();
  }
}

/**
 * Implements hook_entity_insert().
 */
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();

  /** @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 = '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 lightning_api_entity_insert().
    try {
      \Drupal::service('router.route_provider')
        ->getRouteByName($route);
      return Url::fromRoute($route, [
        $resource_type
          ->getEntityTypeId() => $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 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;
}

/**
 * Implements hook_form_FORM_ID_alter.
 */
function lightning_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
  // Lightning API.
  $form['actions']['generate']['keys']['#access'] = FALSE;
}