You are here

function swagger_ui_formatter_requirements in Swagger UI Field Formatter 8.3

Same name and namespace in other branches
  1. 8.2 swagger_ui_formatter.install \swagger_ui_formatter_requirements()
  2. 7.2 swagger_ui_formatter.install \swagger_ui_formatter_requirements()

Implements hook_requirements().

Drush and Config Installer does not perform requirements check before enables a module.

See also

https://www.drupal.org/project/config_installer/issues/3061127

https://github.com/drush-ops/drush/issues/3669

File

./swagger_ui_formatter.install, line 24

Code

function swagger_ui_formatter_requirements(string $phase) : array {
  $requirements = [];

  // Make sure that the Swagger UI library discovery service is available.
  if ($phase === 'install') {

    /** @var \Drupal\Core\DependencyInjection\ClassResolverInterface $class_resolver */
    $class_resolver = \Drupal::service('class_resolver');
    $swagger_ui_library_discovery = $class_resolver
      ->getInstanceFromDefinition(SwaggerUiLibraryDiscovery::class);
  }
  else {

    /** @var \Drupal\swagger_ui_formatter\Service\SwaggerUiLibraryDiscoveryInterface $swagger_ui_library_discovery */
    $swagger_ui_library_discovery = \Drupal::service('swagger_ui_formatter.swagger_ui_library_discovery');
  }
  if (in_array($phase, [
    'runtime',
    'install',
  ])) {
    try {
      $library_dir = $swagger_ui_library_discovery
        ->libraryDirectory();
      $library_version = $swagger_ui_library_discovery
        ->libraryVersion();
      $requirements['swagger_ui_formatter'] = [
        'title' => t('Swagger UI library'),
        'severity' => REQUIREMENT_OK,
        'value' => $library_version,
        'description' => [
          '#markup' => t('Swagger UI library installed at %path.', [
            '%path' => $library_dir,
          ]),
        ],
      ];
    } catch (SwaggerUiLibraryDiscoveryExceptionInterface $exception) {
      $requirements['swagger_ui_formatter'] = [
        'title' => t('Swagger UI library'),
        'severity' => REQUIREMENT_ERROR,
        'description' => [
          // @todo Make this translatable.
          '#markup' => $exception
            ->getMessage(),
        ],
      ];
    }

    // Make sure that the status info is being refreshed if the theme changes.
    if ($swagger_ui_library_discovery instanceof CacheableDependencyInterface) {
      $requirement_description =& $requirements['swagger_ui_formatter']['description'];
      $cacheable_metadata = CacheableMetadata::createFromRenderArray($requirement_description)
        ->merge(CacheableMetadata::createFromObject($swagger_ui_library_discovery));
      $cacheable_metadata
        ->applyTo($requirement_description);
    }
  }
  return $requirements;
}