You are here

function lightning_api_install in Lightning API 8

Same name and namespace in other branches
  1. 8.4 lightning_api.install \lightning_api_install()
  2. 8.2 lightning_api.install \lightning_api_install()
  3. 8.3 lightning_api.install \lightning_api_install()

Implements hook_install().

File

./lightning_api.install, line 10

Code

function lightning_api_install() {
  $module_handler = \Drupal::moduleHandler();

  // If the openapi_redoc and jsonapi modules are installed, give the API
  // documentation a nice path alias.
  if ($module_handler
    ->moduleExists('openapi_redoc') && $module_handler
    ->moduleExists('jsonapi')) {

    /** @var \Drupal\Core\Path\AliasStorageInterface $alias_storage */
    $alias_storage = \Drupal::service('path.alias_storage');
    $path = $alias_storage
      ->lookupPathSource('/api-docs', LanguageInterface::LANGCODE_NOT_SPECIFIED);
    if (empty($path)) {
      $route = 'openapi_redoc.jsonapi';
      try {
        \Drupal::urlGenerator()
          ->getPathFromRoute($route);
      } catch (RouteNotFoundException $e) {
        \Drupal::service('router.builder')
          ->rebuild();
      }
      $alias_storage
        ->save('/' . Url::fromRoute($route)
        ->getInternalPath(), '/api-docs');
    }
  }
}