swagger_ui_formatter.module in Swagger UI Field Formatter 7.2
Same filename and directory in other branches
Swagger ui field formatter functionality.
File
swagger_ui_formatter.moduleView source
<?php
/**
* @file
* Swagger ui field formatter functionality.
*/
/**
* Implements hook_field_formatter_info().
*/
function swagger_ui_formatter_field_formatter_info() {
return array(
'swagger_ui_formatter_swagger_ui' => array(
'label' => t('Swagger UI Formatter'),
'description' => t('Formats file fields with Swagger YAML or JSON files with a rendered swagger ui'),
'field types' => array(
'file',
),
'settings' => array(
'validator' => 'default',
'validator_url' => '',
'doc_expansion' => 'none',
'show_top_bar' => FALSE,
'sort_tags_by_name' => FALSE,
),
),
);
}
/**
* Implements hook_field_formatter_settings_form().
*/
function swagger_ui_formatter_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$form = array();
$form['validator'] = array(
'#type' => 'select',
'#title' => t('Validator'),
'#description' => t("Default=Swagger.io's online validator, None= No validation, Custom=Provide a custom validator url"),
'#default_value' => $settings['validator'],
'#options' => array(
'default' => t('Default'),
'none' => t('None'),
'custom' => t('Custom'),
),
);
$form['validator_url'] = array(
'#type' => 'textfield',
'#title' => t('Validator URL'),
'#description' => t('The custom validator url to be used to validated the swagger files.'),
'#default_value' => $settings['validator_url'],
'#states' => array(
'visible' => array(
':input[name="fields[' . $field['field_name'] . '][settings_edit_form][settings][validator]"]' => array(
'value' => 'custom',
),
),
),
);
$form['doc_expansion'] = array(
'#type' => 'select',
'#title' => t('Doc Expansion'),
'#description' => t('Controls how the API listing is displayed'),
'#default_value' => $settings['doc_expansion'],
'#options' => array(
'none' => t('Default'),
'list' => t('List - shows operations for each resource'),
'full' => t('Fully Expanded - shows operations and their details'),
),
);
$form['show_top_bar'] = array(
'#type' => 'checkbox',
'#title' => t('Show Top Bar'),
'#default_value' => $settings['show_top_bar'],
'#description' => t('Controls whether the Swagger UI top bar should be displayed or not.'),
);
$form['sort_tags_by_name'] = array(
'#type' => 'checkbox',
'#title' => t('Sort tags by name'),
'#default_value' => $settings['sort_tags_by_name'],
'#description' => t('Controls whether the tag groups should be ordered alphabetically or not.'),
);
return $form;
}
/**
* Implements hook_field_formatter_settings_summary().
*/
function swagger_ui_formatter_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
$settings = $display['settings'];
$summary = t('Uses @validator validator, Doc Expansion of "@doc_expansion", shows top bar: @show_top_bar, tags sorted by name: @sort_tags_by_name', array(
'@validator' => $settings['validator'],
'@doc_expansion' => $settings['doc_expansion'],
'@show_top_bar' => $settings['show_top_bar'] ? t('Yes') : t('No'),
'@sort_tags_by_name' => $settings['sort_tags_by_name'] ? t('Yes') : t('No'),
));
return $summary;
}
/**
* Implements hook_libraries_info().
*
* For defining swagger ui 3.0 libraries.
*/
function swagger_ui_formatter_libraries_info() {
$libraries['swagger_ui'] = array(
'name' => 'Swagger UI',
'vendor url' => 'https://swagger.io/swagger-ui',
'download url' => 'https://github.com/swagger-api/swagger-ui/releases',
'version arguments' => array(
'file' => 'package.json',
'pattern' => '@"version":\\s+"([0-9\\.-]+)@',
'lines' => 3,
),
'path' => 'dist',
'integration files' => array(
'swagger_ui_formatter' => array(
'js' => array(
'swagger_ui_formatter.js',
),
),
),
'files' => array(
'js' => array(
'swagger-ui-bundle.js' => array(
'type' => 'file',
'group' => JS_LIBRARY,
'preprocess' => FALSE,
'weight' => 1,
),
'swagger-ui-standalone-preset.js' => array(
'type' => 'file',
'group' => JS_LIBRARY,
'preprocess' => FALSE,
'weight' => 1,
),
),
'css' => array(
'swagger-ui.css',
),
),
);
return $libraries;
}
/**
* Implements hook_field_formatter_view().
*/
function swagger_ui_formatter_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
$library = libraries_load('swagger_ui');
if (!$library['loaded']) {
return array();
}
$element = array();
$settings = $display['settings'];
$swagger_fields = array();
foreach ($items as $delta => $item) {
$swagger_fields[] = array(
'field_name' => 'swagger-ui-' . $item['fid'] . '-' . $delta,
'file_name' => file_create_url($item['uri']),
);
$element[$delta] = array(
'#theme' => 'swagger_ui_formatter',
'#delta' => $delta,
'#field_name' => $item['fid'],
);
}
if ($settings['validator'] == 'none') {
$validator_url = NULL;
}
elseif ($settings['validator'] == 'default') {
$validator_url = FALSE;
}
else {
$validator_url = $settings['validator_url'];
}
$element['#attached']['js'][] = array(
'data' => array(
'swagger_ui_formatter' => array(
'swagger_fields' => $swagger_fields,
'validator_url' => $validator_url,
'doc_expansion' => $settings['doc_expansion'],
'show_top_bar' => $settings['show_top_bar'],
'sort_tags_by_name' => $settings['sort_tags_by_name'],
),
),
'type' => 'setting',
);
return $element;
}
/**
* Implements hook_theme().
*/
function swagger_ui_formatter_theme($existing, $type, $theme, $path) {
$path = drupal_get_path('module', 'swagger_ui_formatter') . '/templates';
return array(
'swagger_ui_formatter' => array(
'variables' => array(
'field_name' => NULL,
'delta' => NULL,
),
'path' => $path,
'template' => 'swagger-ui-formatter',
),
);
}
Functions
Name | Description |
---|---|
swagger_ui_formatter_field_formatter_info | Implements hook_field_formatter_info(). |
swagger_ui_formatter_field_formatter_settings_form | Implements hook_field_formatter_settings_form(). |
swagger_ui_formatter_field_formatter_settings_summary | Implements hook_field_formatter_settings_summary(). |
swagger_ui_formatter_field_formatter_view | Implements hook_field_formatter_view(). |
swagger_ui_formatter_libraries_info | Implements hook_libraries_info(). |
swagger_ui_formatter_theme | Implements hook_theme(). |