swagger_ui_formatter.install in Swagger UI Field Formatter 8.3
File
swagger_ui_formatter.install
View source
<?php
declare (strict_types=1);
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\swagger_ui_formatter\Exception\SwaggerUiLibraryDiscoveryExceptionInterface;
use Drupal\swagger_ui_formatter\Service\SwaggerUiLibraryDiscovery;
function swagger_ui_formatter_requirements(string $phase) : array {
$requirements = [];
if ($phase === 'install') {
$class_resolver = \Drupal::service('class_resolver');
$swagger_ui_library_discovery = $class_resolver
->getInstanceFromDefinition(SwaggerUiLibraryDiscovery::class);
}
else {
$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' => [
'#markup' => $exception
->getMessage(),
],
];
}
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;
}
function swagger_ui_formatter_update_8001() : void {
}
function swagger_ui_formatter_update_8002() : void {
drupal_flush_all_caches();
$storage = \Drupal::entityTypeManager()
->getStorage('entity_view_display');
foreach ($storage
->loadMultiple() as $display) {
$display_needs_update = FALSE;
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();
}
}
}