You are here

swagger_ui_formatter.install in Swagger UI Field Formatter 8.2

Same filename and directory in other branches
  1. 8.3 swagger_ui_formatter.install
  2. 7.2 swagger_ui_formatter.install

Install, update and uninstall functions for Swagger UI Field Formatter.

File

swagger_ui_formatter.install
View source
<?php

/**
 * @file
 * Install, update and uninstall functions for Swagger UI Field Formatter.
 */

/**
 * Implements hook_requirements().
 *
 * Drush and Config Installer does not perform requirements check before
 * enables a module.
 *
 * @see https://www.drupal.org/project/config_installer/issues/3061127
 * @see https://github.com/drush-ops/drush/issues/3669
 */
function swagger_ui_formatter_requirements($phase) {
  $requirements = [];

  // Make sure _swagger_ui_formatter_get_library_path() is defined.
  if ($phase === 'install') {
    module_load_include('module', 'swagger_ui_formatter');
  }
  if (in_array($phase, [
    'runtime',
    'install',
  ])) {
    if ($library_path = _swagger_ui_formatter_get_library_path()) {
      $requirements['swagger_ui_library'] = [
        'title' => t('Swagger UI'),
        'severity' => REQUIREMENT_OK,
        'value' => _swagger_ui_formatter_get_library_version(),
        'description' => t('Swagger UI library installed at %path.', [
          '%path' => DRUPAL_ROOT . $library_path,
        ]),
      ];
    }
    else {
      $requirements['swagger_ui_library'] = [
        'title' => t('Swagger UI'),
        'severity' => REQUIREMENT_ERROR,
        'description' => t('Swagger UI library was not found. Download <a href=":link" target="_blank">the appropriate version</a> and place it in the [DRUPAL ROOT]/libraries directory (e.g. [DRUPAL ROOT]/libraries/swagger-ui).', [
          ':link' => 'https://github.com/swagger-api/swagger-ui/releases',
        ]),
      ];
    }
  }
  return $requirements;
}

/**
 * Clear cache due to updated library definitions.
 */
function swagger_ui_formatter_update_8001() {

  // An empty update forces a call to drupal_flush_all_caches().
}

/**
 * Update old field formatter IDs.
 */
function swagger_ui_formatter_update_8002() {

  // Rebuild cache first due to updated library and class definitions and to
  // avoid undefined/invalid plugin ID errors.
  drupal_flush_all_caches();

  /** @var \Drupal\Core\Config\Entity\ConfigEntityStorage $storage */
  $storage = \Drupal::entityTypeManager()
    ->getStorage('entity_view_display');

  /** @var \Drupal\Core\Entity\Entity\EntityViewDisplay $display */
  foreach ($storage
    ->loadMultiple() as $display) {
    $display_needs_update = FALSE;

    // Loop through components (enabled "non-hidden" fields and properties) and
    // search for the old "swagger_ui" field formatter ID. Once one is found
    // change it to the new ID and mark the display for update.
    foreach ($display
      ->getComponents() as $name => $component) {
      if (isset($component['type']) && $component['type'] === 'swagger_ui') {
        $component['type'] = 'swagger_ui_file';
        $display
          ->setComponent($name, $component);
        $display_needs_update = TRUE;
      }
    }
    if ($display_needs_update) {
      $display
        ->save();
    }
  }
}

Functions

Namesort descending Description
swagger_ui_formatter_requirements Implements hook_requirements().
swagger_ui_formatter_update_8001 Clear cache due to updated library definitions.
swagger_ui_formatter_update_8002 Update old field formatter IDs.