You are here

lightning_api.install in Lightning API 8.4

Contains installation and update routines for Lightning API.

File

lightning_api.install
View source
<?php

/**
 * @file
 * Contains installation and update routines for Lightning API.
 */
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\path_alias\Entity\PathAlias;
use Drupal\views\Entity\View;

/**
 * Implements hook_install().
 */
function lightning_api_install() {
  $module_handler = \Drupal::moduleHandler();

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

    /** @var \Drupal\path_alias\AliasRepository $alias_storage */
    $alias_storage = \Drupal::service('path_alias.repository');
    $path = $alias_storage
      ->lookupByAlias('/api-docs', LanguageInterface::LANGCODE_NOT_SPECIFIED);
    if (empty($path)) {
      $route_parameters = [
        'openapi_ui' => 'redoc',
        'openapi_generator' => 'jsonapi',
      ];
      PathAlias::create([
        'path' => Url::fromRoute('openapi.documentation', $route_parameters)
          ->toString(),
        'alias' => '/api-docs',
      ])
        ->save();
    }
  }

  // Disable the content view's 'Include destination' switch.
  if (!\Drupal::isConfigSyncing()) {
    lightning_api_update_8301();
  }
}

/**
 * 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',
  ]);
}

/**
 * Replaces openapi_redoc and openapi_swagger_ui with openapi_ui_* equivalents.
 *
 * This is needed because the corresponding openapi update hook only installs
 * them if the old versions are enabled.
 *
 * @see openapi_update_8100
 */
function lightning_api_update_8300() {

  /** @var \Drupal\Core\Extension\ModuleInstallerInterface $module_installer */
  $module_installer = \Drupal::service('module_installer');
  $uninstalled = $module_installer
    ->uninstall([
    'openapi_redoc',
    'openapi_swagger_ui',
  ]);

  // If the uninstall failed -- for example, the openapi_redoc and
  // openapi_swagger_ui modules were physically unavailable, as they would be if
  // they were brought in by Composer back when they were all part of the
  // openapi project before beta2 -- we need to forcibly remove them from the
  // core.extension config so that they are actually uninstalled.
  if (!$uninstalled) {
    \Drupal::configFactory()
      ->getEditable('core.extension')
      ->clear('module.openapi_redoc')
      ->clear('module.openapi_swagger_ui')
      ->save();
    Drupal::keyValue('system.schema')
      ->deleteMultiple([
      'openapi_redoc',
      'openapi_swagger_ui',
    ]);
  }
  $module_installer
    ->install([
    'openapi_ui_swagger',
    'openapi_ui_redoc',
  ]);
}

/**
 * Disables the content view's "include destination" switch for operations.
 *
 * @see lightning_api_view_presave()
 */
function lightning_api_update_8301() {
  if (!Drupal::moduleHandler()
    ->moduleExists('views')) {
    return;
  }
  $view = View::load('content');
  if ($view) {
    lightning_api_view_presave($view
      ->enforceIsNew());
    $view
      ->enforceIsNew(FALSE)
      ->save();
  }
}

/**
 * Installs Lightning Core.
 */
function lightning_api_update_8302() {
  Drupal::service('module_installer')
    ->install([
    'lightning_core',
  ]);
}

/**
 * Uninstalls Simple OAuth Extras.
 */
function lightning_api_update_8401() {
  Drupal::service('module_installer')
    ->uninstall([
    'simple_oauth_extras',
  ]);

  /** @var \Drupal\Core\Entity\EntityLastInstalledSchemaRepositoryInterface $schema */
  $schema = Drupal::service('entity.last_installed_schema.repository');
  $entity_updater = Drupal::entityDefinitionUpdateManager();
  $installed_storages = $schema
    ->getLastInstalledFieldStorageDefinitions('consumer');
  if (empty($installed_storages['redirect_uri'])) {
    $storage_definition = BaseFieldDefinition::create('uri')
      ->setLabel(new TranslatableMarkup('Redirect URI'))
      ->setDescription(new TranslatableMarkup('The URI this client will redirect to when needed.'))
      ->setDisplayOptions('view', [
      'label' => 'inline',
      'weight' => 4,
    ])
      ->setDisplayOptions('form', [
      'weight' => 4,
    ])
      ->setDisplayConfigurable('view', TRUE)
      ->setTranslatable(TRUE)
      ->setSetting('max_length', 255);
    $entity_updater
      ->installFieldStorageDefinition('redirect', 'consumer', 'simple_oauth', $storage_definition);
  }
  if (empty($installed_storages['user_id'])) {
    $storage_definition = BaseFieldDefinition::create('entity_reference')
      ->setLabel(new TranslatableMarkup('User'))
      ->setDescription(new TranslatableMarkup('When no specific user is authenticated Drupal will use this user as the author of all the actions made.'))
      ->setRevisionable(TRUE)
      ->setSetting('target_type', 'user')
      ->setSetting('handler', 'default')
      ->setTranslatable(FALSE)
      ->setDisplayOptions('view', [
      'label' => 'inline',
      'type' => 'entity_reference_label',
      'weight' => 1,
    ])
      ->setCardinality(1)
      ->setDisplayOptions('form', [
      'type' => 'entity_reference_autocomplete',
      'weight' => 0,
      'settings' => [
        'match_operator' => 'CONTAINS',
        'size' => '60',
        'autocomplete_type' => 'tags',
        'placeholder' => '',
      ],
    ]);
    $entity_updater
      ->installFieldStorageDefinition('user_id', 'consumer', 'simple_oauth', $storage_definition);
  }
  if (isset($installed_storages['langcode'])) {
    $storage_definitions = Drupal::service('entity_field.manager')
      ->getFieldStorageDefinitions('consumer');
    $entity_updater
      ->updateFieldStorageDefinition($storage_definitions['langcode']);
  }
}

/**
 * Installs OpenAPI generators for REST and JSON:API.
 */
function lightning_api_update_8402() {
  Drupal::service('module_installer')
    ->install([
    'openapi_jsonapi',
    'openapi_rest',
  ]);
}

/**
 * Implements hook_requirements().
 */
function lightning_api_requirements($phase) {
  $requirements = [];
  if ($phase === 'runtime' && Drupal::moduleHandler()
    ->moduleExists('openapi_rest')) {
    $requirements['openapi_rest_deprecated_dependency'] = [
      'title' => t('Deprecated dependency: OpenAPI REST'),
      'description' => t('The OpenAPI REST module currently ships with Lightning API, but it will not in Lightning API 5.0. If you intend to continue using it, you should explicitly add it to your project now, for example: <code>composer require drupal/openapi_rest</code>. Otherwise, you may want to <a href=":uninstall_url">uninstall it now</a>.', [
        ':uninstall_url' => Url::fromRoute('system.modules_uninstall')
          ->toString(),
      ]),
      'severity' => REQUIREMENT_WARNING,
    ];
  }
  return $requirements;
}

Functions

Namesort descending Description
lightning_api_install Implements hook_install().
lightning_api_requirements Implements hook_requirements().
lightning_api_update_8001 Sets a default value for lightning_api.settings:bundle_docs.
lightning_api_update_8002 Installs the Consumers module.
lightning_api_update_8300 Replaces openapi_redoc and openapi_swagger_ui with openapi_ui_* equivalents.
lightning_api_update_8301 Disables the content view's "include destination" switch for operations.
lightning_api_update_8302 Installs Lightning Core.
lightning_api_update_8401 Uninstalls Simple OAuth Extras.
lightning_api_update_8402 Installs OpenAPI generators for REST and JSON:API.