You are here

lightning_api.install in Lightning API 8

File

lightning_api.install
View source
<?php

use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\Url;
use Symfony\Component\Routing\Exception\RouteNotFoundException;

/**
 * Implements hook_install().
 */
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');
    }
  }
}

/**
 * Sets a default value for lightning_api.settings:bundle_docs.
 */
function lightning_api_update_8001() {
  \Drupal::configFactory()
    ->getEditable('lightning_api.settings')
    ->set('bundle_docs', TRUE)
    ->save();
}

/**
 * Installs the Consumers module.
 */
function lightning_api_update_8002() {
  \Drupal::service('module_installer')
    ->install([
    'consumers',
  ]);
}

Functions

Namesort descending Description
lightning_api_install Implements hook_install().
lightning_api_update_8001 Sets a default value for lightning_api.settings:bundle_docs.
lightning_api_update_8002 Installs the Consumers module.